File tree Expand file tree Collapse file tree 2 files changed +73
-2
lines changed Expand file tree Collapse file tree 2 files changed +73
-2
lines changed Original file line number Diff line number Diff line change @@ -446,6 +446,68 @@ type Hello {
446
446
. to . throw ( 'Must provide schema definition with query type.' ) ;
447
447
} ) ;
448
448
449
+ it ( 'Allows only a single query type' , ( ) => {
450
+ const body = `
451
+ schema {
452
+ query: Hello
453
+ query: Yellow
454
+ }
455
+
456
+ type Hello {
457
+ bar: Bar
458
+ }
459
+
460
+ type Yellow {
461
+ isColor: Boolean
462
+ }
463
+ ` ;
464
+ const doc = parse ( body ) ;
465
+ expect ( ( ) => buildASTSchema ( doc ) )
466
+ . to . throw ( 'Must provide only one query type in schema.' ) ;
467
+ } ) ;
468
+
469
+ it ( 'Allows only a single mutation type' , ( ) => {
470
+ const body = `
471
+ schema {
472
+ query: Hello
473
+ mutation: Hello
474
+ mutation: Yellow
475
+ }
476
+
477
+ type Hello {
478
+ bar: Bar
479
+ }
480
+
481
+ type Yellow {
482
+ isColor: Boolean
483
+ }
484
+ ` ;
485
+ const doc = parse ( body ) ;
486
+ expect ( ( ) => buildASTSchema ( doc ) )
487
+ . to . throw ( 'Must provide only one mutation type in schema.' ) ;
488
+ } ) ;
489
+
490
+ it ( 'Allows only a single subscription type' , ( ) => {
491
+ const body = `
492
+ schema {
493
+ query: Hello
494
+ subscription: Hello
495
+ subscription: Yellow
496
+ }
497
+
498
+ type Hello {
499
+ bar: Bar
500
+ }
501
+
502
+ type Yellow {
503
+ isColor: Boolean
504
+ }
505
+ ` ;
506
+ const doc = parse ( body ) ;
507
+ expect ( ( ) => buildASTSchema ( doc ) )
508
+ . to . throw ( 'Must provide only one subscription type in schema.' ) ;
509
+ } ) ;
510
+
449
511
it ( 'Unknown type referenced' , ( ) => {
450
512
const body = `
451
513
schema {
Original file line number Diff line number Diff line change @@ -111,8 +111,8 @@ function getNamedTypeAST(typeAST: Type): NamedType {
111
111
}
112
112
113
113
/**
114
- * This takes the ast of a schema document produced by parseSchema in
115
- * src/language/schema/ parser.js.
114
+ * This takes the ast of a schema document produced by the parse function in
115
+ * src/language/parser.js.
116
116
*
117
117
* Given that AST it constructs a GraphQLSchema. As constructed
118
118
* they are not particularly useful for non-introspection queries
@@ -160,10 +160,19 @@ export function buildASTSchema(ast: Document): GraphQLSchema {
160
160
schemaDef . operationTypes . forEach ( operationType => {
161
161
const typeName = operationType . type . name . value ;
162
162
if ( operationType . operation === 'query' ) {
163
+ if ( queryTypeName ) {
164
+ throw new Error ( 'Must provide only one query type in schema.' ) ;
165
+ }
163
166
queryTypeName = typeName ;
164
167
} else if ( operationType . operation === 'mutation' ) {
168
+ if ( mutationTypeName ) {
169
+ throw new Error ( 'Must provide only one mutation type in schema.' ) ;
170
+ }
165
171
mutationTypeName = typeName ;
166
172
} else if ( operationType . operation === 'subscription' ) {
173
+ if ( subscriptionTypeName ) {
174
+ throw new Error ( 'Must provide only one subscription type in schema.' ) ;
175
+ }
167
176
subscriptionTypeName = typeName ;
168
177
}
169
178
} ) ;
You can’t perform that action at this time.
0 commit comments