diff --git a/src/execution/execute.js b/src/execution/execute.js index 4dd8b02ec7..5137a5b1f7 100644 --- a/src/execution/execute.js +++ b/src/execution/execute.js @@ -174,7 +174,8 @@ function buildExecutionContext( 'Must provide operation name if query contains multiple operations.' ); } - if (!operationName || definition.name.value === operationName) { + if (!operationName || + definition.name && definition.name.value === operationName) { operation = definition; } break; diff --git a/src/language/__tests__/parser.js b/src/language/__tests__/parser.js index 46da1be287..6cc6af056a 100644 --- a/src/language/__tests__/parser.js +++ b/src/language/__tests__/parser.js @@ -92,7 +92,7 @@ fragment MissingOn Type it('parse provides useful error when using source', () => { expect( () => parse(new Source('query', 'MyQuery.graphql')) - ).to.throw('Syntax Error MyQuery.graphql (1:6) Expected Name, found EOF'); + ).to.throw('Syntax Error MyQuery.graphql (1:6) Expected {, found EOF'); }); it('parses variable inline values', () => { @@ -193,6 +193,14 @@ fragment ${fragmentName} on Type { `)).to.not.throw(); }); + it('parses anonymous operations', () => { + expect(() => parse(` + mutation { + mutationField + } + `)).to.not.throw(); + }); + it('parse creates ast', () => { var source = new Source(`{ diff --git a/src/language/parser.js b/src/language/parser.js index c45c98480b..68fe3ea462 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -215,7 +215,7 @@ function parseDefinition(parser): Definition { /** * OperationDefinition : * - SelectionSet - * - OperationType Name VariableDefinitions? Directives? SelectionSet + * - OperationType Name? VariableDefinitions? Directives? SelectionSet * * OperationType : one of query mutation */ @@ -234,10 +234,14 @@ function parseOperationDefinition(parser): OperationDefinition { } var operationToken = expect(parser, TokenKind.NAME); var operation = operationToken.value; + var name; + if (peek(parser, TokenKind.NAME)) { + name = parseName(parser); + } return { kind: OPERATION_DEFINITION, operation, - name: parseName(parser), + name, variableDefinitions: parseVariableDefinitions(parser), directives: parseDirectives(parser), selectionSet: parseSelectionSet(parser), diff --git a/src/utilities/getOperationAST.js b/src/utilities/getOperationAST.js index 81ce74028a..cecd51cfa5 100644 --- a/src/utilities/getOperationAST.js +++ b/src/utilities/getOperationAST.js @@ -33,7 +33,7 @@ export function getOperationAST( return null; } operation = definition; - } else if (definition.name.value === operationName) { + } else if (definition.name && definition.name.value === operationName) { return definition; } }