@@ -53,6 +53,7 @@ import type {
53
53
DocumentNode ,
54
54
DirectiveDefinitionNode ,
55
55
SchemaExtensionNode ,
56
+ SchemaDefinitionNode ,
56
57
} from '../language/ast' ;
57
58
58
59
type Options = { |
@@ -106,18 +107,28 @@ export function extendSchema(
106
107
// have the same name. For example, a type named "skip".
107
108
const directiveDefinitions : Array < DirectiveDefinitionNode > = [ ] ;
108
109
110
+ let schemaDef : ?SchemaDefinitionNode ;
109
111
// Schema extensions are collected which may add additional operation types.
110
112
const schemaExtensions : Array < SchemaExtensionNode > = [ ] ;
111
113
112
114
for ( let i = 0 ; i < documentAST . definitions . length ; i ++ ) {
113
115
const def = documentAST . definitions [ i ] ;
114
116
switch ( def . kind ) {
115
117
case Kind . SCHEMA_DEFINITION :
116
- // Sanity check that a schema extension is not defining a new schema
117
- throw new GraphQLError (
118
- 'Cannot define a new schema within a schema extension.' ,
119
- [ def ] ,
120
- ) ;
118
+ // Sanity check that a schema extension is not overriding the schema
119
+ if (
120
+ schema . astNode ||
121
+ schema . getQueryType ( ) ||
122
+ schema . getMutationType ( ) ||
123
+ schema . getSubscriptionType ( )
124
+ ) {
125
+ throw new GraphQLError (
126
+ 'Cannot define a new schema within a schema extension.' ,
127
+ [ def ] ,
128
+ ) ;
129
+ }
130
+ schemaDef = def ;
131
+ break ;
121
132
case Kind . SCHEMA_EXTENSION :
122
133
schemaExtensions . push ( def ) ;
123
134
break ;
@@ -216,9 +227,9 @@ export function extendSchema(
216
227
subscription : extendMaybeNamedType ( schema . getSubscriptionType ( ) ) ,
217
228
} ;
218
229
219
- // Then, incorporate all schema extensions.
220
- for ( const schemaExtension of schemaExtensions ) {
221
- if ( schemaExtension . operationTypes ) {
230
+ // Then, incorporate schema definition and all schema extensions.
231
+ for ( const schemaExtension of [ schemaDef , ... schemaExtensions ] ) {
232
+ if ( schemaExtension && schemaExtension . operationTypes ) {
222
233
for ( const operationType of schemaExtension . operationTypes ) {
223
234
const operation = operationType . operation ;
224
235
if ( operationTypes [ operation ] ) {
0 commit comments