From bf61399be32e741a92165462e50f28b76a6d71b9 Mon Sep 17 00:00:00 2001 From: helloqiu Date: Wed, 24 Oct 2018 18:08:50 +0800 Subject: [PATCH 1/2] fix(converter): wrap field with NonNull if it is required #536 --- graphene_django/converter.py | 10 +++++----- graphene_django/tests/test_converter.py | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index c84b61ae1..dcd21f73c 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -255,10 +255,10 @@ def dynamic_type(): @convert_django_field.register(ArrayField) def convert_postgres_array_to_list(field, registry=None): - base_type = convert_django_field(field.base_field) - if not isinstance(base_type, (List, NonNull)): - base_type = type(base_type) - return List(base_type, description=field.help_text, required=not field.null) + inner_type = convert_django_field(field.base_field) + if not isinstance(inner_type, (List, NonNull)): + inner_type = NonNull(type(inner_type)) if inner_type.kwargs['required'] else type(inner_type) + return List(inner_type, description=field.help_text, required=not field.null) @convert_django_field.register(HStoreField) @@ -271,5 +271,5 @@ def convert_postgres_field_to_string(field, registry=None): def convert_postgres_range_to_string(field, registry=None): inner_type = convert_django_field(field.base_field) if not isinstance(inner_type, (List, NonNull)): - inner_type = type(inner_type) + inner_type = NonNull(type(inner_type)) if inner_type.kwargs['required'] else type(inner_type) return List(inner_type, description=field.help_text, required=not field.null) diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index 8e4495ad6..224bc401b 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -310,6 +310,14 @@ def test_should_postgres_array_convert_list(): ) assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type.of_type, graphene.List) + assert isinstance(field.type.of_type.of_type, graphene.NonNull) + assert field.type.of_type.of_type.of_type == graphene.String + + field = assert_conversion( + ArrayField, graphene.List, models.CharField(max_length=100, null=True) + ) + assert isinstance(field.type, graphene.NonNull) + assert isinstance(field.type.of_type, graphene.List) assert field.type.of_type.of_type == graphene.String @@ -321,6 +329,15 @@ def test_should_postgres_array_multiple_convert_list(): assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type.of_type, graphene.List) assert isinstance(field.type.of_type.of_type, graphene.List) + assert isinstance(field.type.of_type.of_type.of_type, graphene.NonNull) + assert field.type.of_type.of_type.of_type.of_type == graphene.String + + field = assert_conversion( + ArrayField, graphene.List, ArrayField(models.CharField(max_length=100, null=True)) + ) + assert isinstance(field.type, graphene.NonNull) + assert isinstance(field.type.of_type, graphene.List) + assert isinstance(field.type.of_type.of_type, graphene.List) assert field.type.of_type.of_type.of_type == graphene.String @@ -341,7 +358,8 @@ def test_should_postgres_range_convert_list(): field = assert_conversion(IntegerRangeField, graphene.List) assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type.of_type, graphene.List) - assert field.type.of_type.of_type == graphene.Int + assert isinstance(field.type.of_type.of_type, graphene.NonNull) + assert field.type.of_type.of_type.of_type == graphene.Int def test_generate_enum_name(graphene_settings): From cea771c493f60b2aab691737bca53d3cabe678d5 Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Wed, 10 Jun 2020 17:49:13 +0100 Subject: [PATCH 2/2] Update formatting --- graphene_django/converter.py | 12 ++++++++++-- graphene_django/tests/test_converter.py | 4 +++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index dcd21f73c..92963d6ee 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -257,7 +257,11 @@ def dynamic_type(): def convert_postgres_array_to_list(field, registry=None): inner_type = convert_django_field(field.base_field) if not isinstance(inner_type, (List, NonNull)): - inner_type = NonNull(type(inner_type)) if inner_type.kwargs['required'] else type(inner_type) + inner_type = ( + NonNull(type(inner_type)) + if inner_type.kwargs["required"] + else type(inner_type) + ) return List(inner_type, description=field.help_text, required=not field.null) @@ -271,5 +275,9 @@ def convert_postgres_field_to_string(field, registry=None): def convert_postgres_range_to_string(field, registry=None): inner_type = convert_django_field(field.base_field) if not isinstance(inner_type, (List, NonNull)): - inner_type = NonNull(type(inner_type)) if inner_type.kwargs['required'] else type(inner_type) + inner_type = ( + NonNull(type(inner_type)) + if inner_type.kwargs["required"] + else type(inner_type) + ) return List(inner_type, description=field.help_text, required=not field.null) diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index 224bc401b..f6e3606da 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -333,7 +333,9 @@ def test_should_postgres_array_multiple_convert_list(): assert field.type.of_type.of_type.of_type.of_type == graphene.String field = assert_conversion( - ArrayField, graphene.List, ArrayField(models.CharField(max_length=100, null=True)) + ArrayField, + graphene.List, + ArrayField(models.CharField(max_length=100, null=True)), ) assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type.of_type, graphene.List)