Skip to content

Commit 437b263

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

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ def test_should_postgres_array_convert_list():
268268
)
269269
assert isinstance(field.type, graphene.NonNull)
270270
assert isinstance(field.type.of_type, graphene.List)
271+
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
272+
assert field.type.of_type.of_type.of_type == graphene.String
273+
274+
field = assert_conversion(
275+
ArrayField, graphene.List, models.CharField(max_length=100, null=True)
276+
)
277+
assert isinstance(field.type, graphene.NonNull)
278+
assert isinstance(field.type.of_type, graphene.List)
271279
assert field.type.of_type.of_type == graphene.String
272280

273281

@@ -279,6 +287,15 @@ def test_should_postgres_array_multiple_convert_list():
279287
assert isinstance(field.type, graphene.NonNull)
280288
assert isinstance(field.type.of_type, graphene.List)
281289
assert isinstance(field.type.of_type.of_type, graphene.List)
290+
assert isinstance(field.type.of_type.of_type.of_type, graphene.NonNull)
291+
assert field.type.of_type.of_type.of_type.of_type == graphene.String
292+
293+
field = assert_conversion(
294+
ArrayField, graphene.List, ArrayField(models.CharField(max_length=100, null=True))
295+
)
296+
assert isinstance(field.type, graphene.NonNull)
297+
assert isinstance(field.type.of_type, graphene.List)
298+
assert isinstance(field.type.of_type.of_type, graphene.List)
282299
assert field.type.of_type.of_type.of_type == graphene.String
283300

284301

@@ -299,4 +316,5 @@ def test_should_postgres_range_convert_list():
299316
field = assert_conversion(IntegerRangeField, graphene.List)
300317
assert isinstance(field.type, graphene.NonNull)
301318
assert isinstance(field.type.of_type, graphene.List)
302-
assert field.type.of_type.of_type == graphene.Int
319+
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
320+
assert field.type.of_type.of_type.of_type == graphene.Int

0 commit comments

Comments
 (0)