Skip to content

Commit

Permalink
Merge branch 'master' of github.com:syrusakbary/graphene
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Nov 20, 2015
2 parents 3921e41 + 4677677 commit b1e0c3b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ easily.
`Django <http://github.com/graphql-python/swapi-graphene>`__
implementation

*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.
*What is supported in this Python version?* **Everything**: Interfaces,
ObjectTypes, Scalars, Unions and Relay (Nodes, Connections), in addition
to queries, mutations and subscriptions.

Installation
------------
Expand Down
27 changes: 27 additions & 0 deletions examples/field_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import graphene


class Patron(graphene.ObjectType):
id = graphene.ID()
name = graphene.String()
age = graphene.ID()


class Query(graphene.ObjectType):

patron = graphene.Field(Patron)

def resolve_patron(self, args, info):
return Patron(id=1, name='Demo')

schema = graphene.Schema(query=Query)
query = '''
query something{
patron {
id
name
}
}
'''
result = schema.execute(query)
print(result.data['patron'])
4 changes: 2 additions & 2 deletions graphene/contrib/django/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pytest import raises
from graphql.core.type import GraphQLInterfaceType, GraphQLObjectType
from mock import patch
from pytest import raises

from graphene import Schema
from graphene.contrib.django.types import DjangoInterface, DjangoNode
Expand Down Expand Up @@ -36,7 +36,7 @@ def test_django_interface():

@patch('graphene.contrib.django.tests.models.Article.objects.get', return_value=Article(id=1))
def test_django_get_node(get):
human = Human.get_node(1)
human = Human.get_node(1, None)
get.assert_called_with(id=1)
assert human.id == 1

Expand Down
10 changes: 9 additions & 1 deletion graphene/contrib/django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def __init__(self, _root=None):
))
super(InstanceObjectType, self).__init__(_root=_root)

@property
def instance(self):
return self._root

@instance.setter
def instance(self, value):
self._root = value

def __getattr__(self, attr):
return getattr(self._root, attr)

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

@classmethod
def get_node(cls, id):
def get_node(cls, id, info=None):
try:
instance = cls._meta.model.objects.get(id=id)
return cls(instance)
Expand Down
26 changes: 24 additions & 2 deletions graphene/relay/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pytest import raises
from graphql.core.type import GraphQLList
from pytest import raises

import graphene
from graphene import relay
Expand All @@ -11,10 +11,32 @@ class OtherNode(relay.Node):
name = graphene.String()

@classmethod
def get_node(cls, id):
def get_node(cls, id, info):
pass


def test_works_old_get_node():
class Part(relay.Node):
x = graphene.String()

@classmethod
def get_node(cls, id):
return id

assert Part.get_node(1) == 1


def test_works_old_static_get_node():
class Part(relay.Node):
x = graphene.String()

@staticmethod
def get_node(id):
return id

assert Part.get_node(1) == 1


def test_field_no_contributed_raises_error():
with raises(Exception) as excinfo:
class Part(relay.Node):
Expand Down
26 changes: 24 additions & 2 deletions graphene/relay/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import inspect
import warnings
from functools import wraps
from graphql_relay.node.node import to_global_id

from ..core.types import (Boolean, Field, InputObjectType, Interface, List,
Expand Down Expand Up @@ -73,8 +76,27 @@ class BaseNode(object):
def _prepare_class(cls):
from graphene.relay.utils import is_node
if is_node(cls):
assert hasattr(
cls, 'get_node'), 'get_node classmethod not found in %s Node' % cls
get_node = getattr(cls, 'get_node')
assert get_node, 'get_node classmethod not found in %s Node' % cls
assert callable(get_node), 'get_node have to be callable'
args = 3
if isinstance(get_node, staticmethod):
args -= 1

get_node_num_args = len(inspect.getargspec(get_node).args)
if get_node_num_args < args:
warnings.warn("get_node will receive also the info arg"
" in future versions of graphene".format(cls.__name__),
FutureWarning)

@staticmethod
@wraps(get_node)
def wrapped_node(*node_args):
if len(node_args) < args:
node_args += (None, )
return get_node(*node_args[:-1])

setattr(cls, 'get_node', wrapped_node)

def to_global_id(self):
type_name = self._meta.type_name
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run_tests(self):

setup(
name='graphene',
version='0.4.0.1',
version='0.4.1.1',

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

0 comments on commit b1e0c3b

Please sign in to comment.