From b82e5a6b5b44b7fd4536ca59ea22262c6f545bdc Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sat, 27 Jul 2019 19:37:34 +0300 Subject: [PATCH] buildExecutionContext: simplify errors handling (#2054) --- src/execution/execute.js | 43 +++++++++++++++------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/execution/execute.js b/src/execution/execute.js index 83cbaf803a..69d4248648 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -272,7 +272,6 @@ export function buildExecutionContext( fieldResolver: ?GraphQLFieldResolver, typeResolver?: ?GraphQLTypeResolver, ): $ReadOnlyArray | ExecutionContext { - const errors: Array = []; let operation: OperationDefinitionNode | void; let hasMultipleAssumedOperations = false; const fragments: ObjMap = Object.create(null); @@ -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 { @@ -342,7 +331,7 @@ export function buildExecutionContext( variableValues, fieldResolver: fieldResolver || defaultFieldResolver, typeResolver: typeResolver || defaultTypeResolver, - errors, + errors: [], }; }