Skip to content

Commit 6c01733

Browse files
committed
Make thunk resolver simpler and backward compatible
1 parent 1804bd7 commit 6c01733

File tree

4 files changed

+15
-17
lines changed

4 files changed

+15
-17
lines changed

docs/modules/type.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Types
9292
.. autoclass:: GraphQLType
9393
.. autoclass:: GraphQLWrappingType
9494

95+
.. autoclass:: Thunk
9596
.. autoclass:: ThunkCollection
9697
.. autoclass:: ThunkMapping
9798

src/graphql/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
GraphQLNamedType,
145145
GraphQLNamedInputType,
146146
GraphQLNamedOutputType,
147+
Thunk,
147148
ThunkCollection,
148149
ThunkMapping,
149150
GraphQLArgument,
@@ -517,6 +518,7 @@
517518
"GraphQLNamedType",
518519
"GraphQLNamedInputType",
519520
"GraphQLNamedOutputType",
521+
"Thunk",
520522
"ThunkCollection",
521523
"ThunkMapping",
522524
"GraphQLArgument",

src/graphql/type/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
GraphQLNamedType,
7979
GraphQLNamedInputType,
8080
GraphQLNamedOutputType,
81+
Thunk,
8182
ThunkCollection,
8283
ThunkMapping,
8384
GraphQLArgument,
@@ -208,6 +209,7 @@
208209
"GraphQLNamedType",
209210
"GraphQLNamedInputType",
210211
"GraphQLNamedOutputType",
212+
"Thunk",
211213
"ThunkCollection",
212214
"ThunkMapping",
213215
"GraphQLArgument",

src/graphql/type/definition.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
"GraphQLTypeResolver",
134134
"GraphQLUnionType",
135135
"GraphQLWrappingType",
136+
"Thunk",
136137
"ThunkCollection",
137138
"ThunkMapping",
138139
]
@@ -261,26 +262,18 @@ def __copy__(self) -> "GraphQLNamedType": # pragma: no cover
261262

262263
ThunkCollection = Union[Callable[[], Collection[T]], Collection[T]]
263264
ThunkMapping = Union[Callable[[], Mapping[str, T]], Mapping[str, T]]
265+
Thunk = Union[Callable[[], T], T]
264266

265267

266-
def resolve_thunk_collection(thunk: ThunkCollection[T]) -> Collection[T]:
267-
"""Resolve the given thunk for a collection.
268+
def resolve_thunk(thunk: Thunk[T]) -> T:
269+
"""Resolve the given thunk.
268270
269271
Used while defining GraphQL types to allow for circular references in otherwise
270272
immutable type definitions.
271273
"""
272274
return thunk() if callable(thunk) else thunk
273275

274276

275-
def resolve_thunk_mapping(thunk: ThunkMapping[T]) -> Mapping[str, T]:
276-
"""Resolve the given thunk for a mapping.
277-
278-
Used while defining GraphQL fields to allow for circular references in otherwise
279-
immutable field definitions.
280-
"""
281-
return thunk() if callable(thunk) else thunk
282-
283-
284277
GraphQLScalarSerializer = Callable[[Any], Any]
285278
GraphQLScalarValueParser = Callable[[Any], Any]
286279
GraphQLScalarLiteralParser = Callable[[ValueNode, Optional[Dict[str, Any]]], Any]
@@ -739,7 +732,7 @@ def __copy__(self) -> "GraphQLObjectType": # pragma: no cover
739732
def fields(self) -> GraphQLFieldMap:
740733
"""Get provided fields, wrapping them as GraphQLFields if needed."""
741734
try:
742-
fields = resolve_thunk_mapping(self._fields)
735+
fields = resolve_thunk(self._fields)
743736
except Exception as error:
744737
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
745738
if not isinstance(fields, Mapping) or not all(
@@ -767,7 +760,7 @@ def fields(self) -> GraphQLFieldMap:
767760
def interfaces(self) -> List["GraphQLInterfaceType"]:
768761
"""Get provided interfaces."""
769762
try:
770-
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk_collection(
763+
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk(
771764
self._interfaces # type: ignore
772765
)
773766
except Exception as error:
@@ -864,7 +857,7 @@ def __copy__(self) -> "GraphQLInterfaceType": # pragma: no cover
864857
def fields(self) -> GraphQLFieldMap:
865858
"""Get provided fields, wrapping them as GraphQLFields if needed."""
866859
try:
867-
fields = resolve_thunk_mapping(self._fields)
860+
fields = resolve_thunk(self._fields)
868861
except Exception as error:
869862
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
870863
if not isinstance(fields, Mapping) or not all(
@@ -892,7 +885,7 @@ def fields(self) -> GraphQLFieldMap:
892885
def interfaces(self) -> List["GraphQLInterfaceType"]:
893886
"""Get provided interfaces."""
894887
try:
895-
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk_collection(
888+
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk(
896889
self._interfaces # type: ignore
897890
)
898891
except Exception as error:
@@ -987,7 +980,7 @@ def __copy__(self) -> "GraphQLUnionType": # pragma: no cover
987980
def types(self) -> List[GraphQLObjectType]:
988981
"""Get provided types."""
989982
try:
990-
types: Collection[GraphQLObjectType] = resolve_thunk_collection(self._types)
983+
types: Collection[GraphQLObjectType] = resolve_thunk(self._types)
991984
except Exception as error:
992985
raise TypeError(f"{self.name} types cannot be resolved. {error}")
993986
if types is None:
@@ -1339,7 +1332,7 @@ def __copy__(self) -> "GraphQLInputObjectType": # pragma: no cover
13391332
def fields(self) -> GraphQLInputFieldMap:
13401333
"""Get provided fields, wrap them as GraphQLInputField if needed."""
13411334
try:
1342-
fields = resolve_thunk_mapping(self._fields)
1335+
fields = resolve_thunk(self._fields)
13431336
except Exception as error:
13441337
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
13451338
if not isinstance(fields, Mapping) or not all(

0 commit comments

Comments
 (0)