Skip to content

Commit 5a13648

Browse files
committed
Extract completeObjectValue from CompleteValue
1 parent 4c5904c commit 5a13648

File tree

1 file changed

+57
-30
lines changed

1 file changed

+57
-30
lines changed

src/execution/execute.js

Lines changed: 57 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -705,12 +705,20 @@ function completeValue(
705705
return completeLeafValue(exeContext, returnType, fieldASTs, info, result);
706706
}
707707

708+
if (returnType instanceof GraphQLObjectType) {
709+
return completeObjectValue(
710+
exeContext,
711+
returnType,
712+
fieldASTs,
713+
info,
714+
result
715+
);
716+
}
717+
708718
// Field type must be Object, Interface or Union and expect sub-selections.
709719
let runtimeType: ?GraphQLObjectType;
710720

711-
if (returnType instanceof GraphQLObjectType) {
712-
runtimeType = returnType;
713-
} else if (isAbstractType(returnType)) {
721+
if (isAbstractType(returnType)) {
714722
const abstractType = ((returnType: any): GraphQLAbstractType);
715723
runtimeType = abstractType.getObjectType(result, info);
716724
if (runtimeType && !abstractType.isPossibleType(runtimeType)) {
@@ -726,33 +734,13 @@ function completeValue(
726734
return null;
727735
}
728736

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+
);
756744
}
757745

758746
/**
@@ -804,6 +792,45 @@ function completeLeafValue(
804792
return isNullish(serializedResult) ? null : serializedResult;
805793
}
806794

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+
807834
/**
808835
* If a resolve function is not given, then a default resolve behavior is used
809836
* which takes the property of the source object of the same name as the field

0 commit comments

Comments
 (0)