|
133 | 133 | "GraphQLTypeResolver", |
134 | 134 | "GraphQLUnionType", |
135 | 135 | "GraphQLWrappingType", |
| 136 | + "Thunk", |
136 | 137 | "ThunkCollection", |
137 | 138 | "ThunkMapping", |
138 | 139 | ] |
@@ -261,26 +262,18 @@ def __copy__(self) -> "GraphQLNamedType": # pragma: no cover |
261 | 262 |
|
262 | 263 | ThunkCollection = Union[Callable[[], Collection[T]], Collection[T]] |
263 | 264 | ThunkMapping = Union[Callable[[], Mapping[str, T]], Mapping[str, T]] |
| 265 | +Thunk = Union[Callable[[], T], T] |
264 | 266 |
|
265 | 267 |
|
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. |
268 | 270 |
|
269 | 271 | Used while defining GraphQL types to allow for circular references in otherwise |
270 | 272 | immutable type definitions. |
271 | 273 | """ |
272 | 274 | return thunk() if callable(thunk) else thunk |
273 | 275 |
|
274 | 276 |
|
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 | | - |
284 | 277 | GraphQLScalarSerializer = Callable[[Any], Any] |
285 | 278 | GraphQLScalarValueParser = Callable[[Any], Any] |
286 | 279 | GraphQLScalarLiteralParser = Callable[[ValueNode, Optional[Dict[str, Any]]], Any] |
@@ -739,7 +732,7 @@ def __copy__(self) -> "GraphQLObjectType": # pragma: no cover |
739 | 732 | def fields(self) -> GraphQLFieldMap: |
740 | 733 | """Get provided fields, wrapping them as GraphQLFields if needed.""" |
741 | 734 | try: |
742 | | - fields = resolve_thunk_mapping(self._fields) |
| 735 | + fields = resolve_thunk(self._fields) |
743 | 736 | except Exception as error: |
744 | 737 | raise TypeError(f"{self.name} fields cannot be resolved. {error}") |
745 | 738 | if not isinstance(fields, Mapping) or not all( |
@@ -767,7 +760,7 @@ def fields(self) -> GraphQLFieldMap: |
767 | 760 | def interfaces(self) -> List["GraphQLInterfaceType"]: |
768 | 761 | """Get provided interfaces.""" |
769 | 762 | try: |
770 | | - interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk_collection( |
| 763 | + interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk( |
771 | 764 | self._interfaces # type: ignore |
772 | 765 | ) |
773 | 766 | except Exception as error: |
@@ -864,7 +857,7 @@ def __copy__(self) -> "GraphQLInterfaceType": # pragma: no cover |
864 | 857 | def fields(self) -> GraphQLFieldMap: |
865 | 858 | """Get provided fields, wrapping them as GraphQLFields if needed.""" |
866 | 859 | try: |
867 | | - fields = resolve_thunk_mapping(self._fields) |
| 860 | + fields = resolve_thunk(self._fields) |
868 | 861 | except Exception as error: |
869 | 862 | raise TypeError(f"{self.name} fields cannot be resolved. {error}") |
870 | 863 | if not isinstance(fields, Mapping) or not all( |
@@ -892,7 +885,7 @@ def fields(self) -> GraphQLFieldMap: |
892 | 885 | def interfaces(self) -> List["GraphQLInterfaceType"]: |
893 | 886 | """Get provided interfaces.""" |
894 | 887 | try: |
895 | | - interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk_collection( |
| 888 | + interfaces: Collection["GraphQLInterfaceType"] = resolve_thunk( |
896 | 889 | self._interfaces # type: ignore |
897 | 890 | ) |
898 | 891 | except Exception as error: |
@@ -987,7 +980,7 @@ def __copy__(self) -> "GraphQLUnionType": # pragma: no cover |
987 | 980 | def types(self) -> List[GraphQLObjectType]: |
988 | 981 | """Get provided types.""" |
989 | 982 | try: |
990 | | - types: Collection[GraphQLObjectType] = resolve_thunk_collection(self._types) |
| 983 | + types: Collection[GraphQLObjectType] = resolve_thunk(self._types) |
991 | 984 | except Exception as error: |
992 | 985 | raise TypeError(f"{self.name} types cannot be resolved. {error}") |
993 | 986 | if types is None: |
@@ -1339,7 +1332,7 @@ def __copy__(self) -> "GraphQLInputObjectType": # pragma: no cover |
1339 | 1332 | def fields(self) -> GraphQLInputFieldMap: |
1340 | 1333 | """Get provided fields, wrap them as GraphQLInputField if needed.""" |
1341 | 1334 | try: |
1342 | | - fields = resolve_thunk_mapping(self._fields) |
| 1335 | + fields = resolve_thunk(self._fields) |
1343 | 1336 | except Exception as error: |
1344 | 1337 | raise TypeError(f"{self.name} fields cannot be resolved. {error}") |
1345 | 1338 | if not isinstance(fields, Mapping) or not all( |
|
0 commit comments