@@ -1027,66 +1027,62 @@ function completeObjectValue(
1027
1027
path: ResponsePath,
1028
1028
result: mixed
1029
1029
): mixed {
1030
- // If there is an isTypeOf predicate function,
1031
- // call it with the current result.
1032
- // Otherwise assume the type is correct
1033
- if ( ! returnType . isTypeOf ) {
1034
- return validateResultTypeAndExecuteFields (
1035
- exeContext ,
1036
- returnType ,
1037
- fieldNodes ,
1038
- info ,
1039
- path ,
1040
- result ,
1041
- true
1042
- ) ;
1043
- }
1044
-
1045
- const isTypeOf = returnType.isTypeOf(result, exeContext.contextValue, info);
1030
+ // If there is an isTypeOf predicate function, call it with the
1031
+ // current result. If isTypeOf returns false, then raise an error rather
1032
+ // than continuing execution.
1033
+ if ( returnType . isTypeOf ) {
1034
+ const isTypeOf = returnType . isTypeOf ( result , exeContext . contextValue , info ) ;
1035
+
1036
+ if ( isThenable ( isTypeOf ) ) {
1037
+ return ( ( isTypeOf : any ) : Promise < boolean > ) . then ( isTypeOfResult => {
1038
+ if ( ! isTypeOfResult ) {
1039
+ throw invalidReturnTypeError ( returnType , result , fieldNodes ) ;
1040
+ }
1041
+ return collectAndExecuteSubfields (
1042
+ exeContext ,
1043
+ returnType ,
1044
+ fieldNodes ,
1045
+ info ,
1046
+ path ,
1047
+ result
1048
+ ) ;
1049
+ } ) ;
1050
+ }
1046
1051
1047
- if (isThenable(isTypeOf)) {
1048
- return ( ( isTypeOf : any ) : Promise < boolean > ) . then ( isTypeOfResult => (
1049
- validateResultTypeAndExecuteFields (
1050
- exeContext ,
1051
- returnType ,
1052
- fieldNodes ,
1053
- info ,
1054
- path ,
1055
- result ,
1056
- isTypeOfResult
1057
- )
1058
- ) ) ;
1052
+ if ( ! isTypeOf ) {
1053
+ throw invalidReturnTypeError ( returnType , result , fieldNodes ) ;
1054
+ }
1059
1055
}
1060
1056
1061
- return validateResultTypeAndExecuteFields (
1057
+ return collectAndExecuteSubfields (
1062
1058
exeContext,
1063
1059
returnType,
1064
1060
fieldNodes,
1065
1061
info,
1066
1062
path,
1067
- result,
1068
- ((isTypeOf: any): boolean)
1063
+ result
1069
1064
);
1070
1065
}
1071
1066
1072
- function validateResultTypeAndExecuteFields (
1067
+ function invalidReturnTypeError (
1068
+ returnType : GraphQLObjectType ,
1069
+ result : mixed ,
1070
+ fieldNodes : Array < FieldNode >
1071
+ ): GraphQLError {
1072
+ return new GraphQLError (
1073
+ `Expected value of type "${ returnType . name } " but got: ${ String ( result ) } .` ,
1074
+ fieldNodes
1075
+ ) ;
1076
+ }
1077
+
1078
+ function collectAndExecuteSubfields(
1073
1079
exeContext: ExecutionContext,
1074
1080
returnType: GraphQLObjectType,
1075
1081
fieldNodes: Array< FieldNode > ,
1076
1082
info: GraphQLResolveInfo,
1077
1083
path: ResponsePath,
1078
- result: mixed,
1079
- isTypeOfResult: boolean
1084
+ result: mixed
1080
1085
): mixed {
1081
- // If isTypeOf returns false, then raise an error
1082
- // rather than continuing execution.
1083
- if ( ! isTypeOfResult ) {
1084
- throw new GraphQLError (
1085
- `Expected value of type "${ returnType . name } " but got: ${ String ( result ) } .` ,
1086
- fieldNodes
1087
- ) ;
1088
- }
1089
-
1090
1086
// Collect sub-fields to execute to complete this value.
1091
1087
let subFieldNodes = Object . create ( null ) ;
1092
1088
const visitedFragmentNames = Object . create ( null ) ;
0 commit comments