Skip to content

Commit 4c5904c

Browse files
committed
Extract completeLeafValue from CompleteValue
1 parent ab77470 commit 4c5904c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/execution/execute.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from '../type/definition';
2626
import type {
2727
GraphQLType,
28+
GraphQLLeafType,
2829
GraphQLAbstractType,
2930
GraphQLFieldDefinition,
3031
GraphQLResolveInfo,
@@ -701,9 +702,7 @@ function completeValue(
701702
// null if serialization is not possible.
702703
if (returnType instanceof GraphQLScalarType ||
703704
returnType instanceof GraphQLEnumType) {
704-
invariant(returnType.serialize, 'Missing serialize method on type');
705-
const serializedResult = returnType.serialize(result);
706-
return isNullish(serializedResult) ? null : serializedResult;
705+
return completeLeafValue(exeContext, returnType, fieldASTs, info, result);
707706
}
708707

709708
// Field type must be Object, Interface or Union and expect sub-selections.
@@ -789,6 +788,22 @@ function completeListValue(
789788
return containsPromise ? Promise.all(completedResults) : completedResults;
790789
}
791790

791+
/**
792+
* Complete a Scalar or Enum by serializing to a valid value, returning
793+
* null if serialization is not possible.
794+
*/
795+
function completeLeafValue(
796+
exeContext: ExecutionContext,
797+
returnType: GraphQLLeafType,
798+
fieldASTs: Array<Field>,
799+
info: GraphQLResolveInfo,
800+
result: mixed
801+
): mixed {
802+
invariant(returnType.serialize, 'Missing serialize method on type');
803+
const serializedResult = returnType.serialize(result);
804+
return isNullish(serializedResult) ? null : serializedResult;
805+
}
806+
792807
/**
793808
* If a resolve function is not given, then a default resolve behavior is used
794809
* which takes the property of the source object of the same name as the field

0 commit comments

Comments
 (0)