Skip to content

Commit 00c5b69

Browse files
Errors if empty string is provided as operationName (#2149)
1 parent 27dac69 commit 00c5b69

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/execution/__tests__/executor-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,24 @@ describe('Execute: Handles basic execution tasks', () => {
748748
});
749749
});
750750

751+
it('errors if empty string is provided as operation name', () => {
752+
const schema = new GraphQLSchema({
753+
query: new GraphQLObjectType({
754+
name: 'Type',
755+
fields: {
756+
a: { type: GraphQLString },
757+
},
758+
}),
759+
});
760+
const document = parse('{ a }');
761+
const operationName = '';
762+
763+
const result = execute({ schema, document, operationName });
764+
expect(result).to.deep.equal({
765+
errors: [{ message: 'Unknown operation named "".' }],
766+
});
767+
});
768+
751769
it('uses the query schema for queries', () => {
752770
const schema = new GraphQLSchema({
753771
query: new GraphQLObjectType({

src/execution/execute.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,20 @@ export function buildExecutionContext(
279279
typeResolver?: ?GraphQLTypeResolver<mixed, mixed>,
280280
): $ReadOnlyArray<GraphQLError> | ExecutionContext {
281281
let operation: OperationDefinitionNode | void;
282-
let hasMultipleAssumedOperations = false;
283282
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
284283
for (const definition of document.definitions) {
285284
switch (definition.kind) {
286285
case Kind.OPERATION_DEFINITION:
287-
if (!operationName && operation) {
288-
hasMultipleAssumedOperations = true;
289-
} else if (
290-
!operationName ||
291-
(definition.name && definition.name.value === operationName)
292-
) {
286+
if (operationName == null) {
287+
if (operation !== undefined) {
288+
return [
289+
new GraphQLError(
290+
'Must provide operation name if query contains multiple operations.',
291+
),
292+
];
293+
}
294+
operation = definition;
295+
} else if (definition.name && definition.name.value === operationName) {
293296
operation = definition;
294297
}
295298
break;
@@ -300,20 +303,12 @@ export function buildExecutionContext(
300303
}
301304

302305
if (!operation) {
303-
if (operationName) {
306+
if (operationName != null) {
304307
return [new GraphQLError(`Unknown operation named "${operationName}".`)];
305308
}
306309
return [new GraphQLError('Must provide an operation.')];
307310
}
308311

309-
if (hasMultipleAssumedOperations) {
310-
return [
311-
new GraphQLError(
312-
'Must provide operation name if query contains multiple operations.',
313-
),
314-
];
315-
}
316-
317312
const coercedVariableValues = getVariableValues(
318313
schema,
319314
operation.variableDefinitions || [],

0 commit comments

Comments
 (0)