Skip to content

Commit

Permalink
Factor out runtime type validation
Browse files Browse the repository at this point in the history
Moves runtime type validation to its own function.
  • Loading branch information
leebyron authored Jan 20, 2017
1 parent 1305360 commit 1fb45d3
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -949,47 +949,55 @@ function completeAbstractValue(
const runtimeTypePromise: Promise<GraphQLObjectType | string> =
(runtimeType: any);
return runtimeTypePromise.then(resolvedRuntimeType =>
validateRuntimeTypeAndCompleteObjectValue(
completeObjectValue(
exeContext,
returnType,
fieldNodes,
info,
path,
ensureType(exeContext.schema, resolvedRuntimeType),
ensureValidRuntimeType(
resolvedRuntimeType,
exeContext,
returnType,
fieldNodes,
info,
result
),
result
)
);
}

return validateRuntimeTypeAndCompleteObjectValue(
return completeObjectValue(
exeContext,
returnType,
fieldNodes,
info,
path,
ensureType(exeContext.schema, runtimeType),
ensureValidRuntimeType(
resolvedRuntimeType,
exeContext,
returnType,
fieldNodes,
info,
result
),
result
);
}

function ensureType(
schema: GraphQLSchema,
typeOrName: string | GraphQLObjectType
): GraphQLObjectType {
return typeof typeOrName === 'string' ?
schema.getType(typeOrName) :
typeOrName;
}

function validateRuntimeTypeAndCompleteObjectValue(
function ensureValidRuntimeType(
runtimeTypeOrName: string | GraphQLObjectType | void
exeContext: ExecutionContext,
returnType: GraphQLAbstractType,
fieldNodes: Array<FieldNode>,
info: GraphQLResolveInfo,
path: ResponsePath,
runtimeType: GraphQLObjectType,
result: mixed
): mixed {
): GraphQLObjectType {
const runtimeType = typeof runtimeTypeOrName === 'string' ?
exeContext.schema.getType(runtimeTypeOrName) :
runtimeTypeOrName;

if (!(runtimeType instanceof GraphQLObjectType)) {
throw new GraphQLError(
`Abstract type ${returnType.name} must resolve to an Object type at ` +
Expand All @@ -1007,14 +1015,7 @@ function validateRuntimeTypeAndCompleteObjectValue(
);
}

return completeObjectValue(
exeContext,
runtimeType,
fieldNodes,
info,
path,
result
);
return runtimeType;
}

/**
Expand Down

0 comments on commit 1fb45d3

Please sign in to comment.