Skip to content

Commit

Permalink
GraphQLSchema: Remove unneeded recursion during type collection (#2083)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Aug 12, 2019
1 parent 89f9f72 commit cd9fc8e
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/type/schema.js
Expand Up @@ -32,7 +32,7 @@ import {
isInterfaceType,
isUnionType,
isInputObjectType,
isWrappingType,
getNamedType,
} from './definition';

/**
Expand Down Expand Up @@ -324,39 +324,39 @@ function typeMapReducer(map: TypeMap, type: ?GraphQLType): TypeMap {
if (!type) {
return map;
}
if (isWrappingType(type)) {
return typeMapReducer(map, type.ofType);
}
if (map[type.name]) {
if (map[type.name] !== type) {

const namedType = getNamedType(type);
const seenType = map[namedType.name];
if (seenType) {
if (seenType !== namedType) {
throw new Error(
`Schema must contain uniquely named types but contains multiple types named "${type.name}".`,
`Schema must contain uniquely named types but contains multiple types named "${namedType.name}".`,
);
}
return map;
}
map[type.name] = type;
map[namedType.name] = namedType;

let reducedMap = map;

if (isUnionType(type)) {
reducedMap = type.getTypes().reduce(typeMapReducer, reducedMap);
if (isUnionType(namedType)) {
reducedMap = namedType.getTypes().reduce(typeMapReducer, reducedMap);
}

if (isObjectType(type)) {
reducedMap = type.getInterfaces().reduce(typeMapReducer, reducedMap);
if (isObjectType(namedType)) {
reducedMap = namedType.getInterfaces().reduce(typeMapReducer, reducedMap);
}

if (isObjectType(type) || isInterfaceType(type)) {
for (const field of objectValues(type.getFields())) {
if (isObjectType(namedType) || isInterfaceType(namedType)) {
for (const field of objectValues(namedType.getFields())) {
const fieldArgTypes = field.args.map(arg => arg.type);
reducedMap = fieldArgTypes.reduce(typeMapReducer, reducedMap);
reducedMap = typeMapReducer(reducedMap, field.type);
}
}

if (isInputObjectType(type)) {
for (const field of objectValues(type.getFields())) {
if (isInputObjectType(namedType)) {
for (const field of objectValues(namedType.getFields())) {
reducedMap = typeMapReducer(reducedMap, field.type);
}
}
Expand Down

0 comments on commit cd9fc8e

Please sign in to comment.