@@ -34,12 +34,7 @@ import { GraphQLDirective } from '../type/directives';
34
34
import { Kind } from '../language/kinds' ;
35
35
36
36
import type { GraphQLType , GraphQLNamedType } from '../type/definition' ;
37
-
38
- import type {
39
- DocumentNode ,
40
- DirectiveDefinitionNode ,
41
- TypeExtensionNode ,
42
- } from '../language/ast' ;
37
+ import type { DocumentNode , DirectiveDefinitionNode } from '../language/ast' ;
43
38
44
39
type Options = { |
45
40
...GraphQLSchemaValidationOptions ,
@@ -114,6 +109,7 @@ export function extendSchema(
114
109
typeDefinitionMap [ typeName ] = def ;
115
110
break ;
116
111
case Kind . OBJECT_TYPE_EXTENSION :
112
+ case Kind . INTERFACE_TYPE_EXTENSION :
117
113
// Sanity check that this type extension exists within the
118
114
// schema's existing types.
119
115
const extendedTypeName = def . name . value ;
@@ -125,39 +121,12 @@ export function extendSchema(
125
121
[ def ] ,
126
122
) ;
127
123
}
128
- if ( ! isObjectType ( existingType ) ) {
129
- throw new GraphQLError (
130
- `Cannot extend non-object type "${ extendedTypeName } ".` ,
131
- [ def ] ,
132
- ) ;
133
- }
134
- typeExtensionsMap [ extendedTypeName ] = appendExtensionToTypeExtensions (
135
- def ,
136
- typeExtensionsMap [ extendedTypeName ] ,
137
- ) ;
138
- break ;
139
- case Kind . INTERFACE_TYPE_EXTENSION :
140
- const extendedInterfaceTypeName = def . name . value ;
141
- const existingInterfaceType = schema . getType ( extendedInterfaceTypeName ) ;
142
- if ( ! existingInterfaceType ) {
143
- throw new GraphQLError (
144
- `Cannot extend interface "${ extendedInterfaceTypeName } " because ` +
145
- 'it does not exist in the existing schema.' ,
146
- [ def ] ,
147
- ) ;
148
- }
149
- if ( ! isInterfaceType ( existingInterfaceType ) ) {
150
- throw new GraphQLError (
151
- `Cannot extend non-interface type "${ extendedInterfaceTypeName } ".` ,
152
- [ def ] ,
153
- ) ;
154
- }
155
- typeExtensionsMap [
156
- extendedInterfaceTypeName
157
- ] = appendExtensionToTypeExtensions (
158
- def ,
159
- typeExtensionsMap [ extendedInterfaceTypeName ] ,
160
- ) ;
124
+ checkExtensionNode ( existingType , def ) ;
125
+
126
+ const existingTypeExtensions = typeExtensionsMap [ extendedTypeName ] ;
127
+ typeExtensionsMap [ extendedTypeName ] = existingTypeExtensions
128
+ ? existingTypeExtensions . concat ( [ def ] )
129
+ : [ def ] ;
161
130
break ;
162
131
case Kind . DIRECTIVE_DEFINITION :
163
132
const directiveName = def . name . value ;
@@ -212,9 +181,6 @@ export function extendSchema(
212
181
const extendTypeCache = Object . create ( null ) ;
213
182
214
183
// Get the root Query, Mutation, and Subscription object types.
215
- // Note: While this could make early assertions to get the correctly
216
- // typed values below, that would throw immediately while type system
217
- // validation with validateSchema() will produce more actionable results.
218
184
const existingQueryType = schema . getQueryType ( ) ;
219
185
const queryType = existingQueryType
220
186
? getExtendedType ( existingQueryType )
@@ -235,7 +201,7 @@ export function extendSchema(
235
201
// that any type not directly referenced by a field will get created.
236
202
...objectValues ( schema . getTypeMap ( ) ) . map ( type => getExtendedType ( type ) ) ,
237
203
// Do the same with new types.
238
- ...objectValues ( typeDefinitionMap ) . map ( type => astBuilder . buildType ( type ) ) ,
204
+ ...astBuilder . buildTypes ( objectValues ( typeDefinitionMap ) ) ,
239
205
] ;
240
206
241
207
// Support both original legacy names and extended legacy names.
@@ -257,17 +223,6 @@ export function extendSchema(
257
223
allowedLegacyNames,
258
224
} ) ;
259
225
260
- function appendExtensionToTypeExtensions (
261
- extension : TypeExtensionNode ,
262
- existingTypeExtensions : ?Array < TypeExtensionNode > ,
263
- ) : Array < TypeExtensionNode > {
264
- if ( ! existingTypeExtensions ) {
265
- return [ extension ] ;
266
- }
267
- existingTypeExtensions . push ( extension ) ;
268
- return existingTypeExtensions;
269
- }
270
-
271
226
// Below are functions used for producing this schema that have closed over
272
227
// this scope and have access to the schema, cache, and newly defined types.
273
228
@@ -420,3 +375,24 @@ export function extendSchema(
420
375
return getExtendedType ( typeDef ) ;
421
376
}
422
377
}
378
+
379
+ function checkExtensionNode ( type , node ) {
380
+ switch ( node . kind ) {
381
+ case Kind . OBJECT_TYPE_EXTENSION :
382
+ if ( ! isObjectType ( type ) ) {
383
+ throw new GraphQLError (
384
+ `Cannot extend non-object type "${ type . name } ".` ,
385
+ [ node ] ,
386
+ ) ;
387
+ }
388
+ break ;
389
+ case Kind . INTERFACE_TYPE_EXTENSION :
390
+ if ( ! isInterfaceType ( type ) ) {
391
+ throw new GraphQLError (
392
+ `Cannot extend non-interface type "${ type . name } ".` ,
393
+ [ node ] ,
394
+ ) ;
395
+ }
396
+ break ;
397
+ }
398
+ }
0 commit comments