Skip to content

Commit

Permalink
fix: same type list (#46)
Browse files Browse the repository at this point in the history
* fix: same type list
  • Loading branch information
superlevure authored Jan 18, 2024
1 parent 7160cb1 commit 4969268
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
22 changes: 14 additions & 8 deletions graphene_django/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,20 @@

class DjangoListField(Field):
def __init__(self, _type, *args, **kwargs):
from .types import DjangoObjectType

if isinstance(_type, NonNull):
_type = _type.of_type

# Django would never return a Set of None
super().__init__(List(NonNull(_type)), *args, **kwargs)

@property
def type(self):
from .types import DjangoObjectType

assert issubclass(
self._underlying_type, DjangoObjectType
), "DjangoListField only accepts DjangoObjectType types"
), "DjangoListField only accepts DjangoObjectType types as underlying type"
return super().type

@property
def _underlying_type(self):
Expand Down Expand Up @@ -92,19 +95,22 @@ def __init__(
*args,
**kwargs,
):
from graphene_django.types import DjangoObjectType

if isinstance(_type, NonNull):
_type = _type.of_type

# Django would never return a Set of None
super().__init__(List(NonNull(_type)), *args, **kwargs)

self._field = field

@property
def type(self):
from .types import DjangoObjectType

assert issubclass(
self._underlying_type, DjangoObjectType
), "DjangoListField only accepts DjangoObjectType types"

self._field = field
), "DjangoDataloadedListField only accepts DjangoObjectType types as underlying type"
return super().type

@property
def _underlying_type(self):
Expand Down
3 changes: 3 additions & 0 deletions graphene_django/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

class Person(models.Model):
name = models.CharField(max_length=30)
parent = models.ForeignKey(
"self", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
)


class Pet(models.Model):
Expand Down
72 changes: 68 additions & 4 deletions graphene_django/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
Article as ArticleModel,
Film as FilmModel,
FilmDetails as FilmDetailsModel,
Person as PersonModel,
Reporter as ReporterModel,
)


class TestDjangoListField:
def test_only_django_object_types(self):
class TestType(ObjectType):
foo = String()
class Query(ObjectType):
something = DjangoListField(String)

with pytest.raises(AssertionError):
DjangoListField(TestType)
with pytest.raises(TypeError):
Schema(query=Query)

def test_only_import_paths(self):
list_field = DjangoListField("graphene_django.tests.schema.Human")
Expand Down Expand Up @@ -263,6 +264,69 @@ class Query(ObjectType):
]
}

def test_same_type_nested_list_field(self):
class Person(DjangoObjectType):
class Meta:
model = PersonModel
fields = ("name", "parent")

children = DjangoListField(lambda: Person)

class Query(ObjectType):
persons = DjangoListField(Person)

schema = Schema(query=Query)

query = """
query {
persons {
name
children {
name
}
}
}
"""

p1 = PersonModel.objects.create(name="Tara")
PersonModel.objects.create(name="Debra")

PersonModel.objects.create(
name="Toto",
parent=p1,
)
PersonModel.objects.create(
name="Tata",
parent=p1,
)

result = schema.execute(query)

assert not result.errors
assert result.data == {
"persons": [
{
"name": "Tara",
"children": [
{"name": "Toto"},
{"name": "Tata"},
],
},
{
"name": "Debra",
"children": [],
},
{
"name": "Toto",
"children": [],
},
{
"name": "Tata",
"children": [],
},
]
}

def test_get_queryset_filter(self):
class Reporter(DjangoObjectType):
class Meta:
Expand Down

0 comments on commit 4969268

Please sign in to comment.