@@ -41,7 +41,6 @@ import type {
41
41
NamedTypeNode ,
42
42
ListTypeNode ,
43
43
NonNullTypeNode ,
44
- TypeSystemDefinitionNode ,
45
44
SchemaDefinitionNode ,
46
45
OperationTypeDefinitionNode ,
47
46
ScalarTypeDefinitionNode ,
@@ -226,35 +225,72 @@ export class Parser {
226
225
* ExecutableDefinition :
227
226
* - OperationDefinition
228
227
* - FragmentDefinition
228
+ *
229
+ * TypeSystemDefinition :
230
+ * - SchemaDefinition
231
+ * - TypeDefinition
232
+ * - DirectiveDefinition
233
+ *
234
+ * TypeDefinition :
235
+ * - ScalarTypeDefinition
236
+ * - ObjectTypeDefinition
237
+ * - InterfaceTypeDefinition
238
+ * - UnionTypeDefinition
239
+ * - EnumTypeDefinition
240
+ * - InputObjectTypeDefinition
229
241
*/
230
242
parseDefinition ( ) : DefinitionNode {
231
- if ( this . peek ( TokenKind . NAME ) ) {
232
- switch ( this . _lexer . token . value ) {
233
- case 'query' :
234
- case 'mutation' :
235
- case 'subscription' :
236
- return this . parseOperationDefinition ( ) ;
237
- case 'fragment' :
238
- return this . parseFragmentDefinition ( ) ;
243
+ if ( this . peek ( TokenKind . BRACE_L ) ) {
244
+ return this . parseOperationDefinition ( ) ;
245
+ }
246
+
247
+ // Many definitions begin with a description and require a lookahead.
248
+ const hasDescription = this . peekDescription ( ) ;
249
+ const keywordToken = hasDescription
250
+ ? this . _lexer . lookahead ( )
251
+ : this . _lexer . token ;
252
+
253
+ if ( keywordToken . kind === TokenKind . NAME ) {
254
+ switch ( keywordToken . value ) {
239
255
case 'schema' :
256
+ return this . parseSchemaDefinition ( ) ;
240
257
case 'scalar' :
258
+ return this . parseScalarTypeDefinition ( ) ;
241
259
case 'type' :
260
+ return this . parseObjectTypeDefinition ( ) ;
242
261
case 'interface' :
262
+ return this . parseInterfaceTypeDefinition ( ) ;
243
263
case 'union' :
264
+ return this . parseUnionTypeDefinition ( ) ;
244
265
case 'enum' :
266
+ return this . parseEnumTypeDefinition ( ) ;
245
267
case 'input' :
268
+ return this . parseInputObjectTypeDefinition ( ) ;
246
269
case 'directive' :
247
- return this . parseTypeSystemDefinition ( ) ;
270
+ return this . parseDirectiveDefinition ( ) ;
271
+ }
272
+
273
+ if ( hasDescription ) {
274
+ throw syntaxError (
275
+ this . _lexer . source ,
276
+ this . _lexer . token . start ,
277
+ 'Unexpected description, descriptions are supported only on type definitions.' ,
278
+ ) ;
279
+ }
280
+
281
+ switch ( keywordToken . value ) {
282
+ case 'query' :
283
+ case 'mutation' :
284
+ case 'subscription' :
285
+ return this . parseOperationDefinition ( ) ;
286
+ case 'fragment' :
287
+ return this . parseFragmentDefinition ( ) ;
248
288
case 'extend' :
249
289
return this . parseTypeSystemExtension ( ) ;
250
290
}
251
- } else if ( this . peek ( TokenKind . BRACE_L ) ) {
252
- return this . parseOperationDefinition ( ) ;
253
- } else if ( this . peekDescription ( ) ) {
254
- return this . parseTypeSystemDefinition ( ) ;
255
291
}
256
292
257
- throw this . unexpected ( ) ;
293
+ throw this . unexpected ( keywordToken ) ;
258
294
}
259
295
260
296
// Implements the parsing rules in the Operations section.
@@ -731,50 +767,6 @@ export class Parser {
731
767
732
768
// Implements the parsing rules in the Type Definition section.
733
769
734
- /**
735
- * TypeSystemDefinition :
736
- * - SchemaDefinition
737
- * - TypeDefinition
738
- * - DirectiveDefinition
739
- *
740
- * TypeDefinition :
741
- * - ScalarTypeDefinition
742
- * - ObjectTypeDefinition
743
- * - InterfaceTypeDefinition
744
- * - UnionTypeDefinition
745
- * - EnumTypeDefinition
746
- * - InputObjectTypeDefinition
747
- */
748
- parseTypeSystemDefinition ( ) : TypeSystemDefinitionNode {
749
- // Many definitions begin with a description and require a lookahead.
750
- const keywordToken = this . peekDescription ( )
751
- ? this . _lexer . lookahead ( )
752
- : this . _lexer . token ;
753
-
754
- if ( keywordToken . kind === TokenKind . NAME ) {
755
- switch ( keywordToken . value ) {
756
- case 'schema' :
757
- return this . parseSchemaDefinition ( ) ;
758
- case 'scalar' :
759
- return this . parseScalarTypeDefinition ( ) ;
760
- case 'type' :
761
- return this . parseObjectTypeDefinition ( ) ;
762
- case 'interface' :
763
- return this . parseInterfaceTypeDefinition ( ) ;
764
- case 'union' :
765
- return this . parseUnionTypeDefinition ( ) ;
766
- case 'enum' :
767
- return this . parseEnumTypeDefinition ( ) ;
768
- case 'input' :
769
- return this . parseInputObjectTypeDefinition ( ) ;
770
- case 'directive' :
771
- return this . parseDirectiveDefinition ( ) ;
772
- }
773
- }
774
-
775
- throw this . unexpected ( keywordToken ) ;
776
- }
777
-
778
770
peekDescription ( ) : boolean {
779
771
return this . peek ( TokenKind . STRING ) || this . peek ( TokenKind . BLOCK_STRING ) ;
780
772
}
0 commit comments