Skip to content

Commit bc3d9e5

Browse files
committed
fix(converter): wrap field with NonNull if it is required
#536
1 parent f76f38e commit bc3d9e5

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

graphene_django/converter.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,10 @@ def dynamic_type():
211211

212212
@convert_django_field.register(ArrayField)
213213
def convert_postgres_array_to_list(field, registry=None):
214-
base_type = convert_django_field(field.base_field)
215-
if not isinstance(base_type, (List, NonNull)):
216-
base_type = type(base_type)
217-
return List(base_type, description=field.help_text, required=not field.null)
214+
inner_type = convert_django_field(field.base_field)
215+
if not isinstance(inner_type, (List, NonNull)):
216+
inner_type = NonNull(type(inner_type)) if inner_type.kwargs['required'] else type(inner_type)
217+
return List(inner_type, description=field.help_text, required=not field.null)
218218

219219

220220
@convert_django_field.register(HStoreField)
@@ -227,5 +227,5 @@ def convert_posgres_field_to_string(field, registry=None):
227227
def convert_posgres_range_to_string(field, registry=None):
228228
inner_type = convert_django_field(field.base_field)
229229
if not isinstance(inner_type, (List, NonNull)):
230-
inner_type = type(inner_type)
230+
inner_type = NonNull(type(inner_type)) if inner_type.kwargs['required'] else type(inner_type)
231231
return List(inner_type, description=field.help_text, required=not field.null)

graphene_django/tests/test_converter.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class A(DjangoObjectType):
241241
class Meta:
242242
model = Article
243243

244-
graphene_field = convert_django_field(Reporter.articles.rel,
244+
graphene_field = convert_django_field(Reporter.articles.rel,
245245
A._meta.registry)
246246
assert isinstance(graphene_field, graphene.Dynamic)
247247
dynamic_field = graphene_field.get_type()
@@ -270,6 +270,14 @@ def test_should_postgres_array_convert_list():
270270
)
271271
assert isinstance(field.type, graphene.NonNull)
272272
assert isinstance(field.type.of_type, graphene.List)
273+
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
274+
assert field.type.of_type.of_type.of_type == graphene.String
275+
276+
field = assert_conversion(
277+
ArrayField, graphene.List, models.CharField(max_length=100, null=True)
278+
)
279+
assert isinstance(field.type, graphene.NonNull)
280+
assert isinstance(field.type.of_type, graphene.List)
273281
assert field.type.of_type.of_type == graphene.String
274282

275283

@@ -281,6 +289,15 @@ def test_should_postgres_array_multiple_convert_list():
281289
assert isinstance(field.type, graphene.NonNull)
282290
assert isinstance(field.type.of_type, graphene.List)
283291
assert isinstance(field.type.of_type.of_type, graphene.List)
292+
assert isinstance(field.type.of_type.of_type.of_type, graphene.NonNull)
293+
assert field.type.of_type.of_type.of_type.of_type == graphene.String
294+
295+
field = assert_conversion(
296+
ArrayField, graphene.List, ArrayField(models.CharField(max_length=100, null=True))
297+
)
298+
assert isinstance(field.type, graphene.NonNull)
299+
assert isinstance(field.type.of_type, graphene.List)
300+
assert isinstance(field.type.of_type.of_type, graphene.List)
284301
assert field.type.of_type.of_type.of_type == graphene.String
285302

286303

@@ -301,4 +318,5 @@ def test_should_postgres_range_convert_list():
301318
field = assert_conversion(IntegerRangeField, graphene.List)
302319
assert isinstance(field.type, graphene.NonNull)
303320
assert isinstance(field.type.of_type, graphene.List)
304-
assert field.type.of_type.of_type == graphene.Int
321+
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
322+
assert field.type.of_type.of_type.of_type == graphene.Int

0 commit comments

Comments
 (0)