From d73f4aa23581d7604cd92a80e8890ad490170094 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Sat, 15 Oct 2016 17:40:12 -0700 Subject: [PATCH] Added support for SimpleLazyObject. Fixed #22 --- graphene_django/tests/test_query.py | 32 +++++++++++++++++++++++++++++ graphene_django/types.py | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/graphene_django/tests/test_query.py b/graphene_django/tests/test_query.py index 326d4d5..9c31243 100644 --- a/graphene_django/tests/test_query.py +++ b/graphene_django/tests/test_query.py @@ -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): diff --git a/graphene_django/types.py b/graphene_django/types.py index 190088a..ae2dc18 100644 --- a/graphene_django/types.py +++ b/graphene_django/types.py @@ -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)):