@@ -250,6 +250,12 @@ export class Parser {
250
250
251
251
if ( keywordToken . kind === TokenKind . NAME ) {
252
252
switch ( keywordToken . value ) {
253
+ case 'query' :
254
+ case 'mutation' :
255
+ case 'subscription' :
256
+ return this . parseOperationDefinition ( ) ;
257
+ case 'fragment' :
258
+ return this . parseFragmentDefinition ( ) ;
253
259
case 'schema' :
254
260
return this . parseSchemaDefinition ( ) ;
255
261
case 'scalar' :
@@ -266,24 +272,14 @@ export class Parser {
266
272
return this . parseInputObjectTypeDefinition ( ) ;
267
273
case 'directive' :
268
274
return this . parseDirectiveDefinition ( ) ;
269
- }
270
-
271
- if ( hasDescription ) {
272
- throw syntaxError (
273
- this . _lexer . source ,
274
- this . _lexer . token . start ,
275
- 'Unexpected description, descriptions are supported only on type definitions.' ,
276
- ) ;
277
- }
278
-
279
- switch ( keywordToken . value ) {
280
- case 'query' :
281
- case 'mutation' :
282
- case 'subscription' :
283
- return this . parseOperationDefinition ( ) ;
284
- case 'fragment' :
285
- return this . parseFragmentDefinition ( ) ;
286
275
case 'extend' :
276
+ if ( hasDescription ) {
277
+ throw syntaxError (
278
+ this . _lexer . source ,
279
+ this . _lexer . token . start ,
280
+ 'Unexpected description, descriptions are not supported on type extensions.' ,
281
+ ) ;
282
+ }
287
283
return this . parseTypeSystemExtension ( ) ;
288
284
}
289
285
}
@@ -300,23 +296,28 @@ export class Parser {
300
296
*/
301
297
parseOperationDefinition ( ) : OperationDefinitionNode {
302
298
const start = this . _lexer . token ;
299
+
303
300
if ( this . peek ( TokenKind . BRACE_L ) ) {
304
301
return this . node < OperationDefinitionNode > ( start , {
305
302
kind : Kind . OPERATION_DEFINITION ,
303
+ description : undefined ,
306
304
operation : OperationTypeNode . QUERY ,
307
305
name : undefined ,
308
306
variableDefinitions : [ ] ,
309
307
directives : [ ] ,
310
308
selectionSet : this . parseSelectionSet ( ) ,
311
309
} ) ;
312
310
}
311
+
312
+ const description = this . parseDescription ( ) ;
313
313
const operation = this . parseOperationType ( ) ;
314
314
let name ;
315
315
if ( this . peek ( TokenKind . NAME ) ) {
316
316
name = this . parseName ( ) ;
317
317
}
318
318
return this . node < OperationDefinitionNode > ( start , {
319
319
kind : Kind . OPERATION_DEFINITION ,
320
+ description,
320
321
operation,
321
322
name,
322
323
variableDefinitions : this . parseVariableDefinitions ( ) ,
@@ -359,6 +360,7 @@ export class Parser {
359
360
parseVariableDefinition ( ) : VariableDefinitionNode {
360
361
return this . node < VariableDefinitionNode > ( this . _lexer . token , {
361
362
kind : Kind . VARIABLE_DEFINITION ,
363
+ description : this . parseDescription ( ) ,
362
364
variable : this . parseVariable ( ) ,
363
365
type : ( this . expectToken ( TokenKind . COLON ) , this . parseTypeReference ( ) ) ,
364
366
defaultValue : this . expectOptionalToken ( TokenKind . EQUALS )
@@ -506,13 +508,15 @@ export class Parser {
506
508
*/
507
509
parseFragmentDefinition ( ) : FragmentDefinitionNode {
508
510
const start = this . _lexer . token ;
511
+ const description = this . parseDescription ( ) ;
509
512
this . expectKeyword ( 'fragment' ) ;
510
513
// Legacy support for defining variables within fragments changes
511
514
// the grammar of FragmentDefinition:
512
515
// - fragment FragmentName VariableDefinitions? on TypeCondition Directives? SelectionSet
513
516
if ( this . _options ?. allowLegacyFragmentVariables === true ) {
514
517
return this . node < FragmentDefinitionNode > ( start , {
515
518
kind : Kind . FRAGMENT_DEFINITION ,
519
+ description,
516
520
name : this . parseFragmentName ( ) ,
517
521
variableDefinitions : this . parseVariableDefinitions ( ) ,
518
522
typeCondition : ( this . expectKeyword ( 'on' ) , this . parseNamedType ( ) ) ,
@@ -522,6 +526,7 @@ export class Parser {
522
526
}
523
527
return this . node < FragmentDefinitionNode > ( start , {
524
528
kind : Kind . FRAGMENT_DEFINITION ,
529
+ description,
525
530
name : this . parseFragmentName ( ) ,
526
531
typeCondition : ( this . expectKeyword ( 'on' ) , this . parseNamedType ( ) ) ,
527
532
directives : this . parseDirectives ( false ) ,
0 commit comments