Skip to content

Commit b1e0c3b

Browse files
committed
Merge branch 'master' of github.com:syrusakbary/graphene
2 parents 3921e41 + 4677677 commit b1e0c3b

File tree

7 files changed

+90
-9
lines changed

7 files changed

+90
-9
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ easily.
1212
`Django <http://github.com/graphql-python/swapi-graphene>`__
1313
implementation
1414

15-
*What is supported in this Python version?* **Everything**: Interfaces, ObjectTypes, Scalars, Unions and Relay (Nodes, Connections and Mutations), in addition to queries, mutations and subscriptions.
15+
*What is supported in this Python version?* **Everything**: Interfaces,
16+
ObjectTypes, Scalars, Unions and Relay (Nodes, Connections), in addition
17+
to queries, mutations and subscriptions.
1618

1719
Installation
1820
------------

examples/field_example.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import graphene
2+
3+
4+
class Patron(graphene.ObjectType):
5+
id = graphene.ID()
6+
name = graphene.String()
7+
age = graphene.ID()
8+
9+
10+
class Query(graphene.ObjectType):
11+
12+
patron = graphene.Field(Patron)
13+
14+
def resolve_patron(self, args, info):
15+
return Patron(id=1, name='Demo')
16+
17+
schema = graphene.Schema(query=Query)
18+
query = '''
19+
query something{
20+
patron {
21+
id
22+
name
23+
}
24+
}
25+
'''
26+
result = schema.execute(query)
27+
print(result.data['patron'])

graphene/contrib/django/tests/test_types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from pytest import raises
21
from graphql.core.type import GraphQLInterfaceType, GraphQLObjectType
32
from mock import patch
3+
from pytest import raises
44

55
from graphene import Schema
66
from graphene.contrib.django.types import DjangoInterface, DjangoNode
@@ -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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def __init__(self, _root=None):
4949
))
5050
super(InstanceObjectType, self).__init__(_root=_root)
5151

52+
@property
53+
def instance(self):
54+
return self._root
55+
56+
@instance.setter
57+
def instance(self, value):
58+
self._root = value
59+
5260
def __getattr__(self, attr):
5361
return getattr(self._root, attr)
5462

@@ -67,7 +75,7 @@ class DjangoNode(BaseNode, DjangoInterface):
6775
id = GlobalIDField()
6876

6977
@classmethod
70-
def get_node(cls, id):
78+
def get_node(cls, id, info=None):
7179
try:
7280
instance = cls._meta.model.objects.get(id=id)
7381
return cls(instance)

graphene/relay/tests/test_types.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from pytest import raises
21
from graphql.core.type import GraphQLList
2+
from pytest import raises
33

44
import graphene
55
from graphene import relay
@@ -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: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import inspect
2+
import warnings
3+
from functools import wraps
14
from graphql_relay.node.node import to_global_id
25

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

79101
def to_global_id(self):
80102
type_name = self._meta.type_name

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def run_tests(self):
2424

2525
setup(
2626
name='graphene',
27-
version='0.4.0.1',
27+
version='0.4.1.1',
2828

2929
description='Graphene: Python DSL for GraphQL',
3030
long_description=open('README.rst').read(),

0 commit comments

Comments
 (0)