@@ -19,6 +19,7 @@ import type {
19
19
Document ,
20
20
Definition ,
21
21
OperationDefinition ,
22
+ OperationType ,
22
23
VariableDefinition ,
23
24
SelectionSet ,
24
25
Selection ,
@@ -41,6 +42,9 @@ import type {
41
42
42
43
TypeSystemDefinition ,
43
44
45
+ SchemaDefinition ,
46
+ OperationTypeDefinition ,
47
+
44
48
ScalarTypeDefinition ,
45
49
ObjectTypeDefinition ,
46
50
FieldDefinition ,
@@ -86,6 +90,9 @@ import {
86
90
LIST_TYPE ,
87
91
NON_NULL_TYPE ,
88
92
93
+ SCHEMA_DEFINITION ,
94
+ OPERATION_TYPE_DEFINITION ,
95
+
89
96
SCALAR_TYPE_DEFINITION ,
90
97
OBJECT_TYPE_DEFINITION ,
91
98
FIELD_DEFINITION ,
@@ -203,6 +210,7 @@ function parseDefinition(parser: Parser): Definition {
203
210
case 'fragment' : return parseFragmentDefinition ( parser ) ;
204
211
205
212
// Note: the Type System IDL is an experimental non-spec addition.
213
+ case 'schema' :
206
214
case 'scalar' :
207
215
case 'type' :
208
216
case 'interface' :
@@ -224,8 +232,6 @@ function parseDefinition(parser: Parser): Definition {
224
232
* OperationDefinition :
225
233
* - SelectionSet
226
234
* - OperationType Name? VariableDefinitions? Directives? SelectionSet
227
- *
228
- * OperationType : one of query mutation
229
235
*/
230
236
function parseOperationDefinition ( parser : Parser ) : OperationDefinition {
231
237
const start = parser . token . start ;
@@ -240,12 +246,7 @@ function parseOperationDefinition(parser: Parser): OperationDefinition {
240
246
loc : loc ( parser , start )
241
247
} ;
242
248
}
243
- const operationToken = expect ( parser , TokenKind . NAME ) ;
244
- const operation =
245
- operationToken . value === 'mutation' ? 'mutation' :
246
- operationToken . value === 'subscription' ? 'subscription' :
247
- operationToken . value === 'query' ? 'query' :
248
- ( ( ) => { throw unexpected ( parser , operationToken ) ; } ) ( ) ;
249
+ const operation = parseOperationType ( parser ) ;
249
250
let name ;
250
251
if ( peek ( parser , TokenKind . NAME ) ) {
251
252
name = parseName ( parser ) ;
@@ -261,6 +262,21 @@ function parseOperationDefinition(parser: Parser): OperationDefinition {
261
262
} ;
262
263
}
263
264
265
+ /**
266
+ * OperationType : one of query mutation subscription
267
+ */
268
+ function parseOperationType ( parser : Parser ) : OperationType {
269
+ const operationToken = expect ( parser , TokenKind . NAME ) ;
270
+ switch ( operationToken . value ) {
271
+ case 'query' : return 'query' ;
272
+ case 'mutation' : return 'mutation' ;
273
+ // Note: subscription is an experimental non-spec addition.
274
+ case 'subscription' : return 'subscription' ;
275
+ }
276
+
277
+ throw unexpected ( parser , operationToken ) ;
278
+ }
279
+
264
280
/**
265
281
* VariableDefinitions : ( VariableDefinition+ )
266
282
*/
@@ -666,6 +682,7 @@ export function parseNamedType(parser: Parser): NamedType {
666
682
function parseTypeSystemDefinition ( parser : Parser ) : TypeSystemDefinition {
667
683
if ( peek ( parser , TokenKind . NAME ) ) {
668
684
switch ( parser . token . value ) {
685
+ case 'schema' : return parseSchemaDefinition ( parser ) ;
669
686
case 'scalar' : return parseScalarTypeDefinition ( parser ) ;
670
687
case 'type' : return parseObjectTypeDefinition ( parser ) ;
671
688
case 'interface' : return parseInterfaceTypeDefinition ( parser ) ;
@@ -680,6 +697,40 @@ function parseTypeSystemDefinition(parser: Parser): TypeSystemDefinition {
680
697
throw unexpected ( parser ) ;
681
698
}
682
699
700
+ /**
701
+ * SchemaDefinition : schema { OperationTypeDefinition+ }
702
+ *
703
+ * OperationTypeDefinition : OperationType : NamedType
704
+ */
705
+ function parseSchemaDefinition ( parser : Parser ) : SchemaDefinition {
706
+ const start = parser . token . start ;
707
+ expectKeyword ( parser , 'schema' ) ;
708
+ const operationTypes = many (
709
+ parser ,
710
+ TokenKind . BRACE_L ,
711
+ parseOperationTypeDefinition ,
712
+ TokenKind . BRACE_R
713
+ ) ;
714
+ return {
715
+ kind : SCHEMA_DEFINITION ,
716
+ operationTypes,
717
+ loc : loc ( parser , start ) ,
718
+ } ;
719
+ }
720
+
721
+ function parseOperationTypeDefinition ( parser : Parser ) : OperationTypeDefinition {
722
+ const start = parser . token . start ;
723
+ const operation = parseOperationType ( parser ) ;
724
+ expect ( parser , TokenKind . COLON ) ;
725
+ const type = parseNamedType ( parser ) ;
726
+ return {
727
+ kind : OPERATION_TYPE_DEFINITION ,
728
+ operation,
729
+ type,
730
+ loc : loc ( parser , start ) ,
731
+ } ;
732
+ }
733
+
683
734
/**
684
735
* ScalarTypeDefinition : scalar Name
685
736
*/
0 commit comments