File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -91,13 +91,13 @@ def -@
9191require "graphql/tracing"
9292require "graphql/dig"
9393require "graphql/execution"
94+ require "graphql/runtime_type_error"
95+ require "graphql/unresolved_type_error"
96+ require "graphql/invalid_null_error"
9497require "graphql/schema"
9598require "graphql/query"
9699require "graphql/directive"
97100require "graphql/execution"
98- require "graphql/runtime_type_error"
99- require "graphql/unresolved_type_error"
100- require "graphql/invalid_null_error"
101101require "graphql/types"
102102require "graphql/relay"
103103require "graphql/boolean_type"
Original file line number Diff line number Diff line change 44require 'graphql/schema/member/cached_graphql_definition'
55require 'graphql/schema/member/graphql_type_names'
66require 'graphql/schema/member/has_ast_node'
7+ require 'graphql/schema/member/has_invalid_null_error'
78require 'graphql/schema/member/has_path'
89require 'graphql/schema/member/has_unresolved_type_error'
910require 'graphql/schema/member/relay_shortcuts'
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 |
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 )
You can’t perform that action at this time.
0 commit comments