@@ -185,7 +185,7 @@ export function extendSchema(
185185 const typeName = typeRef . name . value ;
186186 const existingType = schema . getType ( typeName ) ;
187187 if ( existingType ) {
188- return getExtendedType ( existingType ) ;
188+ return extendNamedType ( existingType ) ;
189189 }
190190
191191 throw new GraphQLError (
@@ -200,9 +200,9 @@ export function extendSchema(
200200
201201 // Get the extended root operation types.
202202 const operationTypes = {
203- query : getExtendedMaybeType ( schema . getQueryType ( ) ) ,
204- mutation : getExtendedMaybeType ( schema . getMutationType ( ) ) ,
205- subscription : getExtendedMaybeType ( schema . getSubscriptionType ( ) ) ,
203+ query : extendMaybeNamedType ( schema . getQueryType ( ) ) ,
204+ mutation : extendMaybeNamedType ( schema . getMutationType ( ) ) ,
205+ subscription : extendMaybeNamedType ( schema . getSubscriptionType ( ) ) ,
206206 } ;
207207
208208 // Then, incorporate all schema extensions.
@@ -231,7 +231,7 @@ export function extendSchema(
231231 const types = [
232232 // Iterate through all types, getting the type definition for each, ensuring
233233 // that any type not directly referenced by a field will get created.
234- ...objectValues ( schema . getTypeMap ( ) ) . map ( type => getExtendedType ( type ) ) ,
234+ ...objectValues ( schema . getTypeMap ( ) ) . map ( type => extendNamedType ( type ) ) ,
235235 // Do the same with new types.
236236 ...astBuilder . buildTypes ( objectValues ( typeDefinitionMap ) ) ,
237237 ] ;
@@ -268,34 +268,29 @@ export function extendSchema(
268268 ) ;
269269 }
270270
271- function getExtendedMaybeType < T : GraphQLNamedType > (type: ?T): ?T {
272- return type ? getExtendedType ( type ) : null ;
271+ function extendMaybeNamedType < T : GraphQLNamedType > (type: ?T): ?T {
272+ return type ? extendNamedType ( type ) : null ;
273273 }
274274
275- function getExtendedType< T : GraphQLNamedType > (type: T): T {
276- if ( ! extendTypeCache [ type . name ] ) {
277- extendTypeCache [ type . name ] = extendType ( type ) ;
278- }
279- return (extendTypeCache[type.name]: any);
280- }
281-
282- // To be called at most once per type. Only getExtendedType should call this.
283- function extendType ( type ) {
275+ function extendNamedType< T : GraphQLNamedType > (type: T): T {
284276 if ( isIntrospectionType ( type ) ) {
285277 // Introspection types are not extended.
286278 return type ;
287279 }
288- if (isObjectType(type)) {
289- return extendObjectType ( type ) ;
290- }
291- if (isInterfaceType(type)) {
292- return extendInterfaceType ( type ) ;
293- }
294- if (isUnionType(type)) {
295- return extendUnionType ( type ) ;
280+
281+ if (!extendTypeCache[type.name]) {
282+ if ( isObjectType ( type ) ) {
283+ extendTypeCache [ type . name ] = extendObjectType ( type ) ;
284+ } else if (isInterfaceType(type)) {
285+ extendTypeCache [ type . name ] = extendInterfaceType ( type ) ;
286+ } else if (isUnionType(type)) {
287+ extendTypeCache [ type . name ] = extendUnionType ( type ) ;
288+ } else {
289+ // This type is not yet extendable.
290+ extendTypeCache [ type . name ] = type ;
291+ }
296292 }
297- // This type is not yet extendable.
298- return type;
293+ return ( extendTypeCache [ type . name ] : any ) ;
299294 }
300295
301296 function extendObjectType ( type : GraphQLObjectType ) : GraphQLObjectType {
@@ -339,7 +334,7 @@ export function extendSchema(
339334 return new GraphQLUnionType ( {
340335 name : type . name ,
341336 description : type . description ,
342- types : type . getTypes ( ) . map ( getExtendedType ) ,
337+ types : type . getTypes ( ) . map ( extendNamedType ) ,
343338 astNode : type . astNode ,
344339 resolveType : type . resolveType ,
345340 } ) ;
@@ -348,7 +343,7 @@ export function extendSchema(
348343 function extendImplementedInterfaces(
349344 type: GraphQLObjectType,
350345 ): Array< GraphQLInterfaceType > {
351- const interfaces = type . getInterfaces ( ) . map ( getExtendedType ) ;
346+ const interfaces = type . getInterfaces ( ) . map ( extendNamedType ) ;
352347
353348 // If there are any extensions to the interfaces, apply those here.
354349 const extensions = typeExtensionsMap [ type . name ] ;
@@ -374,7 +369,7 @@ export function extendSchema(
374369 newFieldMap [ fieldName ] = {
375370 description : field . description ,
376371 deprecationReason : field . deprecationReason ,
377- type : extendFieldType ( field . type ) ,
372+ type : extendType ( field . type ) ,
378373 args : keyMap ( field . args , arg => arg . name ) ,
379374 astNode : field . astNode ,
380375 resolve : field . resolve ,
@@ -402,14 +397,14 @@ export function extendSchema(
402397 return newFieldMap;
403398 }
404399
405- function extendFieldType < T : GraphQLType > (typeDef: T): T {
400+ function extendType < T : GraphQLType > (typeDef: T): T {
406401 if ( isListType ( typeDef ) ) {
407- return ( GraphQLList ( extendFieldType ( typeDef . ofType ) ) : any ) ;
402+ return ( GraphQLList ( extendType ( typeDef . ofType ) ) : any ) ;
408403 }
409404 if (isNonNullType(typeDef)) {
410- return ( GraphQLNonNull ( extendFieldType ( typeDef . ofType ) ) : any ) ;
405+ return ( GraphQLNonNull ( extendType ( typeDef . ofType ) ) : any ) ;
411406 }
412- return getExtendedType (typeDef);
407+ return extendNamedType (typeDef);
413408 }
414409}
415410
0 commit comments