Skip to content

Commit b82e5a6

Browse files
buildExecutionContext: simplify errors handling (#2054)
1 parent 52820eb commit b82e5a6

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

src/execution/execute.js

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ export function buildExecutionContext(
272272
fieldResolver: ?GraphQLFieldResolver<mixed, mixed>,
273273
typeResolver?: ?GraphQLTypeResolver<mixed, mixed>,
274274
): $ReadOnlyArray<GraphQLError> | ExecutionContext {
275-
const errors: Array<GraphQLError> = [];
276275
let operation: OperationDefinitionNode | void;
277276
let hasMultipleAssumedOperations = false;
278277
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
@@ -297,40 +296,30 @@ export function buildExecutionContext(
297296

298297
if (!operation) {
299298
if (operationName) {
300-
errors.push(
301-
new GraphQLError(`Unknown operation named "${operationName}".`),
302-
);
303-
} else {
304-
errors.push(new GraphQLError('Must provide an operation.'));
299+
return [new GraphQLError(`Unknown operation named "${operationName}".`)];
305300
}
306-
} else if (hasMultipleAssumedOperations) {
307-
errors.push(
301+
return [new GraphQLError('Must provide an operation.')];
302+
}
303+
304+
if (hasMultipleAssumedOperations) {
305+
return [
308306
new GraphQLError(
309307
'Must provide operation name if query contains multiple operations.',
310308
),
311-
);
309+
];
312310
}
313311

314-
let variableValues;
315-
if (operation) {
316-
const coercedVariableValues = getVariableValues(
317-
schema,
318-
operation.variableDefinitions || [],
319-
rawVariableValues || {},
320-
);
321-
322-
if (coercedVariableValues.errors) {
323-
errors.push(...coercedVariableValues.errors);
324-
} else {
325-
variableValues = coercedVariableValues.coerced;
326-
}
327-
}
312+
const coercedVariableValues = getVariableValues(
313+
schema,
314+
operation.variableDefinitions || [],
315+
rawVariableValues || {},
316+
);
328317

329-
if (errors.length !== 0) {
330-
return errors;
318+
if (coercedVariableValues.errors) {
319+
return coercedVariableValues.errors;
331320
}
332321

333-
invariant(operation, 'Has operation if no errors.');
322+
const variableValues = coercedVariableValues.coerced;
334323
invariant(variableValues, 'Has variables if no errors.');
335324

336325
return {
@@ -342,7 +331,7 @@ export function buildExecutionContext(
342331
variableValues,
343332
fieldResolver: fieldResolver || defaultFieldResolver,
344333
typeResolver: typeResolver || defaultTypeResolver,
345-
errors,
334+
errors: [],
346335
};
347336
}
348337

0 commit comments

Comments
 (0)