@@ -693,7 +693,7 @@ function completeValue(
693
693
return completed ;
694
694
}
695
695
696
- // If result is null-like, return null.
696
+ // If result value is null-ish (null, undefined, or NaN) then return null.
697
697
if (isNullish(result)) {
698
698
return null ;
699
699
}
@@ -703,15 +703,18 @@ function completeValue(
703
703
return completeListValue ( exeContext , returnType , fieldASTs , info , result ) ;
704
704
}
705
705
706
- // If field type is Scalar or Enum, serialize to a valid value, returning
707
- // null if serialization is not possible.
706
+ // If field type is a leaf type, Scalar or Enum, serialize to a valid value,
707
+ // returning null if serialization is not possible.
708
708
if (returnType instanceof GraphQLScalarType ||
709
709
returnType instanceof GraphQLEnumType) {
710
710
return completeLeafValue ( exeContext , returnType , fieldASTs , info , result ) ;
711
711
}
712
712
713
- if (returnType instanceof GraphQLObjectType) {
714
- return completeObjectValue (
713
+ // If field type is an abstract type, Interface or Union, determine the
714
+ // runtime Object type and complete for that type.
715
+ if (returnType instanceof GraphQLInterfaceType ||
716
+ returnType instanceof GraphQLUnionType) {
717
+ return completeAbstractValue (
715
718
exeContext ,
716
719
returnType ,
717
720
fieldASTs ,
@@ -720,9 +723,9 @@ function completeValue(
720
723
) ;
721
724
}
722
725
723
- if (returnType instanceof GraphQLInterfaceType ||
724
- returnType instanceof GraphQLUnionType ) {
725
- return completeAbstractValue (
726
+ // If field type is Object, execute and complete all sub-selections.
727
+ if ( returnType instanceof GraphQLObjectType ) {
728
+ return completeObjectValue (
726
729
exeContext ,
727
730
returnType ,
728
731
fieldASTs ,
@@ -731,7 +734,7 @@ function completeValue(
731
734
) ;
732
735
}
733
736
734
- // Not reachable
737
+ // Not reachable. All possible output types have been considered.
735
738
invariant(
736
739
false,
737
740
`Cannot complete value of unexpected type "${ returnType } ".`
@@ -788,7 +791,40 @@ function completeLeafValue(
788
791
}
789
792
790
793
/**
791
- * Complete an Object value by evaluating all sub-selections.
794
+ * Complete a value of an abstract type by determining the runtime object type
795
+ * of that value, then complete the value for that type.
796
+ */
797
+ function completeAbstractValue(
798
+ exeContext: ExecutionContext,
799
+ returnType: GraphQLAbstractType,
800
+ fieldASTs: Array< Field > ,
801
+ info: GraphQLResolveInfo,
802
+ result: mixed
803
+ ): mixed {
804
+ const runtimeType = returnType . getObjectType ( result , info ) ;
805
+ if ( runtimeType && ! returnType . isPossibleType ( runtimeType ) ) {
806
+ throw new GraphQLError (
807
+ `Runtime Object type "${ runtimeType } " is not a possible type ` +
808
+ `for "${ returnType } ".` ,
809
+ fieldASTs
810
+ ) ;
811
+ }
812
+
813
+ if ( ! runtimeType ) {
814
+ return null ;
815
+ }
816
+
817
+ return completeObjectValue (
818
+ exeContext ,
819
+ runtimeType ,
820
+ fieldASTs ,
821
+ info ,
822
+ result
823
+ ) ;
824
+ }
825
+
826
+ /**
827
+ * Complete an Object value by executing all sub-selections.
792
828
*/
793
829
function completeObjectValue(
794
830
exeContext: ExecutionContext,
@@ -826,40 +862,6 @@ function completeObjectValue(
826
862
return executeFields ( exeContext , returnType , result , subFieldASTs ) ;
827
863
}
828
864
829
- /**
830
- * Complete an value of an abstract type by determining the runtime type of
831
- * that value, then completing based on that type.
832
- */
833
- function completeAbstractValue (
834
- exeContext : ExecutionContext ,
835
- returnType : GraphQLAbstractType ,
836
- fieldASTs : Array < Field > ,
837
- info: GraphQLResolveInfo,
838
- result: mixed
839
- ): mixed {
840
- const abstractType = ( ( returnType : any ) : GraphQLAbstractType ) ;
841
- const runtimeType = abstractType . getObjectType ( result , info ) ;
842
- if ( runtimeType && ! abstractType . isPossibleType ( runtimeType ) ) {
843
- throw new GraphQLError (
844
- `Runtime Object type "${ runtimeType } " is not a possible type ` +
845
- `for "${ abstractType } ".` ,
846
- fieldASTs
847
- ) ;
848
- }
849
-
850
- if ( ! runtimeType ) {
851
- return null ;
852
- }
853
-
854
- return completeObjectValue (
855
- exeContext ,
856
- runtimeType ,
857
- fieldASTs ,
858
- info ,
859
- result
860
- ) ;
861
- }
862
-
863
865
/**
864
866
* If a resolve function is not given, then a default resolve behavior is used
865
867
* which takes the property of the source object of the same name as the field
0 commit comments