Skip to content

Solve issues with pickled schemas (#173) #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Nov 2, 2022
15 changes: 14 additions & 1 deletion src/graphql/pyutils/undefined.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any
import warnings
from typing import Any, Optional


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

_instance: Optional["UndefinedType"] = None

def __new__(cls) -> "UndefinedType":
if cls._instance is None:
cls._instance = super().__new__(cls)
else:
warnings.warn("Redefinition of 'Undefined'", RuntimeWarning, stacklevel=2)
return cls._instance

def __reduce__(self) -> str:
return "Undefined"

def __repr__(self) -> str:
return "Undefined"

Expand Down
17 changes: 17 additions & 0 deletions src/graphql/type/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,23 @@ class GraphQLNamedType(GraphQLType):
ast_node: Optional[TypeDefinitionNode]
extension_ast_nodes: Tuple[TypeExtensionNode, ...]

reserved_types: Dict[str, "GraphQLNamedType"] = {}

def __new__(cls, name: str, *_args: Any, **_kwargs: Any) -> "GraphQLNamedType":
if name in cls.reserved_types:
raise TypeError(f"Redefinition of reserved type {name!r}")
return super().__new__(cls)

def __reduce__(self) -> Tuple[Callable, Tuple]:
return self._get_instance, (self.name, tuple(self.to_kwargs().items()))

@classmethod
def _get_instance(cls, name: str, args: Tuple) -> "GraphQLNamedType":
try:
return cls.reserved_types[name]
except KeyError:
return cls(**dict(args))

def __init__(
self,
name: str,
Expand Down
Loading