Skip to content

Commit 7d08481

Browse files
Citodanvendia
authored and
danvendia
committed
Solve issues with pickled schemas (graphql-python#173)
1 parent fccf52e commit 7d08481

20 files changed

+1173
-531
lines changed

src/graphql/pyutils/undefined.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
from typing import Any
1+
import warnings
2+
from typing import Any, Optional
23

34
__all__ = ["Undefined", "UndefinedType"]
45

56

67
class UndefinedType(ValueError):
78
"""Auxiliary class for creating the Undefined singleton."""
89

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

src/graphql/type/definition.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,23 @@ class GraphQLNamedType(GraphQLType):
228228
ast_node: Optional[TypeDefinitionNode]
229229
extension_ast_nodes: Tuple[TypeExtensionNode, ...]
230230

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

0 commit comments

Comments
 (0)