Skip to content

Commit faa80af

Browse files
authored
Make nodes extensible and referencable s (#82)
1 parent f98f3ff commit faa80af

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

Diff for: src/graphql/execution/middleware.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MiddlewareManager:
2020
must be aware of this and check whether values are awaitable before awaiting them.
2121
"""
2222

23-
__slots__ = "middlewares", "_middleware_resolvers", "_cached_resolvers"
23+
__slots__ = "__weakref__", "__dict__", "middlewares", "_middleware_resolvers", "_cached_resolvers"
2424

2525
_cached_resolvers: Dict[GraphQLFieldResolver, GraphQLFieldResolver]
2626
_middleware_resolvers: Optional[List[Callable]]

Diff for: src/graphql/language/ast.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class OperationType(Enum):
214214
class Node:
215215
"""AST nodes"""
216216

217-
__slots__ = ("loc",)
217+
__slots__ = ("__dict__", "__weakref__", "loc",)
218218

219219
loc: Optional[Location]
220220

Diff for: src/graphql/language/source.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class Source:
77
"""A representation of source input to GraphQL."""
88

9-
__slots__ = "body", "name", "location_offset"
9+
__slots__ = "__weakref__", "__dict__", "body", "name", "location_offset"
1010

1111
def __init__(
1212
self,

Diff for: tests/language/test_ast.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from copy import copy, deepcopy
2+
import weakref
23

34
from graphql.language import Location, Node, Source, Token, TokenKind
45
from graphql.pyutils import inspect
@@ -224,3 +225,13 @@ class Foo(Node):
224225

225226
def provides_keys_as_class_attribute():
226227
assert SampleTestNode.keys == ["loc", "alpha", "beta"]
228+
229+
def can_weakref():
230+
node = SampleTestNode(alpha=1, beta=2)
231+
wr = weakref.ref(node) # That this works is 90% of the test
232+
assert wr() is node
233+
234+
def can_make_attrs():
235+
node = SampleTestNode(alpha=1, beta=2)
236+
node.__new_random_attr = "Hello!" # That this works is 90% of the test
237+
assert node.__new_random_attr == "Hello!"

0 commit comments

Comments
 (0)