Skip to content

Commit a4abff9

Browse files
committed
Remove excessive cache inside build_client_schema
Replicates graphql/graphql-js@183ff32
1 parent ea79cc3 commit a4abff9

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

graphql/utilities/build_client_schema.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from itertools import chain
12
from typing import cast, Callable, Dict, List, Sequence
23

34
from ..error import INVALID
@@ -53,19 +54,6 @@ def build_client_schema(
5354
# Get the schema from the introspection result.
5455
schema_introspection = introspection["__schema"]
5556

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-
6957
# Given a type reference in introspection, return the GraphQLType instance,
7058
# preferring cached instances before building new instances.
7159
def get_type(type_ref: Dict) -> GraphQLType:
@@ -87,19 +75,14 @@ def get_type(type_ref: Dict) -> GraphQLType:
8775
return get_named_type(name)
8876

8977
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_:
9580
raise TypeError(
9681
f"Invalid or incomplete schema, unknown type: {type_name}."
9782
" Ensure that a full introspection query is used in order"
9883
" to build a client schema."
9984
)
100-
type_def = build_type(type_introspection)
101-
type_def_cache[type_name] = type_def
102-
return type_def
85+
return type_
10386

10487
def get_input_type(type_ref: Dict) -> GraphQLInputType:
10588
input_type = get_type(type_ref)
@@ -320,9 +303,17 @@ def build_directive(directive_introspection: Dict) -> GraphQLDirective:
320303
args=build_arg_value_def_map(directive_introspection["args"]),
321304
)
322305

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
326317

327318
# Get the root Query, Mutation, and Subscription types.
328319

@@ -355,7 +346,7 @@ def build_directive(directive_introspection: Dict) -> GraphQLDirective:
355346
query=query_type,
356347
mutation=mutation_type,
357348
subscription=subscription_type,
358-
types=types,
349+
types=list(type_map.values()),
359350
directives=directives,
360351
assume_valid=assume_valid,
361352
)

0 commit comments

Comments
 (0)