Skip to content

Commit 266aa7a

Browse files
committed
Add invalid null error for mutations
1 parent ec4d698 commit 266aa7a

7 files changed

Lines changed: 39 additions & 10 deletions

File tree

lib/graphql.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ def -@
9191
require "graphql/tracing"
9292
require "graphql/dig"
9393
require "graphql/execution"
94+
require "graphql/runtime_type_error"
95+
require "graphql/unresolved_type_error"
96+
require "graphql/invalid_null_error"
9497
require "graphql/schema"
9598
require "graphql/query"
9699
require "graphql/directive"
97100
require "graphql/execution"
98-
require "graphql/runtime_type_error"
99-
require "graphql/unresolved_type_error"
100-
require "graphql/invalid_null_error"
101101
require "graphql/types"
102102
require "graphql/relay"
103103
require "graphql/boolean_type"

lib/graphql/schema/member.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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'
78
require 'graphql/schema/member/has_path'
89
require 'graphql/schema/member/has_unresolved_type_error'
910
require 'graphql/schema/member/relay_shortcuts'
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module GraphQL
4+
class Schema
5+
class Member
6+
module HasInvalidNullError
7+
# Set up a member-specific invalid null error
8+
# to use when this member's non-null fields wrongly return `nil`.
9+
# It should help with debugging and bug tracker integrations.
10+
def inherited(child_class)
11+
child_class.const_set(:InvalidNullError, Class.new(GraphQL::InvalidNullError))
12+
super
13+
end
14+
end
15+
end
16+
end
17+
end

lib/graphql/schema/mutation.rb

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

6566
class << self

lib/graphql/schema/object.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ 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
1011

1112
# @return [Object] the application object this type is wrapping
1213
attr_reader :object
@@ -71,13 +72,6 @@ def initialize(object, context)
7172
end
7273

7374
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-
8175
def implements(*new_interfaces, **options)
8276
new_memberships = []
8377
new_interfaces.each do |int|

spec/graphql/schema/mutation_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@
113113
response = Jazz::Schema.execute(query_str)
114114
assert_equal 2, response["errors"].length, "It should return two errors"
115115
end
116+
117+
it "raises a mutation-specific invalid null error" do
118+
query_str = "mutation { returnInvalidNull { int } }"
119+
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
122+
end
116123
end
117124

118125
describe ".null" do

spec/support/jazz.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,14 @@ def resolve
776776
end
777777
end
778778

779+
class ReturnInvalidNull < GraphQL::Schema::Mutation
780+
field :int, Integer, null: false
781+
782+
def resolve
783+
{ int: nil }
784+
end
785+
end
786+
779787
class Mutation < BaseObject
780788
field :add_ensemble, Ensemble, null: false do
781789
argument :input, EnsembleInput, required: true
@@ -796,6 +804,7 @@ class Mutation < BaseObject
796804
field :has_extras, mutation: HasExtras
797805
field :has_extras_stripped, mutation: HasExtrasStripped
798806
field :has_field_extras, mutation: HasFieldExtras, extras: [:lookahead]
807+
field :return_invalid_null, mutation: ReturnInvalidNull
799808

800809
def add_ensemble(input:)
801810
ens = Models::Ensemble.new(input.name)

0 commit comments

Comments
 (0)