Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buildExecutionContext: simplify errors handling #2054

Merged
merged 1 commit into from Jul 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 16 additions & 27 deletions src/execution/execute.js
Expand Up @@ -272,7 +272,6 @@ export function buildExecutionContext(
fieldResolver: ?GraphQLFieldResolver<mixed, mixed>,
typeResolver?: ?GraphQLTypeResolver<mixed, mixed>,
): $ReadOnlyArray<GraphQLError> | ExecutionContext {
const errors: Array<GraphQLError> = [];
let operation: OperationDefinitionNode | void;
let hasMultipleAssumedOperations = false;
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
Expand All @@ -297,40 +296,30 @@ export function buildExecutionContext(

if (!operation) {
if (operationName) {
errors.push(
new GraphQLError(`Unknown operation named "${operationName}".`),
);
} else {
errors.push(new GraphQLError('Must provide an operation.'));
return [new GraphQLError(`Unknown operation named "${operationName}".`)];
}
} else if (hasMultipleAssumedOperations) {
errors.push(
return [new GraphQLError('Must provide an operation.')];
}

if (hasMultipleAssumedOperations) {
return [
new GraphQLError(
'Must provide operation name if query contains multiple operations.',
),
);
];
}

let variableValues;
if (operation) {
const coercedVariableValues = getVariableValues(
schema,
operation.variableDefinitions || [],
rawVariableValues || {},
);

if (coercedVariableValues.errors) {
errors.push(...coercedVariableValues.errors);
} else {
variableValues = coercedVariableValues.coerced;
}
}
const coercedVariableValues = getVariableValues(
schema,
operation.variableDefinitions || [],
rawVariableValues || {},
);

if (errors.length !== 0) {
return errors;
if (coercedVariableValues.errors) {
return coercedVariableValues.errors;
}

invariant(operation, 'Has operation if no errors.');
const variableValues = coercedVariableValues.coerced;
invariant(variableValues, 'Has variables if no errors.');

return {
Expand All @@ -342,7 +331,7 @@ export function buildExecutionContext(
variableValues,
fieldResolver: fieldResolver || defaultFieldResolver,
typeResolver: typeResolver || defaultTypeResolver,
errors,
errors: [],
};
}

Expand Down