Skip to content

Commit

Permalink
fix(converter): wrap field with NonNull if it is required
Browse files Browse the repository at this point in the history
  • Loading branch information
helloqiu committed Oct 24, 2018
1 parent f76f38e commit bc3d9e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 5 additions & 5 deletions graphene_django/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,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)
Expand All @@ -227,5 +227,5 @@ def convert_posgres_field_to_string(field, registry=None):
def convert_posgres_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: 20 additions & 2 deletions graphene_django/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class A(DjangoObjectType):
class Meta:
model = Article

graphene_field = convert_django_field(Reporter.articles.rel,
graphene_field = convert_django_field(Reporter.articles.rel,
A._meta.registry)
assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type()
Expand Down Expand Up @@ -270,6 +270,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 @@ -281,6 +289,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


Expand All @@ -301,4 +318,5 @@ 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

0 comments on commit bc3d9e5

Please sign in to comment.