Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(converter): wrap field with NonNull if it is required #545

Merged
merged 2 commits into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,14 @@ 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)
Expand All @@ -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 = 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)
22 changes: 21 additions & 1 deletion graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -321,6 +329,17 @@ 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


Expand All @@ -341,7 +360,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):
Expand Down