Skip to content

Commit ed27ce0

Browse files
committed
Moved type_map_reducer inside schema
1 parent 2b0232f commit ed27ce0

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

graphql/type/directives.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import collections
22

33
from ..utils.assert_valid_name import assert_valid_name
4-
from .definition import GraphQLArgument, GraphQLArgumentDefinition, GraphQLNonNull, is_input_type
4+
from .definition import (GraphQLArgument, GraphQLArgumentDefinition,
5+
GraphQLNonNull, is_input_type)
56
from .scalars import GraphQLBoolean
67

78

graphql/type/introspection.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
from ..language.printer import print_ast
44
from ..utils.ast_from_value import ast_from_value
5-
from .definition import (GraphQLArgument, GraphQLArgumentDefinition, GraphQLEnumType, GraphQLEnumValue,
6-
GraphQLField, GraphQLInputObjectType,
5+
from .definition import (GraphQLArgument, GraphQLArgumentDefinition,
6+
GraphQLEnumType, GraphQLEnumValue, GraphQLField,
7+
GraphQLFieldDefinition, GraphQLInputObjectType,
78
GraphQLInterfaceType, GraphQLList, GraphQLNonNull,
89
GraphQLObjectType, GraphQLScalarType,
9-
GraphQLUnionType, GraphQLFieldDefinition)
10+
GraphQLUnionType)
1011
from .directives import DirectiveLocation
1112
from .scalars import GraphQLBoolean, GraphQLString
1213

graphql/type/schema.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def _build_type_map(self, _types):
104104
if _types:
105105
types += _types
106106

107-
type_map = reduce(type_map_reducer, types, OrderedDict())
107+
type_map = reduce(self._type_map_reducer, types, OrderedDict())
108108
return type_map
109109

110110
def get_possible_types(self, abstract_type):
@@ -120,45 +120,44 @@ def is_possible_type(self, abstract_type, possible_type):
120120

121121
return possible_type.name in self._possible_type_map[abstract_type.name]
122122

123+
def _type_map_reducer(self, map, type):
124+
if not type:
125+
return map
123126

124-
def type_map_reducer(map, type):
125-
if not type:
126-
return map
127+
if isinstance(type, GraphQLList) or isinstance(type, GraphQLNonNull):
128+
return self._type_map_reducer(map, type.of_type)
127129

128-
if isinstance(type, GraphQLList) or isinstance(type, GraphQLNonNull):
129-
return type_map_reducer(map, type.of_type)
130+
if type.name in map:
131+
assert map[type.name] == type, (
132+
'Schema must contain unique named types but contains multiple types named "{}".'
133+
).format(type.name)
130134

131-
if type.name in map:
132-
assert map[type.name] == type, (
133-
'Schema must contain unique named types but contains multiple types named "{}".'
134-
).format(type.name)
135+
return map
135136

136-
return map
137+
map[type.name] = type
137138

138-
map[type.name] = type
139+
reduced_map = map
139140

140-
reduced_map = map
141+
if isinstance(type, (GraphQLUnionType)):
142+
for t in type.get_types():
143+
reduced_map = self._type_map_reducer(reduced_map, t)
141144

142-
if isinstance(type, (GraphQLUnionType)):
143-
for t in type.get_types():
144-
reduced_map = type_map_reducer(reduced_map, t)
145+
if isinstance(type, GraphQLObjectType):
146+
for t in type.get_interfaces():
147+
reduced_map = self._type_map_reducer(reduced_map, t)
145148

146-
if isinstance(type, GraphQLObjectType):
147-
for t in type.get_interfaces():
148-
reduced_map = type_map_reducer(reduced_map, t)
149+
if isinstance(type, (GraphQLObjectType, GraphQLInterfaceType, GraphQLInputObjectType)):
150+
field_map = type.get_fields()
151+
for field in field_map.values():
152+
args = getattr(field, 'args', None)
153+
if args:
154+
field_arg_types = [arg.type for arg in field.args]
155+
for t in field_arg_types:
156+
reduced_map = self._type_map_reducer(reduced_map, t)
149157

150-
if isinstance(type, (GraphQLObjectType, GraphQLInterfaceType, GraphQLInputObjectType)):
151-
field_map = type.get_fields()
152-
for field in field_map.values():
153-
args = getattr(field, 'args', None)
154-
if args:
155-
field_arg_types = [arg.type for arg in field.args]
156-
for t in field_arg_types:
157-
reduced_map = type_map_reducer(reduced_map, t)
158+
reduced_map = self._type_map_reducer(reduced_map, getattr(field, 'type', None))
158159

159-
reduced_map = type_map_reducer(reduced_map, getattr(field, 'type', None))
160-
161-
return reduced_map
160+
return reduced_map
162161

163162

164163
def assert_object_implements_interface(schema, object, interface):

0 commit comments

Comments
 (0)