Skip to content

Commit a3c746c

Browse files
authored
Factor out checking isTypeOf from subfield exe
More single-purpose functions
1 parent 9239253 commit a3c746c

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

src/execution/execute.js

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,66 +1027,62 @@ function completeObjectValue(
10271027
path: ResponsePath,
10281028
result: mixed
10291029
): 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+
}
10461051

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+
}
10591055
}
10601056

1061-
return validateResultTypeAndExecuteFields(
1057+
return collectAndExecuteSubfields(
10621058
exeContext,
10631059
returnType,
10641060
fieldNodes,
10651061
info,
10661062
path,
1067-
result,
1068-
((isTypeOf: any): boolean)
1063+
result
10691064
);
10701065
}
10711066

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(
10731079
exeContext: ExecutionContext,
10741080
returnType: GraphQLObjectType,
10751081
fieldNodes: Array<FieldNode>,
10761082
info: GraphQLResolveInfo,
10771083
path: ResponsePath,
1078-
result: mixed,
1079-
isTypeOfResult: boolean
1084+
result: mixed
10801085
): 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-
10901086
// Collect sub-fields to execute to complete this value.
10911087
let subFieldNodes = Object.create(null);
10921088
const visitedFragmentNames = Object.create(null);

0 commit comments

Comments
 (0)