diff --git a/src/graphql/language/ast.py b/src/graphql/language/ast.py index a4a6fb3a..d9a2e80a 100644 --- a/src/graphql/language/ast.py +++ b/src/graphql/language/ast.py @@ -226,7 +226,7 @@ class Node: """AST nodes""" # allow custom attributes and weak references (not used internally) - __slots__ = "__dict__", "__weakref__", "loc" + __slots__ = "__dict__", "__weakref__", "loc", "_hash" loc: Optional[Location] @@ -255,7 +255,13 @@ def __eq__(self, other: Any) -> bool: ) def __hash__(self) -> int: - return hash(tuple(getattr(self, key) for key in self.keys)) + if getattr(self, "_hash", None) is None: + self._hash = hash(tuple(getattr(self, key) for key in self.keys)) + return self._hash + + def __setattr__(self, key, value): + object.__setattr__(self, "_hash", None) + super().__setattr__(key, value) def __copy__(self) -> "Node": """Create a shallow copy of the node."""