Skip to content

Commit 48bfc39

Browse files
helloqiujkimbo
andauthored
fix(converter): wrap field with NonNull if it is required (#545)
Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
1 parent 56f1db8 commit 48bfc39

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

graphene_django/converter.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,14 @@ def dynamic_type():
255255

256256
@convert_django_field.register(ArrayField)
257257
def convert_postgres_array_to_list(field, registry=None):
258-
base_type = convert_django_field(field.base_field)
259-
if not isinstance(base_type, (List, NonNull)):
260-
base_type = type(base_type)
261-
return List(base_type, description=field.help_text, required=not field.null)
258+
inner_type = convert_django_field(field.base_field)
259+
if not isinstance(inner_type, (List, NonNull)):
260+
inner_type = (
261+
NonNull(type(inner_type))
262+
if inner_type.kwargs["required"]
263+
else type(inner_type)
264+
)
265+
return List(inner_type, description=field.help_text, required=not field.null)
262266

263267

264268
@convert_django_field.register(HStoreField)
@@ -271,5 +275,9 @@ def convert_postgres_field_to_string(field, registry=None):
271275
def convert_postgres_range_to_string(field, registry=None):
272276
inner_type = convert_django_field(field.base_field)
273277
if not isinstance(inner_type, (List, NonNull)):
274-
inner_type = type(inner_type)
278+
inner_type = (
279+
NonNull(type(inner_type))
280+
if inner_type.kwargs["required"]
281+
else type(inner_type)
282+
)
275283
return List(inner_type, description=field.help_text, required=not field.null)

graphene_django/tests/test_converter.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,14 @@ def test_should_postgres_array_convert_list():
310310
)
311311
assert isinstance(field.type, graphene.NonNull)
312312
assert isinstance(field.type.of_type, graphene.List)
313+
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
314+
assert field.type.of_type.of_type.of_type == graphene.String
315+
316+
field = assert_conversion(
317+
ArrayField, graphene.List, models.CharField(max_length=100, null=True)
318+
)
319+
assert isinstance(field.type, graphene.NonNull)
320+
assert isinstance(field.type.of_type, graphene.List)
313321
assert field.type.of_type.of_type == graphene.String
314322

315323

@@ -321,6 +329,17 @@ def test_should_postgres_array_multiple_convert_list():
321329
assert isinstance(field.type, graphene.NonNull)
322330
assert isinstance(field.type.of_type, graphene.List)
323331
assert isinstance(field.type.of_type.of_type, graphene.List)
332+
assert isinstance(field.type.of_type.of_type.of_type, graphene.NonNull)
333+
assert field.type.of_type.of_type.of_type.of_type == graphene.String
334+
335+
field = assert_conversion(
336+
ArrayField,
337+
graphene.List,
338+
ArrayField(models.CharField(max_length=100, null=True)),
339+
)
340+
assert isinstance(field.type, graphene.NonNull)
341+
assert isinstance(field.type.of_type, graphene.List)
342+
assert isinstance(field.type.of_type.of_type, graphene.List)
324343
assert field.type.of_type.of_type.of_type == graphene.String
325344

326345

@@ -341,7 +360,8 @@ def test_should_postgres_range_convert_list():
341360
field = assert_conversion(IntegerRangeField, graphene.List)
342361
assert isinstance(field.type, graphene.NonNull)
343362
assert isinstance(field.type.of_type, graphene.List)
344-
assert field.type.of_type.of_type == graphene.Int
363+
assert isinstance(field.type.of_type.of_type, graphene.NonNull)
364+
assert field.type.of_type.of_type.of_type == graphene.Int
345365

346366

347367
def test_generate_enum_name(graphene_settings):

0 commit comments

Comments
 (0)