Skip to content

Commit 56f0b39

Browse files
committed
[RFC] Make operation name optional.
Implements graphql/graphql-spec#99
1 parent 2cfbfce commit 56f0b39

File tree

4 files changed

+18
-5
lines changed

4 files changed

+18
-5
lines changed

src/execution/execute.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ function buildExecutionContext(
174174
'Must provide operation name if query contains multiple operations.'
175175
);
176176
}
177-
if (!operationName || definition.name.value === operationName) {
177+
if (!operationName ||
178+
definition.name && definition.name.value === operationName) {
178179
operation = definition;
179180
}
180181
break;

src/language/__tests__/parser.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fragment MissingOn Type
9292
it('parse provides useful error when using source', () => {
9393
expect(
9494
() => parse(new Source('query', 'MyQuery.graphql'))
95-
).to.throw('Syntax Error MyQuery.graphql (1:6) Expected Name, found EOF');
95+
).to.throw('Syntax Error MyQuery.graphql (1:6) Expected {, found EOF');
9696
});
9797

9898
it('parses variable inline values', () => {
@@ -193,6 +193,14 @@ fragment ${fragmentName} on Type {
193193
`)).to.not.throw();
194194
});
195195

196+
it('parses anonymous operations', () => {
197+
expect(() => parse(`
198+
mutation {
199+
mutationField
200+
}
201+
`)).to.not.throw();
202+
});
203+
196204
it('parse creates ast', () => {
197205

198206
var source = new Source(`{

src/language/parser.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ function parseDefinition(parser): Definition {
215215
/**
216216
* OperationDefinition :
217217
* - SelectionSet
218-
* - OperationType Name VariableDefinitions? Directives? SelectionSet
218+
* - OperationType Name? VariableDefinitions? Directives? SelectionSet
219219
*
220220
* OperationType : one of query mutation
221221
*/
@@ -234,10 +234,14 @@ function parseOperationDefinition(parser): OperationDefinition {
234234
}
235235
var operationToken = expect(parser, TokenKind.NAME);
236236
var operation = operationToken.value;
237+
var name;
238+
if (peek(parser, TokenKind.NAME)) {
239+
name = parseName(parser);
240+
}
237241
return {
238242
kind: OPERATION_DEFINITION,
239243
operation,
240-
name: parseName(parser),
244+
name,
241245
variableDefinitions: parseVariableDefinitions(parser),
242246
directives: parseDirectives(parser),
243247
selectionSet: parseSelectionSet(parser),

src/utilities/getOperationAST.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function getOperationAST(
3333
return null;
3434
}
3535
operation = definition;
36-
} else if (definition.name.value === operationName) {
36+
} else if (definition.name && definition.name.value === operationName) {
3737
return definition;
3838
}
3939
}

0 commit comments

Comments
 (0)