From 0276d685b51262686c841763ba0b6e71103f64f3 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Mon, 14 May 2018 20:20:33 +0300 Subject: [PATCH] extendSchema: Use consistent naming + inline 'getExtendedType' function (#1327) --- src/utilities/extendSchema.js | 61 ++++++++++++++++------------------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/utilities/extendSchema.js b/src/utilities/extendSchema.js index d56bacf2dc..46344c5e51 100644 --- a/src/utilities/extendSchema.js +++ b/src/utilities/extendSchema.js @@ -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( @@ -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. @@ -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)), ]; @@ -268,34 +268,29 @@ export function extendSchema( ); } - function getExtendedMaybeType(type: ?T): ?T { - return type ? getExtendedType(type) : null; + function extendMaybeNamedType(type: ?T): ?T { + return type ? extendNamedType(type) : null; } - function getExtendedType(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(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 { @@ -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, }); @@ -348,7 +343,7 @@ export function extendSchema( function extendImplementedInterfaces( type: GraphQLObjectType, ): Array { - 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]; @@ -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, @@ -402,14 +397,14 @@ export function extendSchema( return newFieldMap; } - function extendFieldType(typeDef: T): T { + function extendType(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); } }