Skip to content

Commit

Permalink
Uniform parsing of queries with short-hand syntax with regular queries (
Browse files Browse the repository at this point in the history
#1094)

* Fix parsing of regular queries

by making the parser return the same result as with short-hand syntax
queries

* Add test to prevent future regressions

* Default to empty array when parsing

* Empty array for variable definitions in tests
  • Loading branch information
lebedev authored and leebyron committed Nov 30, 2017
1 parent 1f6b64e commit 7a7ffe4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
52 changes: 51 additions & 1 deletion src/language/__tests__/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('Parser', () => {
loc: { start: 0, end: 40 },
operation: 'query',
name: null,
variableDefinitions: null,
variableDefinitions: [],
directives: [],
selectionSet:
{ kind: Kind.SELECTION_SET,
Expand Down Expand Up @@ -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 });
Expand Down
9 changes: 4 additions & 5 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7a7ffe4

Please sign in to comment.