Allow string references in DjangoListField (#885)

* Allow passing string references to DjangoListField

* Refactor logic to work with string imports
This commit is contained in:
Jonathan Kim 2020-03-13 10:05:35 +00:00 committed by GitHub
parent 348fcf37a0
commit c8a56f8857
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 8 deletions

View File

@ -1,5 +1,6 @@
from functools import partial
import six
from django.db.models.query import QuerySet
from graphql_relay.connection.arrayconnection import connection_from_list_slice
from promise import Promise
@ -19,19 +20,23 @@ class DjangoListField(Field):
if isinstance(_type, NonNull):
_type = _type.of_type
assert issubclass(
_type, DjangoObjectType
), "DjangoListField only accepts DjangoObjectType types"
# Django would never return a Set of None vvvvvvv
super(DjangoListField, self).__init__(List(NonNull(_type)), *args, **kwargs)
assert issubclass(
self._underlying_type, DjangoObjectType
), "DjangoListField only accepts DjangoObjectType types"
@property
def _underlying_type(self):
_type = self._type
while hasattr(_type, "of_type"):
_type = _type.of_type
return _type
@property
def model(self):
_type = self.type.of_type
if isinstance(_type, NonNull):
_type = _type.of_type
return _type._meta.model
return self._underlying_type._meta.model
@staticmethod
def list_resolver(django_object_type, resolver, root, info, **args):

View File

@ -19,6 +19,12 @@ class TestDjangoListField:
with pytest.raises(AssertionError):
list_field = DjangoListField(TestType)
def test_only_import_paths(self):
list_field = DjangoListField("graphene_django.tests.schema.Human")
from .schema import Human
assert list_field._type.of_type.of_type is Human
def test_non_null_type(self):
class Reporter(DjangoObjectType):
class Meta: