Skip to content

Commit b85177c

Browse files
authored
fix: same type list (#1492)
* fix: same type list * chore: improve test
1 parent 4d0484f commit b85177c

File tree

3 files changed

+82
-7
lines changed

3 files changed

+82
-7
lines changed

Diff for: graphene_django/fields.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,20 @@
2020

2121
class DjangoListField(Field):
2222
def __init__(self, _type, *args, **kwargs):
23-
from .types import DjangoObjectType
24-
2523
if isinstance(_type, NonNull):
2624
_type = _type.of_type
2725

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

29+
@property
30+
def type(self):
31+
from .types import DjangoObjectType
32+
3133
assert issubclass(
3234
self._underlying_type, DjangoObjectType
33-
), "DjangoListField only accepts DjangoObjectType types"
35+
), "DjangoListField only accepts DjangoObjectType types as underlying type"
36+
return super().type
3437

3538
@property
3639
def _underlying_type(self):

Diff for: graphene_django/tests/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
class Person(models.Model):
88
name = models.CharField(max_length=30)
9+
parent = models.ForeignKey(
10+
"self", on_delete=models.CASCADE, null=True, blank=True, related_name="children"
11+
)
912

1013

1114
class Pet(models.Model):

Diff for: graphene_django/tests/test_fields.py

+73-4
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@
1212
Article as ArticleModel,
1313
Film as FilmModel,
1414
FilmDetails as FilmDetailsModel,
15+
Person as PersonModel,
1516
Reporter as ReporterModel,
1617
)
1718

1819

1920
class TestDjangoListField:
2021
def test_only_django_object_types(self):
21-
class TestType(ObjectType):
22-
foo = String()
22+
class Query(ObjectType):
23+
something = DjangoListField(String)
24+
25+
with pytest.raises(TypeError) as excinfo:
26+
Schema(query=Query)
2327

24-
with pytest.raises(AssertionError):
25-
DjangoListField(TestType)
28+
assert (
29+
"Query fields cannot be resolved. DjangoListField only accepts DjangoObjectType types as underlying type"
30+
in str(excinfo.value)
31+
)
2632

2733
def test_only_import_paths(self):
2834
list_field = DjangoListField("graphene_django.tests.schema.Human")
@@ -262,6 +268,69 @@ class Query(ObjectType):
262268
]
263269
}
264270

271+
def test_same_type_nested_list_field(self):
272+
class Person(DjangoObjectType):
273+
class Meta:
274+
model = PersonModel
275+
fields = ("name", "parent")
276+
277+
children = DjangoListField(lambda: Person)
278+
279+
class Query(ObjectType):
280+
persons = DjangoListField(Person)
281+
282+
schema = Schema(query=Query)
283+
284+
query = """
285+
query {
286+
persons {
287+
name
288+
children {
289+
name
290+
}
291+
}
292+
}
293+
"""
294+
295+
p1 = PersonModel.objects.create(name="Tara")
296+
PersonModel.objects.create(name="Debra")
297+
298+
PersonModel.objects.create(
299+
name="Toto",
300+
parent=p1,
301+
)
302+
PersonModel.objects.create(
303+
name="Tata",
304+
parent=p1,
305+
)
306+
307+
result = schema.execute(query)
308+
309+
assert not result.errors
310+
assert result.data == {
311+
"persons": [
312+
{
313+
"name": "Tara",
314+
"children": [
315+
{"name": "Toto"},
316+
{"name": "Tata"},
317+
],
318+
},
319+
{
320+
"name": "Debra",
321+
"children": [],
322+
},
323+
{
324+
"name": "Toto",
325+
"children": [],
326+
},
327+
{
328+
"name": "Tata",
329+
"children": [],
330+
},
331+
]
332+
}
333+
265334
def test_get_queryset_filter(self):
266335
class Reporter(DjangoObjectType):
267336
class Meta:

0 commit comments

Comments
 (0)