@@ -203,7 +203,7 @@ export function extendSchema(
203
203
const typeName = typeRef . name . value ;
204
204
const existingType = schema . getType ( typeName ) ;
205
205
if ( existingType ) {
206
- return extendType ( existingType ) ;
206
+ return getExtendedType ( existingType ) ;
207
207
}
208
208
209
209
throw new GraphQLError (
@@ -221,22 +221,24 @@ export function extendSchema(
221
221
// typed values below, that would throw immediately while type system
222
222
// validation with validateSchema() will produce more actionable results.
223
223
const existingQueryType = schema . getQueryType ( ) ;
224
- const queryType = existingQueryType ? extendType ( existingQueryType ) : null ;
224
+ const queryType = existingQueryType
225
+ ? getExtendedType ( existingQueryType )
226
+ : null ;
225
227
226
228
const existingMutationType = schema . getMutationType ( ) ;
227
229
const mutationType = existingMutationType
228
- ? extendType ( existingMutationType )
230
+ ? getExtendedType ( existingMutationType )
229
231
: null ;
230
232
231
233
const existingSubscriptionType = schema . getSubscriptionType ( ) ;
232
234
const subscriptionType = existingSubscriptionType
233
- ? extendType ( existingSubscriptionType )
235
+ ? getExtendedType ( existingSubscriptionType )
234
236
: null ;
235
237
236
238
const types = [
237
239
// Iterate through all types, getting the type definition for each, ensuring
238
240
// that any type not directly referenced by a field will get created.
239
- ...objectValues ( schema . getTypeMap ( ) ) . map ( type => extendType ( type ) ) ,
241
+ ...objectValues ( schema . getTypeMap ( ) ) . map ( type => getExtendedType ( type ) ) ,
240
242
// Do the same with new types.
241
243
...objectValues ( typeDefinitionMap ) . map ( type => astBuilder . buildType ( type ) ) ,
242
244
] ;
@@ -276,23 +278,26 @@ export function extendSchema(
276
278
) ;
277
279
}
278
280
279
- function extendType < T : GraphQLNamedType > (type: T): T {
280
- let extendedType = extendTypeCache [ type . name ] ;
281
+ function getExtendedType < T : GraphQLNamedType > (type: T): T {
282
+ if ( ! extendTypeCache [ type . name ] ) {
283
+ extendTypeCache [ type . name ] = extendType ( type ) ;
284
+ }
285
+ return (extendTypeCache[type.name]: any);
286
+ }
281
287
282
- if ( ! extendedType ) {
283
- if ( isIntrospectionType ( type ) ) {
284
- extendedType = type ;
285
- } else if ( isObjectType ( type ) ) {
288
+ // Should be called only once per type so only getExtendedType should call it.
289
+ function extendType < T : GraphQLNamedType > (type: T): T {
290
+ let extendedType = type ;
291
+ if ( ! isIntrospectionType ( type ) ) {
292
+ if ( isObjectType ( type ) ) {
286
293
extendedType = extendObjectType ( type ) ;
287
294
} else if ( isInterfaceType ( type ) ) {
288
295
extendedType = extendInterfaceType ( type ) ;
289
296
} else if ( isUnionType ( type ) ) {
290
297
extendedType = extendUnionType ( type ) ;
291
- } else {
292
- extendedType = type ;
293
298
}
294
- extendTypeCache [ type . name ] = extendedType ;
295
299
}
300
+ // Workaround: Flow should figure out correct type, but it doesn't.
296
301
return (extendedType: any);
297
302
}
298
303
@@ -337,7 +342,7 @@ export function extendSchema(
337
342
return new GraphQLUnionType ( {
338
343
name : type . name ,
339
344
description : type . description ,
340
- types : type . getTypes ( ) . map ( extendType ) ,
345
+ types : type . getTypes ( ) . map ( getExtendedType ) ,
341
346
astNode : type . astNode ,
342
347
resolveType : type . resolveType ,
343
348
} ) ;
@@ -346,7 +351,7 @@ export function extendSchema(
346
351
function extendImplementedInterfaces(
347
352
type: GraphQLObjectType,
348
353
): Array< GraphQLInterfaceType > {
349
- const interfaces = type . getInterfaces ( ) . map ( extendType ) ;
354
+ const interfaces = type . getInterfaces ( ) . map ( getExtendedType ) ;
350
355
351
356
// If there are any extensions to the interfaces, apply those here.
352
357
const extensions = typeExtensionsMap [ type . name ] ;
@@ -407,6 +412,6 @@ export function extendSchema(
407
412
if (isNonNullType(typeDef)) {
408
413
return ( GraphQLNonNull ( extendFieldType ( typeDef . ofType ) ) : any ) ;
409
414
}
410
- return extendType (typeDef);
415
+ return getExtendedType (typeDef);
411
416
}
412
417
}
0 commit comments