Skip to content

Commit b5d4373

Browse files
committed
extendSchema: Use consistent naming + inline 'getExtendedType' func
1 parent ae5b163 commit b5d4373

File tree

1 file changed

+26
-31
lines changed

1 file changed

+26
-31
lines changed

src/utilities/extendSchema.js

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
@@ -203,12 +203,12 @@ export function extendSchema(
203203
const existingMutationType = schema.getMutationType();
204204
const existingSubscriptionType = schema.getSubscriptionType();
205205
const operationTypes = {
206-
query: existingQueryType ? getExtendedType(existingQueryType) : null,
206+
query: existingQueryType ? extendNamedType(existingQueryType) : null,
207207
mutation: existingMutationType
208-
? getExtendedType(existingMutationType)
208+
? extendNamedType(existingMutationType)
209209
: null,
210210
subscription: existingSubscriptionType
211-
? getExtendedType(existingSubscriptionType)
211+
? extendNamedType(existingSubscriptionType)
212212
: null,
213213
};
214214

@@ -228,7 +228,7 @@ export function extendSchema(
228228
const types = [
229229
// Iterate through all types, getting the type definition for each, ensuring
230230
// that any type not directly referenced by a field will get created.
231-
...objectValues(schema.getTypeMap()).map(type => getExtendedType(type)),
231+
...objectValues(schema.getTypeMap()).map(type => extendNamedType(type)),
232232
// Do the same with new types.
233233
...astBuilder.buildTypes(objectValues(typeDefinitionMap)),
234234
];
@@ -265,30 +265,25 @@ export function extendSchema(
265265
);
266266
}
267267

268-
function getExtendedType<T: GraphQLNamedType>(type: T): T {
269-
if (!extendTypeCache[type.name]) {
270-
extendTypeCache[type.name] = extendType(type);
271-
}
272-
return (extendTypeCache[type.name]: any);
273-
}
274-
275-
// To be called at most once per type. Only getExtendedType should call this.
276-
function extendType(type) {
268+
function extendNamedType<T: GraphQLNamedType>(type: T): T {
277269
if (isIntrospectionType(type)) {
278270
// Introspection types are not extended.
279271
return type;
280272
}
281-
if (isObjectType(type)) {
282-
return extendObjectType(type);
283-
}
284-
if (isInterfaceType(type)) {
285-
return extendInterfaceType(type);
286-
}
287-
if (isUnionType(type)) {
288-
return extendUnionType(type);
273+
274+
if (!extendTypeCache[type.name]) {
275+
if (isObjectType(type)) {
276+
extendTypeCache[type.name] = extendObjectType(type);
277+
} else if (isInterfaceType(type)) {
278+
extendTypeCache[type.name] = extendInterfaceType(type);
279+
} else if (isUnionType(type)) {
280+
extendTypeCache[type.name] = extendUnionType(type);
281+
} else {
282+
// This type is not yet extendable.
283+
extendTypeCache[type.name] = type;
284+
}
289285
}
290-
// This type is not yet extendable.
291-
return type;
286+
return (extendTypeCache[type.name]: any);
292287
}
293288

294289
function extendObjectType(type: GraphQLObjectType): GraphQLObjectType {
@@ -332,7 +327,7 @@ export function extendSchema(
332327
return new GraphQLUnionType({
333328
name: type.name,
334329
description: type.description,
335-
types: type.getTypes().map(getExtendedType),
330+
types: type.getTypes().map(extendNamedType),
336331
astNode: type.astNode,
337332
resolveType: type.resolveType,
338333
});
@@ -341,7 +336,7 @@ export function extendSchema(
341336
function extendImplementedInterfaces(
342337
type: GraphQLObjectType,
343338
): Array<GraphQLInterfaceType> {
344-
const interfaces = type.getInterfaces().map(getExtendedType);
339+
const interfaces = type.getInterfaces().map(extendNamedType);
345340

346341
// If there are any extensions to the interfaces, apply those here.
347342
const extensions = typeExtensionsMap[type.name];
@@ -367,7 +362,7 @@ export function extendSchema(
367362
newFieldMap[fieldName] = {
368363
description: field.description,
369364
deprecationReason: field.deprecationReason,
370-
type: extendFieldType(field.type),
365+
type: extendType(field.type),
371366
args: keyMap(field.args, arg => arg.name),
372367
astNode: field.astNode,
373368
resolve: field.resolve,
@@ -395,14 +390,14 @@ export function extendSchema(
395390
return newFieldMap;
396391
}
397392

398-
function extendFieldType<T: GraphQLType>(typeDef: T): T {
393+
function extendType<T: GraphQLType>(typeDef: T): T {
399394
if (isListType(typeDef)) {
400-
return (GraphQLList(extendFieldType(typeDef.ofType)): any);
395+
return (GraphQLList(extendType(typeDef.ofType)): any);
401396
}
402397
if (isNonNullType(typeDef)) {
403-
return (GraphQLNonNull(extendFieldType(typeDef.ofType)): any);
398+
return (GraphQLNonNull(extendType(typeDef.ofType)): any);
404399
}
405-
return getExtendedType(typeDef);
400+
return extendNamedType(typeDef);
406401
}
407402
}
408403

0 commit comments

Comments
 (0)