Skip to content

Commit 54cc6a4

Browse files
authored
Enforce NonNull for returned related Sets and their content (#690)
* Enforce NonNull for returned related Sets and their content. #448 * Run format. * Remove duplicate assertion
1 parent e2e496f commit 54cc6a4

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

graphene_django/converter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ def dynamic_type():
198198

199199
return DjangoConnectionField(_type, description=description)
200200

201-
return DjangoListField(_type, description=description)
201+
return DjangoListField(
202+
_type,
203+
required=True, # A Set is always returned, never None.
204+
description=description,
205+
)
202206

203207
return Dynamic(dynamic_type)
204208

graphene_django/fields.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
class DjangoListField(Field):
1717
def __init__(self, _type, *args, **kwargs):
18-
super(DjangoListField, self).__init__(List(_type), *args, **kwargs)
18+
# Django would never return a Set of None vvvvvvv
19+
super(DjangoListField, self).__init__(List(NonNull(_type)), *args, **kwargs)
1920

2021
@property
2122
def model(self):

graphene_django/tests/test_converter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22
from django.db import models
33
from django.utils.translation import ugettext_lazy as _
4+
from graphene import NonNull
45
from py.test import raises
56

67
import graphene
@@ -234,8 +235,12 @@ class Meta:
234235
assert isinstance(graphene_field, graphene.Dynamic)
235236
dynamic_field = graphene_field.get_type()
236237
assert isinstance(dynamic_field, graphene.Field)
237-
assert isinstance(dynamic_field.type, graphene.List)
238-
assert dynamic_field.type.of_type == A
238+
# A NonNull List of NonNull A ([A!]!)
239+
# https://github.com/graphql-python/graphene-django/issues/448
240+
assert isinstance(dynamic_field.type, NonNull)
241+
assert isinstance(dynamic_field.type.of_type, graphene.List)
242+
assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
243+
assert dynamic_field.type.of_type.of_type.of_type == A
239244

240245

241246
def test_should_manytomany_convert_connectionorlist_connection():
@@ -262,8 +267,11 @@ class Meta:
262267
assert isinstance(graphene_field, graphene.Dynamic)
263268
dynamic_field = graphene_field.get_type()
264269
assert isinstance(dynamic_field, graphene.Field)
265-
assert isinstance(dynamic_field.type, graphene.List)
266-
assert dynamic_field.type.of_type == A
270+
# a NonNull List of NonNull A ([A!]!)
271+
assert isinstance(dynamic_field.type, NonNull)
272+
assert isinstance(dynamic_field.type.of_type, graphene.List)
273+
assert isinstance(dynamic_field.type.of_type.of_type, NonNull)
274+
assert dynamic_field.type.of_type.of_type.of_type == A
267275

268276

269277
def test_should_onetoone_reverse_convert_model():

graphene_django/tests/test_types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def test_schema_representation():
170170
firstName: String!
171171
lastName: String!
172172
email: String!
173-
pets: [Reporter]
173+
pets: [Reporter!]!
174174
aChoice: ReporterAChoice!
175175
reporterType: ReporterReporterType
176176
articles(before: String, after: String, first: Int, last: Int): ArticleConnection

0 commit comments

Comments
 (0)