Wrap or not wrap. Root is the question

This commit is contained in:
Syrus Akbary 2015-11-19 08:37:03 -08:00
parent 1795ed516c
commit 4a591354f1
5 changed files with 50 additions and 12 deletions

View File

@ -1,3 +1,4 @@
from pytest import raises
from graphql.core.type import GraphQLInterfaceType, GraphQLObjectType
from mock import patch
@ -33,10 +34,11 @@ def test_django_interface():
assert DjangoNode._meta.is_interface is True
@patch('graphene.contrib.django.tests.models.Article.objects.filter')
def test_django_get_node(objects):
Human.get_node(1)
objects.assert_called_with(id=1)
@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)
get.assert_called_with(id=1)
assert human.id == 1
def test_pseudo_interface_registered():
@ -67,10 +69,26 @@ def test_node_replacedfield():
def test_interface_resolve_type():
resolve_type = Character.resolve_type(schema, Human(object()))
resolve_type = Character.resolve_type(schema, Human())
assert isinstance(resolve_type, GraphQLObjectType)
def test_interface_objecttype_init_none():
h = Human()
assert h._root is None
def test_interface_objecttype_init_good():
instance = Article()
h = Human(instance)
assert h._root == instance
def test_interface_objecttype_init_unexpected():
with raises(AssertionError) as excinfo:
Human(object())
assert str(excinfo.value) == "Human received a non-compatible instance (object) when expecting Article"
def test_object_type():
object_type = schema.T(Human)
Human._meta.fields_map

View File

@ -38,12 +38,19 @@ class DjangoObjectTypeMeta(ObjectTypeMeta):
class InstanceObjectType(BaseObjectType):
def __init__(self, instance=None):
self.instance = instance
super(InstanceObjectType, self).__init__()
def __init__(self, _root=None):
if _root:
assert isinstance(_root, self._meta.model), (
'{} received a non-compatible instance ({}) '
'when expecting {}'.format(
self.__class__.__name__,
_root.__class__.__name__,
self._meta.model.__name__
))
super(InstanceObjectType, self).__init__(_root=_root)
def __getattr__(self, attr):
return getattr(self.instance, attr)
return getattr(self._root, attr)
class DjangoObjectType(six.with_metaclass(
@ -61,5 +68,8 @@ class DjangoNode(BaseNode, DjangoInterface):
@classmethod
def get_node(cls, id):
instance = cls._meta.model.objects.filter(id=id).first()
return cls(instance)
try:
instance = cls._meta.model.objects.get(id=id)
return cls(instance)
except cls._meta.model.DoesNotExist:
return None

View File

@ -113,7 +113,7 @@ class Schema(object):
return self.executor.execute(
self.schema,
request,
root=self.query(root),
root=root,
args=vars,
operation_name=operation_name,
**kwargs

View File

@ -1,4 +1,5 @@
from collections import OrderedDict
from functools import wraps
import six
from graphql.core.type import GraphQLField, GraphQLInputObjectField
@ -76,6 +77,14 @@ class Field(OrderedType):
assert len(arguments) == 0
arguments = type_objecttype.get_arguments()
resolver = getattr(type_objecttype, 'mutate')
else:
my_resolver = resolver
@wraps(my_resolver)
def wrapped_func(instance, args, info):
if not isinstance(instance, self.object_type):
instance = self.object_type(_root=instance)
return my_resolver(instance, args, info)
resolver = wrapped_func
resolver = snake_case_args(resolver)
assert type, 'Internal type for field %s is None' % str(self)

View File

@ -144,6 +144,7 @@ class BaseObjectType(BaseType):
def __init__(self, *args, **kwargs):
signals.pre_init.send(self.__class__, args=args, kwargs=kwargs)
self._root = kwargs.pop('_root', None)
args_len = len(args)
fields = self._meta.fields
if args_len > len(fields):