Skip to content

Commit dea5aac

Browse files
JeffRMooreleebyron
authored andcommitted
Improve coercion error messages
* Remove redundant falsy check for runtimeType * Replace silent conversion to null with an invariant and a useful error message * Add an error message for unexpected return value from resolveType * Make sure null and undefined mean could not determine type while false or 0 indicates wrong return type
1 parent 3974438 commit dea5aac

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/execution/execute.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,12 +817,19 @@ function completeAbstractValue(
817817
returnType.resolveType(result, exeContext.contextValue, info) :
818818
defaultResolveTypeFn(result, exeContext.contextValue, info, returnType);
819819

820-
if (!runtimeType) {
821-
return null;
822-
}
820+
invariant(
821+
!isNullish(runtimeType),
822+
`Could not determine runtime type of value "${result}" for field ${
823+
info.parentType}.${info.fieldName}.`
824+
);
825+
invariant(
826+
runtimeType instanceof GraphQLObjectType,
827+
`resolveType must return an instance of GraphQLObjectType for field ${
828+
info.parentType}.${info.fieldName}, received "${runtimeType}".`
829+
);
823830

824831
const schema = exeContext.schema;
825-
if (runtimeType && !schema.isPossibleType(returnType, runtimeType)) {
832+
if (!schema.isPossibleType(returnType, runtimeType)) {
826833
throw new GraphQLError(
827834
`Runtime Object type "${runtimeType}" is not a possible type ` +
828835
`for "${returnType}".`,

0 commit comments

Comments
 (0)