1
+ from itertools import chain
1
2
from typing import cast , Callable , Dict , List , Sequence
2
3
3
4
from ..error import INVALID
@@ -53,19 +54,6 @@ def build_client_schema(
53
54
# Get the schema from the introspection result.
54
55
schema_introspection = introspection ["__schema" ]
55
56
56
- # Converts the list of types into a dict based on the type names.
57
- type_introspection_map : Dict [str , Dict ] = {
58
- type_ ["name" ]: type_ for type_ in schema_introspection ["types" ]
59
- }
60
-
61
- # A cache to use to store the actual GraphQLType definition objects by name.
62
- # Initialize to the GraphQL built in scalars. All functions below are inline so
63
- # that this type def cache is within the scope of the closure.
64
- type_def_cache : Dict [str , GraphQLNamedType ] = {
65
- ** specified_scalar_types ,
66
- ** introspection_types ,
67
- }
68
-
69
57
# Given a type reference in introspection, return the GraphQLType instance,
70
58
# preferring cached instances before building new instances.
71
59
def get_type (type_ref : Dict ) -> GraphQLType :
@@ -87,19 +75,14 @@ def get_type(type_ref: Dict) -> GraphQLType:
87
75
return get_named_type (name )
88
76
89
77
def get_named_type (type_name : str ) -> GraphQLNamedType :
90
- cached_type = type_def_cache .get (type_name )
91
- if cached_type :
92
- return cached_type
93
- type_introspection = type_introspection_map .get (type_name )
94
- if not type_introspection :
78
+ type_ = type_map .get (type_name )
79
+ if not type_ :
95
80
raise TypeError (
96
81
f"Invalid or incomplete schema, unknown type: { type_name } ."
97
82
" Ensure that a full introspection query is used in order"
98
83
" to build a client schema."
99
84
)
100
- type_def = build_type (type_introspection )
101
- type_def_cache [type_name ] = type_def
102
- return type_def
85
+ return type_
103
86
104
87
def get_input_type (type_ref : Dict ) -> GraphQLInputType :
105
88
input_type = get_type (type_ref )
@@ -320,9 +303,17 @@ def build_directive(directive_introspection: Dict) -> GraphQLDirective:
320
303
args = build_arg_value_def_map (directive_introspection ["args" ]),
321
304
)
322
305
323
- # Iterate through all types, getting the type definition for each, ensuring that
324
- # any type not directly referenced by a field will get created.
325
- types = [get_named_type (name ) for name in type_introspection_map ]
306
+ # Iterate through all types, getting the type definition for each.
307
+ type_map : Dict [str , GraphQLNamedType ] = {
308
+ type_introspection ["name" ]: build_type (type_introspection )
309
+ for type_introspection in schema_introspection ["types" ]
310
+ }
311
+
312
+ for std_type_name , std_type in chain (
313
+ specified_scalar_types .items (), introspection_types .items ()
314
+ ):
315
+ if std_type_name in type_map :
316
+ type_map [std_type_name ] = std_type
326
317
327
318
# Get the root Query, Mutation, and Subscription types.
328
319
@@ -355,7 +346,7 @@ def build_directive(directive_introspection: Dict) -> GraphQLDirective:
355
346
query = query_type ,
356
347
mutation = mutation_type ,
357
348
subscription = subscription_type ,
358
- types = types ,
349
+ types = list ( type_map . values ()) ,
359
350
directives = directives ,
360
351
assume_valid = assume_valid ,
361
352
)
0 commit comments