Skip to content

Commit

Permalink
extendSchema: Use consistent naming + inline 'getExtendedType' functi…
Browse files Browse the repository at this point in the history
…on (#1327)
  • Loading branch information
IvanGoncharov authored and mjmahone committed May 14, 2018
1 parent 2592e12 commit 0276d68
Showing 1 changed file with 28 additions and 33 deletions.
61 changes: 28 additions & 33 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export function extendSchema(
const typeName = typeRef.name.value;
const existingType = schema.getType(typeName);
if (existingType) {
return getExtendedType(existingType);
return extendNamedType(existingType);
}

throw new GraphQLError(
Expand All @@ -200,9 +200,9 @@ export function extendSchema(

// Get the extended root operation types.
const operationTypes = {
query: getExtendedMaybeType(schema.getQueryType()),
mutation: getExtendedMaybeType(schema.getMutationType()),
subscription: getExtendedMaybeType(schema.getSubscriptionType()),
query: extendMaybeNamedType(schema.getQueryType()),
mutation: extendMaybeNamedType(schema.getMutationType()),
subscription: extendMaybeNamedType(schema.getSubscriptionType()),
};

// Then, incorporate all schema extensions.
Expand Down Expand Up @@ -231,7 +231,7 @@ export function extendSchema(
const types = [
// Iterate through all types, getting the type definition for each, ensuring
// that any type not directly referenced by a field will get created.
...objectValues(schema.getTypeMap()).map(type => getExtendedType(type)),
...objectValues(schema.getTypeMap()).map(type => extendNamedType(type)),
// Do the same with new types.
...astBuilder.buildTypes(objectValues(typeDefinitionMap)),
];
Expand Down Expand Up @@ -268,34 +268,29 @@ export function extendSchema(
);
}

function getExtendedMaybeType<T: GraphQLNamedType>(type: ?T): ?T {
return type ? getExtendedType(type) : null;
function extendMaybeNamedType<T: GraphQLNamedType>(type: ?T): ?T {
return type ? extendNamedType(type) : null;
}

function getExtendedType<T: GraphQLNamedType>(type: T): T {
if (!extendTypeCache[type.name]) {
extendTypeCache[type.name] = extendType(type);
}
return (extendTypeCache[type.name]: any);
}

// To be called at most once per type. Only getExtendedType should call this.
function extendType(type) {
function extendNamedType<T: GraphQLNamedType>(type: T): T {
if (isIntrospectionType(type)) {
// Introspection types are not extended.
return type;
}
if (isObjectType(type)) {
return extendObjectType(type);
}
if (isInterfaceType(type)) {
return extendInterfaceType(type);
}
if (isUnionType(type)) {
return extendUnionType(type);

if (!extendTypeCache[type.name]) {
if (isObjectType(type)) {
extendTypeCache[type.name] = extendObjectType(type);
} else if (isInterfaceType(type)) {
extendTypeCache[type.name] = extendInterfaceType(type);
} else if (isUnionType(type)) {
extendTypeCache[type.name] = extendUnionType(type);
} else {
// This type is not yet extendable.
extendTypeCache[type.name] = type;
}
}
// This type is not yet extendable.
return type;
return (extendTypeCache[type.name]: any);
}

function extendObjectType(type: GraphQLObjectType): GraphQLObjectType {
Expand Down Expand Up @@ -339,7 +334,7 @@ export function extendSchema(
return new GraphQLUnionType({
name: type.name,
description: type.description,
types: type.getTypes().map(getExtendedType),
types: type.getTypes().map(extendNamedType),
astNode: type.astNode,
resolveType: type.resolveType,
});
Expand All @@ -348,7 +343,7 @@ export function extendSchema(
function extendImplementedInterfaces(
type: GraphQLObjectType,
): Array<GraphQLInterfaceType> {
const interfaces = type.getInterfaces().map(getExtendedType);
const interfaces = type.getInterfaces().map(extendNamedType);

// If there are any extensions to the interfaces, apply those here.
const extensions = typeExtensionsMap[type.name];
Expand All @@ -374,7 +369,7 @@ export function extendSchema(
newFieldMap[fieldName] = {
description: field.description,
deprecationReason: field.deprecationReason,
type: extendFieldType(field.type),
type: extendType(field.type),
args: keyMap(field.args, arg => arg.name),
astNode: field.astNode,
resolve: field.resolve,
Expand Down Expand Up @@ -402,14 +397,14 @@ export function extendSchema(
return newFieldMap;
}

function extendFieldType<T: GraphQLType>(typeDef: T): T {
function extendType<T: GraphQLType>(typeDef: T): T {
if (isListType(typeDef)) {
return (GraphQLList(extendFieldType(typeDef.ofType)): any);
return (GraphQLList(extendType(typeDef.ofType)): any);
}
if (isNonNullType(typeDef)) {
return (GraphQLNonNull(extendFieldType(typeDef.ofType)): any);
return (GraphQLNonNull(extendType(typeDef.ofType)): any);
}
return getExtendedType(typeDef);
return extendNamedType(typeDef);
}
}

Expand Down

0 comments on commit 0276d68

Please sign in to comment.