Skip to content

Commit

Permalink
Make thunk resolver simpler and backward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Sep 30, 2021
1 parent 1804bd7 commit 6c01733
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/modules/type.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Types
.. autoclass:: GraphQLType
.. autoclass:: GraphQLWrappingType

.. autoclass:: Thunk
.. autoclass:: ThunkCollection
.. autoclass:: ThunkMapping

Expand Down
2 changes: 2 additions & 0 deletions src/graphql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
Thunk,
ThunkCollection,
ThunkMapping,
GraphQLArgument,
Expand Down Expand Up @@ -517,6 +518,7 @@
"GraphQLNamedType",
"GraphQLNamedInputType",
"GraphQLNamedOutputType",
"Thunk",
"ThunkCollection",
"ThunkMapping",
"GraphQLArgument",
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/type/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
GraphQLNamedType,
GraphQLNamedInputType,
GraphQLNamedOutputType,
Thunk,
ThunkCollection,
ThunkMapping,
GraphQLArgument,
Expand Down Expand Up @@ -208,6 +209,7 @@
"GraphQLNamedType",
"GraphQLNamedInputType",
"GraphQLNamedOutputType",
"Thunk",
"ThunkCollection",
"ThunkMapping",
"GraphQLArgument",
Expand Down
27 changes: 10 additions & 17 deletions src/graphql/type/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
"GraphQLTypeResolver",
"GraphQLUnionType",
"GraphQLWrappingType",
"Thunk",
"ThunkCollection",
"ThunkMapping",
]
Expand Down Expand Up @@ -261,26 +262,18 @@ def __copy__(self) -> "GraphQLNamedType": # pragma: no cover

ThunkCollection = Union[Callable[[], Collection[T]], Collection[T]]
ThunkMapping = Union[Callable[[], Mapping[str, T]], Mapping[str, T]]
Thunk = Union[Callable[[], T], T]


def resolve_thunk_collection(thunk: ThunkCollection[T]) -> Collection[T]:
"""Resolve the given thunk for a collection.
def resolve_thunk(thunk: Thunk[T]) -> T:
"""Resolve the given thunk.
Used while defining GraphQL types to allow for circular references in otherwise
immutable type definitions.
"""
return thunk() if callable(thunk) else thunk


def resolve_thunk_mapping(thunk: ThunkMapping[T]) -> Mapping[str, T]:
"""Resolve the given thunk for a mapping.
Used while defining GraphQL fields to allow for circular references in otherwise
immutable field definitions.
"""
return thunk() if callable(thunk) else thunk


GraphQLScalarSerializer = Callable[[Any], Any]
GraphQLScalarValueParser = Callable[[Any], Any]
GraphQLScalarLiteralParser = Callable[[ValueNode, Optional[Dict[str, Any]]], Any]
Expand Down Expand Up @@ -739,7 +732,7 @@ def __copy__(self) -> "GraphQLObjectType": # pragma: no cover
def fields(self) -> GraphQLFieldMap:
"""Get provided fields, wrapping them as GraphQLFields if needed."""
try:
fields = resolve_thunk_mapping(self._fields)
fields = resolve_thunk(self._fields)
except Exception as error:
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
if not isinstance(fields, Mapping) or not all(
Expand Down Expand Up @@ -767,7 +760,7 @@ def fields(self) -> GraphQLFieldMap:
def interfaces(self) -> List["GraphQLInterfaceType"]:
"""Get provided interfaces."""
try:
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk_collection(
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk(
self._interfaces # type: ignore
)
except Exception as error:
Expand Down Expand Up @@ -864,7 +857,7 @@ def __copy__(self) -> "GraphQLInterfaceType": # pragma: no cover
def fields(self) -> GraphQLFieldMap:
"""Get provided fields, wrapping them as GraphQLFields if needed."""
try:
fields = resolve_thunk_mapping(self._fields)
fields = resolve_thunk(self._fields)
except Exception as error:
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
if not isinstance(fields, Mapping) or not all(
Expand Down Expand Up @@ -892,7 +885,7 @@ def fields(self) -> GraphQLFieldMap:
def interfaces(self) -> List["GraphQLInterfaceType"]:
"""Get provided interfaces."""
try:
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk_collection(
interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk(
self._interfaces # type: ignore
)
except Exception as error:
Expand Down Expand Up @@ -987,7 +980,7 @@ def __copy__(self) -> "GraphQLUnionType": # pragma: no cover
def types(self) -> List[GraphQLObjectType]:
"""Get provided types."""
try:
types: Collection[GraphQLObjectType] = resolve_thunk_collection(self._types)
types: Collection[GraphQLObjectType] = resolve_thunk(self._types)
except Exception as error:
raise TypeError(f"{self.name} types cannot be resolved. {error}")
if types is None:
Expand Down Expand Up @@ -1339,7 +1332,7 @@ def __copy__(self) -> "GraphQLInputObjectType": # pragma: no cover
def fields(self) -> GraphQLInputFieldMap:
"""Get provided fields, wrap them as GraphQLInputField if needed."""
try:
fields = resolve_thunk_mapping(self._fields)
fields = resolve_thunk(self._fields)
except Exception as error:
raise TypeError(f"{self.name} fields cannot be resolved. {error}")
if not isinstance(fields, Mapping) or not all(
Expand Down

0 comments on commit 6c01733

Please sign in to comment.