Skip to content

Commit c573130

Browse files
committed
parser: add error handling for descriptions on shorthand queries and type extensions
1 parent 1149981 commit c573130

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

src/language/__tests__/parser-test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,5 +767,18 @@ describe('Parser', () => {
767767
expect(varDef.type.name.value).to.equal('Int');
768768
}
769769
});
770+
771+
it('produces sensible error for description on shorthand query', () => {
772+
expect(() =>
773+
parse(dedent`
774+
"This is a description"
775+
{
776+
field
777+
}
778+
`),
779+
).to.throw(
780+
'Syntax Error: Unexpected description, descriptions are not supported on shorthand queries.',
781+
);
782+
});
770783
});
771784
});

src/language/parser.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,6 @@ export class Parser {
270270

271271
if (keywordToken.kind === TokenKind.NAME) {
272272
switch (keywordToken.value) {
273-
case 'query':
274-
case 'mutation':
275-
case 'subscription':
276-
return this.parseOperationDefinition();
277-
case 'fragment':
278-
return this.parseFragmentDefinition();
279273
case 'schema':
280274
return this.parseSchemaDefinition();
281275
case 'scalar':
@@ -292,18 +286,37 @@ export class Parser {
292286
return this.parseInputObjectTypeDefinition();
293287
case 'directive':
294288
return this.parseDirectiveDefinition();
289+
}
290+
291+
if (hasDescription && keywordToken.value === 'extend') {
292+
throw syntaxError(
293+
this._lexer.source,
294+
this._lexer.token.start,
295+
'Unexpected description, descriptions are not supported on type extensions.',
296+
);
297+
}
298+
299+
switch (keywordToken.value) {
300+
case 'query':
301+
case 'mutation':
302+
case 'subscription':
303+
return this.parseOperationDefinition();
304+
case 'fragment':
305+
return this.parseFragmentDefinition();
295306
case 'extend':
296-
if (hasDescription) {
297-
throw syntaxError(
298-
this._lexer.source,
299-
this._lexer.token.start,
300-
'Unexpected description, descriptions are not supported on type extensions.',
301-
);
302-
}
303307
return this.parseTypeSystemExtension();
304308
}
305309
}
306310

311+
// Check for shorthand query with description
312+
if (hasDescription && keywordToken.kind === TokenKind.BRACE_L) {
313+
throw syntaxError(
314+
this._lexer.source,
315+
this._lexer.token.start,
316+
'Unexpected description, descriptions are not supported on shorthand queries.',
317+
);
318+
}
319+
307320
throw this.unexpected(keywordToken);
308321
}
309322

0 commit comments

Comments
 (0)