Skip to content

Commit 533fc43

Browse files
committed
Minor follow-up to #311
1 parent 371a5a0 commit 533fc43

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

src/execution/execute.js

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ function completeValue(
693693
return completed;
694694
}
695695

696-
// If result is null-like, return null.
696+
// If result value is null-ish (null, undefined, or NaN) then return null.
697697
if (isNullish(result)) {
698698
return null;
699699
}
@@ -703,15 +703,18 @@ function completeValue(
703703
return completeListValue(exeContext, returnType, fieldASTs, info, result);
704704
}
705705

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.
708708
if (returnType instanceof GraphQLScalarType ||
709709
returnType instanceof GraphQLEnumType) {
710710
return completeLeafValue(exeContext, returnType, fieldASTs, info, result);
711711
}
712712

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(
715718
exeContext,
716719
returnType,
717720
fieldASTs,
@@ -720,9 +723,9 @@ function completeValue(
720723
);
721724
}
722725

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(
726729
exeContext,
727730
returnType,
728731
fieldASTs,
@@ -731,7 +734,7 @@ function completeValue(
731734
);
732735
}
733736

734-
// Not reachable
737+
// Not reachable. All possible output types have been considered.
735738
invariant(
736739
false,
737740
`Cannot complete value of unexpected type "${returnType}".`
@@ -788,7 +791,40 @@ function completeLeafValue(
788791
}
789792

790793
/**
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.
792828
*/
793829
function completeObjectValue(
794830
exeContext: ExecutionContext,
@@ -826,40 +862,6 @@ function completeObjectValue(
826862
return executeFields(exeContext, returnType, result, subFieldASTs);
827863
}
828864

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

0 commit comments

Comments
 (0)