Skip to content

Commit

Permalink
buildExecutionContext: simplify errors handling (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Jul 27, 2019
1 parent 52820eb commit b82e5a6
Showing 1 changed file with 16 additions and 27 deletions.
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

0 comments on commit b82e5a6

Please sign in to comment.