Skip to content

Commit

Permalink
Make nodes extensible and referencable s (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstraLuma authored Mar 18, 2020
1 parent f98f3ff commit faa80af
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/graphql/execution/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
2 changes: 1 addition & 1 deletion src/graphql/language/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class OperationType(Enum):
class Node:
"""AST nodes"""

__slots__ = ("loc",)
__slots__ = ("__dict__", "__weakref__", "loc",)

loc: Optional[Location]

Expand Down
2 changes: 1 addition & 1 deletion src/graphql/language/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions tests/language/test_ast.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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!"

0 comments on commit faa80af

Please sign in to comment.