Skip to content

Commit b1582f1

Browse files
committed
use the payload type's non-null error
1 parent 266aa7a commit b1582f1

7 files changed

Lines changed: 22 additions & 25 deletions

File tree

lib/graphql/execution/interpreter/runtime.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,8 @@ def evaluate_selections(path, scoped_context, owner_object, owner_type, selectio
254254
def continue_value(path, value, field, is_non_null, ast_node)
255255
if value.nil?
256256
if is_non_null
257-
err = field.owner::InvalidNullError.new(field.owner, field, value)
257+
parent_type = field.owner_type
258+
err = parent_type::InvalidNullError.new(parent_type, field, value)
258259
write_invalid_null_in_response(path, err)
259260
else
260261
write_in_response(path, nil)
@@ -311,7 +312,7 @@ def continue_field(path, value, field, type, ast_node, next_selections, is_non_n
311312
possible_types = query.possible_types(type)
312313

313314
if !possible_types.include?(resolved_type)
314-
parent_type = field.owner
315+
parent_type = field.owner_type
315316
err_class = type::UnresolvedTypeError
316317
type_error = err_class.new(resolved_value, field, parent_type, resolved_type, possible_types)
317318
schema.type_error(type_error, context)

lib/graphql/schema/field.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,18 @@ class Field
3636
# @return [Symbol] The method on the type to look up
3737
attr_reader :resolver_method
3838

39-
# @return [Class] The type that this field belongs to
39+
# @return [Class] The thing this field was defined on (type, mutation, resolver)
4040
attr_accessor :owner
4141

42+
# @return [Class] The GraphQL type this field belongs to. (For fields defined on mutations, it's the payload type)
43+
def owner_type
44+
@owner_type ||= if owner < GraphQL::Schema::Mutation
45+
owner.payload_type
46+
else
47+
owner
48+
end
49+
end
50+
4251
# @return [Symbol] the original name of the field, passed in by the user
4352
attr_reader :original_name
4453

lib/graphql/schema/member.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
require 'graphql/schema/member/cached_graphql_definition'
55
require 'graphql/schema/member/graphql_type_names'
66
require 'graphql/schema/member/has_ast_node'
7-
require 'graphql/schema/member/has_invalid_null_error'
87
require 'graphql/schema/member/has_path'
98
require 'graphql/schema/member/has_unresolved_type_error'
109
require 'graphql/schema/member/relay_shortcuts'

lib/graphql/schema/member/has_invalid_null_error.rb

Lines changed: 0 additions & 17 deletions
This file was deleted.

lib/graphql/schema/mutation.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ class Schema
6060
#
6161
class Mutation < GraphQL::Schema::Resolver
6262
extend GraphQL::Schema::Member::HasFields
63-
extend GraphQL::Schema::Member::HasInvalidNullError
6463
extend GraphQL::Schema::Resolver::HasPayloadType
6564

6665
class << self

lib/graphql/schema/object.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class Schema
77
class Object < GraphQL::Schema::Member
88
extend GraphQL::Schema::Member::AcceptsDefinition
99
extend GraphQL::Schema::Member::HasFields
10-
extend GraphQL::Schema::Member::HasInvalidNullError
1110

1211
# @return [Object] the application object this type is wrapping
1312
attr_reader :object
@@ -72,6 +71,13 @@ def initialize(object, context)
7271
end
7372

7473
class << self
74+
# Set up a type-specific invalid null error to use when this object's non-null fields wrongly return `nil`.
75+
# It should help with debugging and bug tracker integrations.
76+
def inherited(child_class)
77+
child_class.const_set(:InvalidNullError, Class.new(GraphQL::InvalidNullError))
78+
super
79+
end
80+
7581
def implements(*new_interfaces, **options)
7682
new_memberships = []
7783
new_interfaces.each do |int|

spec/graphql/schema/mutation_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@
117117
it "raises a mutation-specific invalid null error" do
118118
query_str = "mutation { returnInvalidNull { int } }"
119119
response = Jazz::Schema.execute(query_str)
120-
assert_equal ["Cannot return null for non-nullable field ReturnInvalidNull.int"], response["errors"].map { |e| e["message"] }
121-
assert_instance_of Jazz::ReturnInvalidNull::InvalidNullError, response.query.context.errors.first
120+
assert_equal ["Cannot return null for non-nullable field ReturnInvalidNullPayload.int"], response["errors"].map { |e| e["message"] }
121+
assert_instance_of Jazz::ReturnInvalidNull.payload_type::InvalidNullError, response.query.context.errors.first
122122
end
123123
end
124124

0 commit comments

Comments
 (0)