Skip to content

Commit d6a5269

Browse files
committed
Added support for old behavior of get_node
1 parent f69665d commit d6a5269

File tree

4 files changed

+47
-5
lines changed

4 files changed

+47
-5
lines changed

graphene/contrib/django/tests/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_django_interface():
3636

3737
@patch('graphene.contrib.django.tests.models.Article.objects.get', return_value=Article(id=1))
3838
def test_django_get_node(get):
39-
human = Human.get_node(1)
39+
human = Human.get_node(1, None)
4040
get.assert_called_with(id=1)
4141
assert human.id == 1
4242

graphene/contrib/django/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class DjangoNode(BaseNode, DjangoInterface):
6767
id = GlobalIDField()
6868

6969
@classmethod
70-
def get_node(cls, id):
70+
def get_node(cls, id, info=None):
7171
try:
7272
instance = cls._meta.model.objects.get(id=id)
7373
return cls(instance)

graphene/relay/tests/test_types.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,32 @@ class OtherNode(relay.Node):
1111
name = graphene.String()
1212

1313
@classmethod
14-
def get_node(cls, id):
14+
def get_node(cls, id, info):
1515
pass
1616

1717

18+
def test_works_old_get_node():
19+
class Part(relay.Node):
20+
x = graphene.String()
21+
22+
@classmethod
23+
def get_node(cls, id):
24+
return id
25+
26+
assert Part.get_node(1) == 1
27+
28+
29+
def test_works_old_static_get_node():
30+
class Part(relay.Node):
31+
x = graphene.String()
32+
33+
@staticmethod
34+
def get_node(id):
35+
return id
36+
37+
assert Part.get_node(1) == 1
38+
39+
1840
def test_field_no_contributed_raises_error():
1941
with raises(Exception) as excinfo:
2042
class Part(relay.Node):

graphene/relay/types.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
from functools import wraps
13
from graphql_relay.node.node import to_global_id
24

35
from ..core.types import (Boolean, Field, InputObjectType, Interface, List,
@@ -73,8 +75,26 @@ class BaseNode(object):
7375
def _prepare_class(cls):
7476
from graphene.relay.utils import is_node
7577
if is_node(cls):
76-
assert hasattr(
77-
cls, 'get_node'), 'get_node classmethod not found in %s Node' % cls
78+
get_node = getattr(cls, 'get_node')
79+
assert get_node, 'get_node classmethod not found in %s Node' % cls
80+
assert callable(get_node), 'get_node have to be callable'
81+
args = 3
82+
if isinstance(get_node, staticmethod):
83+
args -= 1
84+
85+
if get_node.func_code.co_argcount < args:
86+
warnings.warn("get_node will receive also the info arg"
87+
" in future versions of graphene".format(cls.__name__),
88+
FutureWarning)
89+
90+
@staticmethod
91+
@wraps(get_node)
92+
def wrapped_node(*node_args):
93+
if len(node_args) < args:
94+
node_args += (None, )
95+
return get_node(*node_args[:-1])
96+
97+
setattr(cls, 'get_node', wrapped_node)
7898

7999
def to_global_id(self):
80100
type_name = self._meta.type_name

0 commit comments

Comments
 (0)