mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-02-04 13:40:35 +03:00
Added support for SimpleLazyObject. Fixed #22
This commit is contained in:
parent
8136223bb1
commit
d73f4aa235
|
@ -2,6 +2,7 @@ import datetime
|
|||
|
||||
import pytest
|
||||
from django.db import models
|
||||
from django.utils.functional import SimpleLazyObject
|
||||
from py.test import raises
|
||||
|
||||
import graphene
|
||||
|
@ -33,6 +34,37 @@ def test_should_query_only_fields():
|
|||
assert not result.errors
|
||||
|
||||
|
||||
def test_should_query_simplelazy_objects():
|
||||
class ReporterType(DjangoObjectType):
|
||||
|
||||
class Meta:
|
||||
model = Reporter
|
||||
only_fields = ('id', )
|
||||
|
||||
|
||||
class Query(graphene.ObjectType):
|
||||
reporter = graphene.Field(ReporterType)
|
||||
|
||||
def resolve_reporter(self, args, context, info):
|
||||
return SimpleLazyObject(lambda: Reporter(id=1))
|
||||
|
||||
schema = graphene.Schema(query=Query)
|
||||
query = '''
|
||||
query {
|
||||
reporter {
|
||||
id
|
||||
}
|
||||
}
|
||||
'''
|
||||
result = schema.execute(query)
|
||||
assert not result.errors
|
||||
assert result.data == {
|
||||
'reporter': {
|
||||
'id': '1'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def test_should_query_well():
|
||||
class ReporterType(DjangoObjectType):
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@ from collections import OrderedDict
|
|||
|
||||
import six
|
||||
|
||||
from django.utils.functional import SimpleLazyObject
|
||||
from graphene import Field, ObjectType
|
||||
from graphene.types.objecttype import ObjectTypeMeta
|
||||
from graphene.types.options import Options
|
||||
|
@ -103,6 +104,9 @@ class DjangoObjectType(six.with_metaclass(DjangoObjectTypeMeta, ObjectType)):
|
|||
|
||||
@classmethod
|
||||
def is_type_of(cls, root, context, info):
|
||||
if isinstance(root, SimpleLazyObject):
|
||||
root._setup()
|
||||
root = root._wrapped
|
||||
if isinstance(root, cls):
|
||||
return True
|
||||
if not is_valid_django_model(type(root)):
|
||||
|
|
Loading…
Reference in New Issue
Block a user