@@ -129,6 +129,252 @@ mod field_execution {
129129 }
130130}
131131
132+ mod non_null_null_result {
133+ use std:: future;
134+
135+ use arcstr:: ArcStr ;
136+
137+ use crate :: {
138+ BoxFuture , DefaultScalarValue , EmptyMutation , EmptySubscription , ExecutionResult , Executor ,
139+ FieldError , FieldResult , GraphQLType , GraphQLValue , GraphQLValueAsync , Registry , RootNode ,
140+ Value , executor:: ExecutionError , graphql, graphql_object, macros:: reflect,
141+ marker:: IsOutputType , parser:: SourcePosition , schema:: meta:: MetaType ,
142+ types:: base:: Arguments ,
143+ } ;
144+
145+ struct Query ;
146+
147+ impl GraphQLType < DefaultScalarValue > for Query {
148+ fn name ( _: & Self :: TypeInfo ) -> Option < ArcStr > {
149+ Some ( "Query" . into ( ) )
150+ }
151+
152+ fn meta (
153+ info : & Self :: TypeInfo ,
154+ registry : & mut Registry < DefaultScalarValue > ,
155+ ) -> MetaType < DefaultScalarValue > {
156+ let fields = & [ registry. field :: < String > ( "critical" , & ( ) ) ] ;
157+ registry. build_object_type :: < Self > ( info, fields) . into_meta ( )
158+ }
159+ }
160+
161+ impl GraphQLValue < DefaultScalarValue > for Query {
162+ type Context = ( ) ;
163+ type TypeInfo = ( ) ;
164+
165+ fn type_name ( & self , info : & Self :: TypeInfo ) -> Option < ArcStr > {
166+ <Self as GraphQLType >:: name ( info)
167+ }
168+
169+ fn resolve_field (
170+ & self ,
171+ _: & Self :: TypeInfo ,
172+ _: & str ,
173+ _: & Arguments < DefaultScalarValue > ,
174+ _: & Executor < Self :: Context > ,
175+ ) -> ExecutionResult {
176+ Ok ( Value :: null ( ) )
177+ }
178+ }
179+
180+ impl GraphQLValueAsync < DefaultScalarValue > for Query {
181+ fn resolve_field_async < ' a > (
182+ & ' a self ,
183+ _: & ' a Self :: TypeInfo ,
184+ _: & ' a str ,
185+ _: & ' a Arguments < DefaultScalarValue > ,
186+ _: & ' a Executor < Self :: Context > ,
187+ ) -> BoxFuture < ' a , ExecutionResult > {
188+ Box :: pin ( future:: ready ( Ok ( Value :: null ( ) ) ) )
189+ }
190+ }
191+
192+ fn schema ( ) -> RootNode < Query , EmptyMutation , EmptySubscription > {
193+ RootNode :: new ( Query , EmptyMutation :: new ( ) , EmptySubscription :: new ( ) )
194+ }
195+
196+ #[ tokio:: test]
197+ async fn direct_null_from_non_null_field_is_reported ( ) {
198+ let document = "{ critical }" ;
199+
200+ for ( value, errors) in [
201+ crate :: execute ( document, None , & schema ( ) , & graphql:: vars! { } , & ( ) )
202+ . await
203+ . expect ( "async execution failed" ) ,
204+ crate :: execute_sync ( document, None , & schema ( ) , & graphql:: vars! { } , & ( ) )
205+ . expect ( "sync execution failed" ) ,
206+ ] {
207+ assert_eq ! ( value, graphql:: value!( null) ) ;
208+ assert_eq ! (
209+ errors,
210+ [ ExecutionError :: new(
211+ SourcePosition :: new( 2 , 0 , 2 ) ,
212+ & [ "critical" ] ,
213+ FieldError :: new(
214+ "Cannot return null for non-nullable field Query.critical." ,
215+ graphql:: value!( null) ,
216+ ) ,
217+ ) ] ,
218+ ) ;
219+ }
220+ }
221+
222+ /// Object with a non-null `value` field that resolves to `null`.
223+ struct NullValue ;
224+
225+ impl GraphQLType < DefaultScalarValue > for NullValue {
226+ fn name ( _: & Self :: TypeInfo ) -> Option < ArcStr > {
227+ Some ( "NullValue" . into ( ) )
228+ }
229+
230+ fn meta (
231+ info : & Self :: TypeInfo ,
232+ registry : & mut Registry < DefaultScalarValue > ,
233+ ) -> MetaType < DefaultScalarValue > {
234+ let fields = & [ registry. field :: < String > ( "value" , & ( ) ) ] ;
235+ registry. build_object_type :: < Self > ( info, fields) . into_meta ( )
236+ }
237+ }
238+
239+ impl GraphQLValue < DefaultScalarValue > for NullValue {
240+ type Context = ( ) ;
241+ type TypeInfo = ( ) ;
242+
243+ fn type_name ( & self , info : & Self :: TypeInfo ) -> Option < ArcStr > {
244+ <Self as GraphQLType >:: name ( info)
245+ }
246+
247+ fn resolve_field (
248+ & self ,
249+ _: & Self :: TypeInfo ,
250+ _: & str ,
251+ _: & Arguments < DefaultScalarValue > ,
252+ _: & Executor < Self :: Context > ,
253+ ) -> ExecutionResult {
254+ Ok ( Value :: null ( ) )
255+ }
256+ }
257+
258+ impl GraphQLValueAsync < DefaultScalarValue > for NullValue {
259+ fn resolve_field_async < ' a > (
260+ & ' a self ,
261+ _: & ' a Self :: TypeInfo ,
262+ _: & ' a str ,
263+ _: & ' a Arguments < DefaultScalarValue > ,
264+ _: & ' a Executor < Self :: Context > ,
265+ ) -> BoxFuture < ' a , ExecutionResult > {
266+ Box :: pin ( future:: ready ( Ok ( Value :: null ( ) ) ) )
267+ }
268+ }
269+
270+ // `#[graphql_object]` requires these traits for the types of its fields. Derived types
271+ // implement the traits automatically.
272+ impl IsOutputType < DefaultScalarValue > for NullValue { }
273+
274+ impl reflect:: BaseType < DefaultScalarValue > for NullValue {
275+ const NAME : reflect:: Type = "NullValue" ;
276+ }
277+
278+ impl reflect:: BaseSubTypes < DefaultScalarValue > for NullValue {
279+ const NAMES : reflect:: Types = & [ <Self as reflect:: BaseType < DefaultScalarValue > >:: NAME ] ;
280+ }
281+
282+ impl reflect:: WrappedType < DefaultScalarValue > for NullValue {
283+ const VALUE : reflect:: WrappedValue = 1 ;
284+ }
285+
286+ struct Nested ;
287+
288+ #[ graphql_object( scalar = DefaultScalarValue ) ]
289+ impl Nested {
290+ fn null_value ( ) -> NullValue {
291+ NullValue
292+ }
293+
294+ fn failing ( ) -> FieldResult < String > {
295+ Err ( "failure" . into ( ) )
296+ }
297+ }
298+
299+ struct NestedQuery ;
300+
301+ #[ graphql_object( scalar = DefaultScalarValue ) ]
302+ impl NestedQuery {
303+ fn nested ( ) -> Option < Nested > {
304+ Some ( Nested )
305+ }
306+
307+ fn non_null_nested ( ) -> Nested {
308+ Nested
309+ }
310+ }
311+
312+ /// Executes `document` against a [`NestedQuery`] schema asynchronously and synchronously.
313+ async fn execute_nested (
314+ document : & str ,
315+ ) -> [ ( Value , Vec < ExecutionError < DefaultScalarValue > > ) ; 2 ] {
316+ let schema = RootNode :: new ( NestedQuery , EmptyMutation :: new ( ) , EmptySubscription :: new ( ) ) ;
317+ [
318+ crate :: execute ( document, None , & schema, & graphql:: vars! { } , & ( ) )
319+ . await
320+ . expect ( "async execution failed" ) ,
321+ crate :: execute_sync ( document, None , & schema, & graphql:: vars! { } , & ( ) )
322+ . expect ( "sync execution failed" ) ,
323+ ]
324+ }
325+
326+ fn null_value_error ( index : usize , path : & [ & str ] ) -> ExecutionError < DefaultScalarValue > {
327+ ExecutionError :: new (
328+ SourcePosition :: new ( index, 0 , index) ,
329+ path,
330+ FieldError :: new (
331+ "Cannot return null for non-nullable field NullValue.value." ,
332+ graphql:: value!( null) ,
333+ ) ,
334+ )
335+ }
336+
337+ #[ tokio:: test]
338+ async fn null_propagates_to_nearest_nullable_parent ( ) {
339+ for ( value, errors) in execute_nested ( "{ nested { nullValue { value } } }" ) . await {
340+ assert_eq ! ( value, graphql:: value!( { "nested" : null} ) ) ;
341+ assert_eq ! (
342+ errors,
343+ [ null_value_error( 23 , & [ "nested" , "nullValue" , "value" ] ) ] ,
344+ ) ;
345+ }
346+ }
347+
348+ #[ tokio:: test]
349+ async fn non_null_parents_add_no_error_for_null_child ( ) {
350+ for ( value, errors) in execute_nested ( "{ nonNullNested { nullValue { value } } }" ) . await {
351+ assert_eq ! ( value, graphql:: value!( null) ) ;
352+ assert_eq ! (
353+ errors,
354+ [ null_value_error(
355+ 30 ,
356+ & [ "nonNullNested" , "nullValue" , "value" ]
357+ ) ] ,
358+ ) ;
359+ }
360+ }
361+
362+ #[ tokio:: test]
363+ async fn non_null_parent_adds_no_error_for_failed_child ( ) {
364+ for ( value, errors) in execute_nested ( "{ nonNullNested { failing } }" ) . await {
365+ assert_eq ! ( value, graphql:: value!( null) ) ;
366+ assert_eq ! (
367+ errors,
368+ [ ExecutionError :: new(
369+ SourcePosition :: new( 18 , 0 , 18 ) ,
370+ & [ "nonNullNested" , "failing" ] ,
371+ FieldError :: new( "failure" , graphql:: value!( null) ) ,
372+ ) ] ,
373+ ) ;
374+ }
375+ }
376+ }
377+
132378mod merge_parallel_fragments {
133379 use crate :: {
134380 graphql, graphql_object,
0 commit comments