diff --git a/src/graphql/execution/middleware.py b/src/graphql/execution/middleware.py index 334a088a..d75243e6 100644 --- a/src/graphql/execution/middleware.py +++ b/src/graphql/execution/middleware.py @@ -20,7 +20,7 @@ class MiddlewareManager: must be aware of this and check whether values are awaitable before awaiting them. """ - __slots__ = "middlewares", "_middleware_resolvers", "_cached_resolvers" + __slots__ = "__weakref__", "__dict__", "middlewares", "_middleware_resolvers", "_cached_resolvers" _cached_resolvers: Dict[GraphQLFieldResolver, GraphQLFieldResolver] _middleware_resolvers: Optional[List[Callable]] diff --git a/src/graphql/language/ast.py b/src/graphql/language/ast.py index a5da60c6..74e0c075 100644 --- a/src/graphql/language/ast.py +++ b/src/graphql/language/ast.py @@ -214,7 +214,7 @@ class OperationType(Enum): class Node: """AST nodes""" - __slots__ = ("loc",) + __slots__ = ("__dict__", "__weakref__", "loc",) loc: Optional[Location] diff --git a/src/graphql/language/source.py b/src/graphql/language/source.py index 9b0e0c42..7f942d93 100644 --- a/src/graphql/language/source.py +++ b/src/graphql/language/source.py @@ -6,7 +6,7 @@ class Source: """A representation of source input to GraphQL.""" - __slots__ = "body", "name", "location_offset" + __slots__ = "__weakref__", "__dict__", "body", "name", "location_offset" def __init__( self, diff --git a/tests/language/test_ast.py b/tests/language/test_ast.py index e9162e98..aa965d57 100644 --- a/tests/language/test_ast.py +++ b/tests/language/test_ast.py @@ -1,4 +1,5 @@ from copy import copy, deepcopy +import weakref from graphql.language import Location, Node, Source, Token, TokenKind from graphql.pyutils import inspect @@ -224,3 +225,13 @@ class Foo(Node): def provides_keys_as_class_attribute(): assert SampleTestNode.keys == ["loc", "alpha", "beta"] + + def can_weakref(): + node = SampleTestNode(alpha=1, beta=2) + wr = weakref.ref(node) # That this works is 90% of the test + assert wr() is node + + def can_make_attrs(): + node = SampleTestNode(alpha=1, beta=2) + node.__new_random_attr = "Hello!" # That this works is 90% of the test + assert node.__new_random_attr == "Hello!"