Skip to content

Errors if empty string is provided as operationName #2149

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

Merged
merged 1 commit into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/execution/__tests__/executor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,24 @@ describe('Execute: Handles basic execution tasks', () => {
});
});

it('errors if empty string is provided as operation name', () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Type',
fields: {
a: { type: GraphQLString },
},
}),
});
const document = parse('{ a }');
const operationName = '';

const result = execute({ schema, document, operationName });
expect(result).to.deep.equal({
errors: [{ message: 'Unknown operation named "".' }],
});
});

it('uses the query schema for queries', () => {
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
Expand Down
27 changes: 11 additions & 16 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,20 @@ export function buildExecutionContext(
typeResolver?: ?GraphQLTypeResolver<mixed, mixed>,
): $ReadOnlyArray<GraphQLError> | ExecutionContext {
let operation: OperationDefinitionNode | void;
let hasMultipleAssumedOperations = false;
const fragments: ObjMap<FragmentDefinitionNode> = Object.create(null);
for (const definition of document.definitions) {
switch (definition.kind) {
case Kind.OPERATION_DEFINITION:
if (!operationName && operation) {
hasMultipleAssumedOperations = true;
} else if (
!operationName ||
(definition.name && definition.name.value === operationName)
) {
if (operationName == null) {
if (operation !== undefined) {
return [
new GraphQLError(
'Must provide operation name if query contains multiple operations.',
),
];
}
operation = definition;
} else if (definition.name && definition.name.value === operationName) {
operation = definition;
}
break;
Expand All @@ -300,20 +303,12 @@ export function buildExecutionContext(
}

if (!operation) {
if (operationName) {
if (operationName != null) {
return [new GraphQLError(`Unknown operation named "${operationName}".`)];
}
return [new GraphQLError('Must provide an operation.')];
}

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

const coercedVariableValues = getVariableValues(
schema,
operation.variableDefinitions || [],
Expand Down