@@ -258,16 +258,25 @@ export class Parser {
258258 * - InputObjectTypeDefinition
259259 */
260260 parseDefinition ( ) : DefinitionNode {
261- if ( this . peek ( TokenKind . BRACE_L ) ) {
262- return this . parseOperationDefinition ( ) ;
263- }
264261
265262 // Many definitions begin with a description and require a lookahead.
266263 const hasDescription = this . peekDescription ( ) ;
267264 const keywordToken = hasDescription
268265 ? this . _lexer . lookahead ( )
269266 : this . _lexer . token ;
270267
268+ if ( keywordToken . kind === TokenKind . BRACE_L ) {
269+ // Check for shorthand query with description
270+ if ( hasDescription ) {
271+ throw syntaxError (
272+ this . _lexer . source ,
273+ this . _lexer . token . start ,
274+ 'Unexpected description, shorthand queries do not support descriptions.' ,
275+ ) ;
276+ }
277+ return this . parseOperationDefinition ( ) ;
278+ }
279+
271280 if ( keywordToken . kind === TokenKind . NAME ) {
272281 switch ( keywordToken . value ) {
273282 case 'schema' :
@@ -286,37 +295,27 @@ export class Parser {
286295 return this . parseInputObjectTypeDefinition ( ) ;
287296 case 'directive' :
288297 return this . parseDirectiveDefinition ( ) ;
298+ case 'query' :
299+ case 'mutation' :
300+ case 'subscription' :
301+ return this . parseOperationDefinition ( ) ;
302+ case 'fragment' :
303+ return this . parseFragmentDefinition ( ) ;
289304 }
290305
291- if ( hasDescription && keywordToken . value === 'extend' ) {
306+ if ( hasDescription ) {
292307 throw syntaxError (
293308 this . _lexer . source ,
294309 this . _lexer . token . start ,
295- 'Unexpected description, descriptions are not supported on type extensions .' ,
310+ 'Unexpected description, only GraphQL definitions support descriptions .' ,
296311 ) ;
297312 }
298313
299- switch ( keywordToken . value ) {
300- case 'query' :
301- case 'mutation' :
302- case 'subscription' :
303- return this . parseOperationDefinition ( ) ;
304- case 'fragment' :
305- return this . parseFragmentDefinition ( ) ;
306- case 'extend' :
307- return this . parseTypeSystemExtension ( ) ;
314+ if ( keywordToken . value == 'extend' ) {
315+ return this . parseTypeSystemExtension ( ) ;
308316 }
309317 }
310318
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-
320319 throw this . unexpected ( keywordToken ) ;
321320 }
322321
0 commit comments