Refactor logic to work with string imports

This commit is contained in:
Jonathan Kim 2020-02-23 09:45:11 +00:00
parent 38a0d4c4f2
commit a516c5dc0c
2 changed files with 16 additions and 11 deletions

View File

@ -20,20 +20,23 @@ class DjangoListField(Field):
if isinstance(_type, NonNull): if isinstance(_type, NonNull):
_type = _type.of_type _type = _type.of_type
if not isinstance(_type, six.string_types):
assert issubclass(
_type, DjangoObjectType
), "DjangoListField only accepts DjangoObjectType types"
# Django would never return a Set of None vvvvvvv # Django would never return a Set of None vvvvvvv
super(DjangoListField, self).__init__(List(NonNull(_type)), *args, **kwargs) 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 @property
def model(self): def model(self):
_type = self.type.of_type return self._underlying_type._meta.model
if isinstance(_type, NonNull):
_type = _type.of_type
return _type._meta.model
@staticmethod @staticmethod
def list_resolver(django_object_type, resolver, root, info, **args): def list_resolver(django_object_type, resolver, root, info, **args):

View File

@ -20,8 +20,10 @@ class TestDjangoListField:
list_field = DjangoListField(TestType) list_field = DjangoListField(TestType)
def test_only_import_paths(self): def test_only_import_paths(self):
list_field = DjangoListField("graphene_django.tests.models.Reporter") list_field = DjangoListField("graphene_django.tests.schema.Human")
assert list_field._type.of_type.of_type is ReporterModel from .schema import Human
assert list_field._type.of_type.of_type is Human
def test_non_null_type(self): def test_non_null_type(self):
class Reporter(DjangoObjectType): class Reporter(DjangoObjectType):