Skip to content

Commit 183ff32

Browse files
Remove excessive cache inside buildClientSchema (#1677)
1 parent f1f1730 commit 183ff32

File tree

1 file changed

+15
-26
lines changed

1 file changed

+15
-26
lines changed

src/utilities/buildClientSchema.js

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* @flow strict
88
*/
99

10+
import objectValues from '../polyfills/objectValues';
1011
import inspect from '../jsutils/inspect';
1112
import invariant from '../jsutils/invariant';
12-
import keyMap from '../jsutils/keyMap';
1313
import keyValMap from '../jsutils/keyValMap';
1414
import { valueFromAST } from './valueFromAST';
1515
import { parseValue } from '../language/parser';
@@ -84,19 +84,18 @@ export function buildClientSchema(
8484
// Get the schema from the introspection result.
8585
const schemaIntrospection = introspection.__schema;
8686

87-
// Converts the list of types into a keyMap based on the type names.
88-
const typeIntrospectionMap = keyMap(
87+
// Iterate through all types, getting the type definition for each.
88+
const typeMap = keyValMap(
8989
schemaIntrospection.types,
90-
type => type.name,
90+
typeIntrospection => typeIntrospection.name,
91+
typeIntrospection => buildType(typeIntrospection),
9192
);
9293

93-
// A cache to use to store the actual GraphQLType definition objects by name.
94-
// Initialize to the GraphQL built in scalars. All functions below are inline
95-
// so that this type def cache is within the scope of the closure.
96-
const typeDefCache = keyMap(
97-
specifiedScalarTypes.concat(introspectionTypes),
98-
type => type.name,
99-
);
94+
for (const stdType of [...specifiedScalarTypes, ...introspectionTypes]) {
95+
if (typeMap[stdType.name]) {
96+
typeMap[stdType.name] = stdType;
97+
}
98+
}
10099

101100
// Given a type reference in introspection, return the GraphQLType instance.
102101
// preferring cached instances before building new instances.
@@ -123,20 +122,16 @@ export function buildClientSchema(
123122
}
124123

125124
function getNamedType(typeName: string): GraphQLNamedType {
126-
if (typeDefCache[typeName]) {
127-
return typeDefCache[typeName];
128-
}
129-
const typeIntrospection = typeIntrospectionMap[typeName];
130-
if (!typeIntrospection) {
125+
const type = typeMap[typeName];
126+
if (!type) {
131127
throw new Error(
132128
`Invalid or incomplete schema, unknown type: ${typeName}. Ensure ` +
133129
'that a full introspection query is used in order to build a ' +
134130
'client schema.',
135131
);
136132
}
137-
const typeDef = buildType(typeIntrospection);
138-
typeDefCache[typeName] = typeDef;
139-
return typeDef;
133+
134+
return type;
140135
}
141136

142137
function getInputType(typeRef: IntrospectionInputTypeRef): GraphQLInputType {
@@ -362,12 +357,6 @@ export function buildClientSchema(
362357
});
363358
}
364359

365-
// Iterate through all types, getting the type definition for each, ensuring
366-
// that any type not directly referenced by a field will get created.
367-
const types = schemaIntrospection.types.map(typeIntrospection =>
368-
getNamedType(typeIntrospection.name),
369-
);
370-
371360
// Get the root Query, Mutation, and Subscription types.
372361
const queryType = schemaIntrospection.queryType
373362
? getObjectType(schemaIntrospection.queryType)
@@ -392,7 +381,7 @@ export function buildClientSchema(
392381
query: queryType,
393382
mutation: mutationType,
394383
subscription: subscriptionType,
395-
types,
384+
types: objectValues(typeMap),
396385
directives,
397386
assumeValid: options && options.assumeValid,
398387
allowedLegacyNames: options && options.allowedLegacyNames,

0 commit comments

Comments
 (0)