diff --git a/src/language/__tests__/parser-test.js b/src/language/__tests__/parser-test.js index 190f070fc7..fe0559c77b 100644 --- a/src/language/__tests__/parser-test.js +++ b/src/language/__tests__/parser-test.js @@ -218,7 +218,7 @@ describe('Parser', () => { loc: { start: 0, end: 40 }, operation: 'query', name: null, - variableDefinitions: null, + variableDefinitions: [], directives: [], selectionSet: { kind: Kind.SELECTION_SET, @@ -270,6 +270,56 @@ describe('Parser', () => { ); }); + it('creates ast from nameless query without variables', () => { + + const source = new Source(`query { + node { + id + } +} +`); + const result = parse(source); + + expect(result).to.containSubset( + { kind: Kind.DOCUMENT, + loc: { start: 0, end: 30 }, + definitions: + [ { kind: Kind.OPERATION_DEFINITION, + loc: { start: 0, end: 29 }, + operation: 'query', + name: null, + variableDefinitions: [], + directives: [], + selectionSet: + { kind: Kind.SELECTION_SET, + loc: { start: 6, end: 29 }, + selections: + [ { kind: Kind.FIELD, + loc: { start: 10, end: 27 }, + alias: null, + name: + { kind: Kind.NAME, + loc: { start: 10, end: 14 }, + value: 'node' }, + arguments: [], + directives: [], + selectionSet: + { kind: Kind.SELECTION_SET, + loc: { start: 15, end: 27 }, + selections: + [ { kind: Kind.FIELD, + loc: { start: 21, end: 23 }, + alias: null, + name: + { kind: Kind.NAME, + loc: { start: 21, end: 23 }, + value: 'id' }, + arguments: [], + directives: [], + selectionSet: null } ] } } ] } } ] } + ); + }); + it('allows parsing without source location information', () => { const source = new Source('{ id }'); const result = parse(source, { noLocation: true }); diff --git a/src/language/parser.js b/src/language/parser.js index a0cbee1437..02f3b7ddc8 100644 --- a/src/language/parser.js +++ b/src/language/parser.js @@ -275,17 +275,16 @@ function parseOperationDefinition(lexer: Lexer<*>): OperationDefinitionNode { kind: OPERATION_DEFINITION, operation: 'query', name: null, - variableDefinitions: null, + variableDefinitions: [], directives: [], selectionSet: parseSelectionSet(lexer), loc: loc(lexer, start) }; } const operation = parseOperationType(lexer); - let name; - if (peek(lexer, TokenKind.NAME)) { - name = parseName(lexer); - } + const name = peek(lexer, TokenKind.NAME) ? + parseName(lexer) : + null; return { kind: OPERATION_DEFINITION, operation,