Skip to content

Commit d73f4aa

Browse files
committed
Added support for SimpleLazyObject. Fixed #22
1 parent 8136223 commit d73f4aa

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

graphene_django/tests/test_query.py

+32
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import pytest
44
from django.db import models
5+
from django.utils.functional import SimpleLazyObject
56
from py.test import raises
67

78
import graphene
@@ -33,6 +34,37 @@ class Meta:
3334
assert not result.errors
3435

3536

37+
def test_should_query_simplelazy_objects():
38+
class ReporterType(DjangoObjectType):
39+
40+
class Meta:
41+
model = Reporter
42+
only_fields = ('id', )
43+
44+
45+
class Query(graphene.ObjectType):
46+
reporter = graphene.Field(ReporterType)
47+
48+
def resolve_reporter(self, args, context, info):
49+
return SimpleLazyObject(lambda: Reporter(id=1))
50+
51+
schema = graphene.Schema(query=Query)
52+
query = '''
53+
query {
54+
reporter {
55+
id
56+
}
57+
}
58+
'''
59+
result = schema.execute(query)
60+
assert not result.errors
61+
assert result.data == {
62+
'reporter': {
63+
'id': '1'
64+
}
65+
}
66+
67+
3668
def test_should_query_well():
3769
class ReporterType(DjangoObjectType):
3870

graphene_django/types.py

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import six
44

5+
from django.utils.functional import SimpleLazyObject
56
from graphene import Field, ObjectType
67
from graphene.types.objecttype import ObjectTypeMeta
78
from graphene.types.options import Options
@@ -103,6 +104,9 @@ def resolve_id(self, args, context, info):
103104

104105
@classmethod
105106
def is_type_of(cls, root, context, info):
107+
if isinstance(root, SimpleLazyObject):
108+
root._setup()
109+
root = root._wrapped
106110
if isinstance(root, cls):
107111
return True
108112
if not is_valid_django_model(type(root)):

0 commit comments

Comments
 (0)