@@ -705,12 +705,20 @@ function completeValue(
705
705
return completeLeafValue ( exeContext , returnType , fieldASTs , info , result ) ;
706
706
}
707
707
708
+ if (returnType instanceof GraphQLObjectType) {
709
+ return completeObjectValue (
710
+ exeContext ,
711
+ returnType ,
712
+ fieldASTs ,
713
+ info ,
714
+ result
715
+ ) ;
716
+ }
717
+
708
718
// Field type must be Object, Interface or Union and expect sub-selections.
709
719
let runtimeType: ?GraphQLObjectType;
710
720
711
- if (returnType instanceof GraphQLObjectType) {
712
- runtimeType = returnType ;
713
- } else if (isAbstractType(returnType)) {
721
+ if (isAbstractType(returnType)) {
714
722
const abstractType = ( ( returnType : any ) : GraphQLAbstractType ) ;
715
723
runtimeType = abstractType . getObjectType ( result , info ) ;
716
724
if ( runtimeType && ! abstractType . isPossibleType ( runtimeType ) ) {
@@ -726,33 +734,13 @@ function completeValue(
726
734
return null ;
727
735
}
728
736
729
- // If there is an isTypeOf predicate function, call it with the
730
- // current result. If isTypeOf returns false, then raise an error rather
731
- // than continuing execution.
732
- if (runtimeType.isTypeOf && ! runtimeType . isTypeOf ( result , info ) ) {
733
- throw new GraphQLError (
734
- `Expected value of type "${ runtimeType } " but got: ${ result } .` ,
735
- fieldASTs
736
- ) ;
737
- }
738
-
739
- // Collect sub-fields to execute to complete this value.
740
- let subFieldASTs = Object.create(null);
741
- const visitedFragmentNames = Object.create(null);
742
- for (let i = 0; i < fieldASTs . length ; i ++ ) {
743
- const selectionSet = fieldASTs [ i ] . selectionSet ;
744
- if ( selectionSet ) {
745
- subFieldASTs = collectFields (
746
- exeContext ,
747
- runtimeType ,
748
- selectionSet ,
749
- subFieldASTs ,
750
- visitedFragmentNames
751
- ) ;
752
- }
753
- }
754
-
755
- return executeFields ( exeContext , runtimeType , result , subFieldASTs ) ;
737
+ return completeObjectValue(
738
+ exeContext,
739
+ runtimeType,
740
+ fieldASTs,
741
+ info,
742
+ result
743
+ );
756
744
}
757
745
758
746
/**
@@ -804,6 +792,45 @@ function completeLeafValue(
804
792
return isNullish ( serializedResult ) ? null : serializedResult ;
805
793
}
806
794
795
+ /**
796
+ * Complete an Object value by evaluating all sub-selections.
797
+ */
798
+ function completeObjectValue(
799
+ exeContext: ExecutionContext,
800
+ returnType: GraphQLObjectType,
801
+ fieldASTs: Array< Field > ,
802
+ info: GraphQLResolveInfo,
803
+ result: mixed
804
+ ): mixed {
805
+ // If there is an isTypeOf predicate function, call it with the
806
+ // current result. If isTypeOf returns false, then raise an error rather
807
+ // than continuing execution.
808
+ if ( returnType . isTypeOf && ! returnType . isTypeOf ( result , info ) ) {
809
+ throw new GraphQLError (
810
+ `Expected value of type "${ returnType } " but got: ${ result } .` ,
811
+ fieldASTs
812
+ ) ;
813
+ }
814
+
815
+ // Collect sub-fields to execute to complete this value.
816
+ let subFieldASTs = Object.create(null);
817
+ const visitedFragmentNames = Object.create(null);
818
+ for (let i = 0; i < fieldASTs . length ; i ++ ) {
819
+ const selectionSet = fieldASTs [ i ] . selectionSet ;
820
+ if ( selectionSet ) {
821
+ subFieldASTs = collectFields (
822
+ exeContext ,
823
+ returnType ,
824
+ selectionSet ,
825
+ subFieldASTs ,
826
+ visitedFragmentNames
827
+ ) ;
828
+ }
829
+ }
830
+
831
+ return executeFields ( exeContext , returnType , result , subFieldASTs ) ;
832
+ }
833
+
807
834
/**
808
835
* If a resolve function is not given, then a default resolve behavior is used
809
836
* which takes the property of the source object of the same name as the field
0 commit comments