Skip to content

Commit

Permalink
Solve issues with pickled schemas (graphql-python#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito authored and danvendia committed Nov 18, 2022
1 parent fccf52e commit 7d08481
Show file tree
Hide file tree
Showing 20 changed files with 1,173 additions and 531 deletions.
15 changes: 14 additions & 1 deletion src/graphql/pyutils/undefined.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
from typing import Any
import warnings
from typing import Any, Optional

__all__ = ["Undefined", "UndefinedType"]


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 @@ -228,6 +228,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

0 comments on commit 7d08481

Please sign in to comment.