7
7
* @flow strict
8
8
*/
9
9
10
+ import objectValues from '../polyfills/objectValues' ;
10
11
import inspect from '../jsutils/inspect' ;
11
12
import invariant from '../jsutils/invariant' ;
12
- import keyMap from '../jsutils/keyMap' ;
13
13
import keyValMap from '../jsutils/keyValMap' ;
14
14
import { valueFromAST } from './valueFromAST' ;
15
15
import { parseValue } from '../language/parser' ;
@@ -84,19 +84,18 @@ export function buildClientSchema(
84
84
// Get the schema from the introspection result.
85
85
const schemaIntrospection = introspection . __schema ;
86
86
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 (
89
89
schemaIntrospection . types ,
90
- type => type . name ,
90
+ typeIntrospection => typeIntrospection . name ,
91
+ typeIntrospection => buildType ( typeIntrospection ) ,
91
92
) ;
92
93
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
+ }
100
99
101
100
// Given a type reference in introspection, return the GraphQLType instance.
102
101
// preferring cached instances before building new instances.
@@ -123,20 +122,16 @@ export function buildClientSchema(
123
122
}
124
123
125
124
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 ) {
131
127
throw new Error (
132
128
`Invalid or incomplete schema, unknown type: ${ typeName } . Ensure ` +
133
129
'that a full introspection query is used in order to build a ' +
134
130
'client schema.' ,
135
131
) ;
136
132
}
137
- const typeDef = buildType ( typeIntrospection ) ;
138
- typeDefCache [ typeName ] = typeDef ;
139
- return typeDef ;
133
+
134
+ return type ;
140
135
}
141
136
142
137
function getInputType ( typeRef : IntrospectionInputTypeRef ) : GraphQLInputType {
@@ -362,12 +357,6 @@ export function buildClientSchema(
362
357
} ) ;
363
358
}
364
359
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
-
371
360
// Get the root Query, Mutation, and Subscription types.
372
361
const queryType = schemaIntrospection . queryType
373
362
? getObjectType ( schemaIntrospection . queryType )
@@ -392,7 +381,7 @@ export function buildClientSchema(
392
381
query : queryType ,
393
382
mutation : mutationType ,
394
383
subscription : subscriptionType ,
395
- types,
384
+ types : objectValues ( typeMap ) ,
396
385
directives,
397
386
assumeValid : options && options . assumeValid ,
398
387
allowedLegacyNames : options && options . allowedLegacyNames ,
0 commit comments