Skip to content

Uniform parsing of queries with short-hand syntax with regular queries #1094

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
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
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