Skip to content

Commit 4c8ecd4

Browse files
authored
Solve issues with pickled schemas (#173)
1 parent 0115ed4 commit 4c8ecd4

20 files changed

+1018
-529
lines changed

src/graphql/pyutils/undefined.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import Any
1+
import warnings
2+
from typing import Any, Optional
23

34

45
__all__ = ["Undefined", "UndefinedType"]
@@ -7,6 +8,18 @@
78
class UndefinedType(ValueError):
89
"""Auxiliary class for creating the Undefined singleton."""
910

11+
_instance: Optional["UndefinedType"] = None
12+
13+
def __new__(cls) -> "UndefinedType":
14+
if cls._instance is None:
15+
cls._instance = super().__new__(cls)
16+
else:
17+
warnings.warn("Redefinition of 'Undefined'", RuntimeWarning, stacklevel=2)
18+
return cls._instance
19+
20+
def __reduce__(self) -> str:
21+
return "Undefined"
22+
1023
def __repr__(self) -> str:
1124
return "Undefined"
1225

src/graphql/type/definition.py

+17
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,23 @@ class GraphQLNamedType(GraphQLType):
231231
ast_node: Optional[TypeDefinitionNode]
232232
extension_ast_nodes: Tuple[TypeExtensionNode, ...]
233233

234+
reserved_types: Dict[str, "GraphQLNamedType"] = {}
235+
236+
def __new__(cls, name: str, *_args: Any, **_kwargs: Any) -> "GraphQLNamedType":
237+
if name in cls.reserved_types:
238+
raise TypeError(f"Redefinition of reserved type {name!r}")
239+
return super().__new__(cls)
240+
241+
def __reduce__(self) -> Tuple[Callable, Tuple]:
242+
return self._get_instance, (self.name, tuple(self.to_kwargs().items()))
243+
244+
@classmethod
245+
def _get_instance(cls, name: str, args: Tuple) -> "GraphQLNamedType":
246+
try:
247+
return cls.reserved_types[name]
248+
except KeyError:
249+
return cls(**dict(args))
250+
234251
def __init__(
235252
self,
236253
name: str,

0 commit comments

Comments
 (0)