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

#536
This commit is contained in:
helloqiu 2018-10-24 18:08:50 +08:00 committed by Jonathan Kim
parent 56f1db80cf
commit bf61399be3
2 changed files with 24 additions and 6 deletions

View File

@ -255,10 +255,10 @@ def convert_field_to_djangomodel(field, registry=None):
@convert_django_field.register(ArrayField) @convert_django_field.register(ArrayField)
def convert_postgres_array_to_list(field, registry=None): def convert_postgres_array_to_list(field, registry=None):
base_type = convert_django_field(field.base_field) inner_type = convert_django_field(field.base_field)
if not isinstance(base_type, (List, NonNull)): if not isinstance(inner_type, (List, NonNull)):
base_type = type(base_type) inner_type = NonNull(type(inner_type)) if inner_type.kwargs['required'] else type(inner_type)
return List(base_type, description=field.help_text, required=not field.null) return List(inner_type, description=field.help_text, required=not field.null)
@convert_django_field.register(HStoreField) @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): def convert_postgres_range_to_string(field, registry=None):
inner_type = convert_django_field(field.base_field) inner_type = convert_django_field(field.base_field)
if not isinstance(inner_type, (List, NonNull)): 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) return List(inner_type, description=field.help_text, required=not field.null)

View File

@ -310,6 +310,14 @@ def test_should_postgres_array_convert_list():
) )
assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type, graphene.NonNull)
assert isinstance(field.type.of_type, graphene.List) 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 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, graphene.NonNull)
assert isinstance(field.type.of_type, graphene.List) 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, 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 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) field = assert_conversion(IntegerRangeField, graphene.List)
assert isinstance(field.type, graphene.NonNull) assert isinstance(field.type, graphene.NonNull)
assert isinstance(field.type.of_type, graphene.List) 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): def test_generate_enum_name(graphene_settings):