1- from typing import Any , Callable , Dict , Generator , Iterable , List , Optional , Tuple , TypeVar , Union
1+ import operator
2+ from typing import Any , Callable , Dict , Iterable , List , Optional , Tuple , TypeVar
23
34import graphql
45from graphql import is_equal_type
@@ -114,14 +115,17 @@ def selections_for_type(
114115 if not implementations :
115116 # Shortcut when there are no implementations - take fields from the interface itself
116117 return selections (context , field_type )
117- return unique_by (implementations , lambda v : v . name ).flatmap (lambda t : interfaces (context , field_type , t ))
118+ return unique_by (implementations , BY_NAME ).flatmap (lambda t : interfaces (context , field_type , t ))
118119 if isinstance (field_type , graphql .GraphQLUnionType ):
119120 # A union is a set of object types - take a subset of them and generate inline fragments
120- return unique_by (field_type .types , lambda m : m . name ).flatmap (lambda m : inline_fragments (context , m ))
121+ return unique_by (field_type .types , BY_NAME ).flatmap (lambda m : inline_fragments (context , m ))
121122 # Other types don't have fields
122123 return st .none ()
123124
124125
126+ BY_NAME = operator .attrgetter ("name" )
127+
128+
125129def interfaces (
126130 context : Context , interface : graphql .GraphQLInterfaceType , implementations : List [InterfaceOrObject ]
127131) -> st .SearchStrategy [SelectionNodes ]:
@@ -132,18 +136,16 @@ def interfaces(
132136 if overlapping_fields :
133137 return compose_interfaces_with_filter (selections (context , interface ), strategies , implementations )
134138 # No overlapping - safe to choose any subset of fields within the interface itself and any fragment
135- return st .tuples (selections (context , interface ), * strategies ).map (flatten ). map ( list ) # type: ignore
139+ return st .tuples (selections (context , interface ), * strategies ).map (flatten ) # type: ignore
136140
137141
138142T = TypeVar ("T" )
139143
140144
141- def flatten (items : Tuple [Union [T , List [T ]], ...]) -> Generator [T , None , None ]:
142- for item in items :
143- if isinstance (item , list ):
144- yield from item
145- else :
146- yield item
145+ def flatten (items : Tuple [List [T ], T ]) -> List [T ]:
146+ output = items [0 ]
147+ output .extend (items [1 :])
148+ return output
147149
148150
149151def inline_fragments (context : Context , items : List [graphql .GraphQLObjectType ]) -> st .SearchStrategy [SelectionNodes ]:
0 commit comments