Added required conversion to model and form fields. Fixed #2

This commit is contained in:
Syrus Akbary 2016-09-22 19:57:28 -07:00
parent 7fbc3e69b1
commit b8f9fec674
4 changed files with 38 additions and 34 deletions

View File

@ -47,7 +47,7 @@ def convert_django_field_with_choices(field, registry=None):
return named_choices_descriptions[self.name] return named_choices_descriptions[self.name]
enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType) enum = Enum(name, list(named_choices), type=EnumWithDescriptionsType)
return enum(description=field.help_text) return enum(description=field.help_text, required=not field.null)
return convert_django_field(field, registry) return convert_django_field(field, registry)
@ -67,12 +67,12 @@ def convert_django_field(field, registry=None):
@convert_django_field.register(models.FileField) @convert_django_field.register(models.FileField)
@convert_django_field.register(UUIDField) @convert_django_field.register(UUIDField)
def convert_field_to_string(field, registry=None): def convert_field_to_string(field, registry=None):
return String(description=field.help_text) return String(description=field.help_text, required=not field.null)
@convert_django_field.register(models.AutoField) @convert_django_field.register(models.AutoField)
def convert_field_to_id(field, registry=None): def convert_field_to_id(field, registry=None):
return ID(description=field.help_text) return ID(description=field.help_text, required=not field.null)
@convert_django_field.register(models.PositiveIntegerField) @convert_django_field.register(models.PositiveIntegerField)
@ -81,7 +81,7 @@ def convert_field_to_id(field, registry=None):
@convert_django_field.register(models.BigIntegerField) @convert_django_field.register(models.BigIntegerField)
@convert_django_field.register(models.IntegerField) @convert_django_field.register(models.IntegerField)
def convert_field_to_int(field, registry=None): def convert_field_to_int(field, registry=None):
return Int(description=field.help_text) return Int(description=field.help_text, required=not field.null)
@convert_django_field.register(models.BooleanField) @convert_django_field.register(models.BooleanField)
@ -91,18 +91,18 @@ def convert_field_to_boolean(field, registry=None):
@convert_django_field.register(models.NullBooleanField) @convert_django_field.register(models.NullBooleanField)
def convert_field_to_nullboolean(field, registry=None): def convert_field_to_nullboolean(field, registry=None):
return Boolean(description=field.help_text) return Boolean(description=field.help_text, required=not field.null)
@convert_django_field.register(models.DecimalField) @convert_django_field.register(models.DecimalField)
@convert_django_field.register(models.FloatField) @convert_django_field.register(models.FloatField)
def convert_field_to_float(field, registry=None): def convert_field_to_float(field, registry=None):
return Float(description=field.help_text) return Float(description=field.help_text, required=not field.null)
@convert_django_field.register(models.DateField) @convert_django_field.register(models.DateField)
def convert_date_to_string(field, registry=None): def convert_date_to_string(field, registry=None):
return DateTime(description=field.help_text) return DateTime(description=field.help_text, required=not field.null)
@convert_django_field.register(models.OneToOneRel) @convert_django_field.register(models.OneToOneRel)
@ -114,7 +114,7 @@ def convert_onetoone_field_to_djangomodel(field, registry=None):
if not _type: if not _type:
return return
return Field(_type) return Field(_type, required=not field.null)
return Dynamic(dynamic_type) return Dynamic(dynamic_type)
@ -149,7 +149,7 @@ def convert_relatedfield_to_djangomodel(field, registry=None):
return return
if isinstance(field.field, models.OneToOneField): if isinstance(field.field, models.OneToOneField):
return Field(_type) return Field(_type, required=not field.field.null)
if is_node(_type): if is_node(_type):
return get_connection_field(_type) return get_connection_field(_type)
@ -168,7 +168,7 @@ def convert_field_to_djangomodel(field, registry=None):
if not _type: if not _type:
return return
return Field(_type, description=field.help_text) return Field(_type, description=field.help_text, required=not field.null)
return Dynamic(dynamic_type) return Dynamic(dynamic_type)
@ -178,13 +178,13 @@ def convert_postgres_array_to_list(field, registry=None):
base_type = convert_django_field(field.base_field) base_type = convert_django_field(field.base_field)
if not isinstance(base_type, (List, NonNull)): if not isinstance(base_type, (List, NonNull)):
base_type = type(base_type) base_type = type(base_type)
return List(base_type, description=field.help_text) return List(base_type, description=field.help_text, required=not field.null)
@convert_django_field.register(HStoreField) @convert_django_field.register(HStoreField)
@convert_django_field.register(JSONField) @convert_django_field.register(JSONField)
def convert_posgres_field_to_string(field, registry=None): def convert_posgres_field_to_string(field, registry=None):
return JSONString(description=field.help_text) return JSONString(description=field.help_text, required=not field.null)
@convert_django_field.register(RangeField) @convert_django_field.register(RangeField)
@ -192,4 +192,4 @@ def convert_posgres_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 = type(inner_type)
return List(inner_type, description=field.help_text) return List(inner_type, description=field.help_text, required=not field.null)

View File

@ -34,13 +34,13 @@ def convert_form_field(field):
@convert_form_field.register(forms.Field) @convert_form_field.register(forms.Field)
@convert_form_field.register(UUIDField) @convert_form_field.register(UUIDField)
def convert_form_field_to_string(field): def convert_form_field_to_string(field):
return String(description=field.help_text) return String(description=field.help_text, required=field.required)
@convert_form_field.register(forms.IntegerField) @convert_form_field.register(forms.IntegerField)
@convert_form_field.register(forms.NumberInput) @convert_form_field.register(forms.NumberInput)
def convert_form_field_to_int(field): def convert_form_field_to_int(field):
return Int(description=field.help_text) return Int(description=field.help_text, required=field.required)
@convert_form_field.register(forms.BooleanField) @convert_form_field.register(forms.BooleanField)
@ -56,16 +56,16 @@ def convert_form_field_to_nullboolean(field):
@convert_form_field.register(forms.DecimalField) @convert_form_field.register(forms.DecimalField)
@convert_form_field.register(forms.FloatField) @convert_form_field.register(forms.FloatField)
def convert_form_field_to_float(field): def convert_form_field_to_float(field):
return Float(description=field.help_text) return Float(description=field.help_text, required=field.required)
@convert_form_field.register(forms.ModelMultipleChoiceField) @convert_form_field.register(forms.ModelMultipleChoiceField)
@convert_form_field.register(GlobalIDMultipleChoiceField) @convert_form_field.register(GlobalIDMultipleChoiceField)
def convert_form_field_to_list(field): def convert_form_field_to_list(field):
return List(ID) return List(ID, required=field.required)
@convert_form_field.register(forms.ModelChoiceField) @convert_form_field.register(forms.ModelChoiceField)
@convert_form_field.register(GlobalIDFormField) @convert_form_field.register(GlobalIDFormField)
def convert_form_field_to_id(field): def convert_form_field_to_id(field):
return ID() return ID(required=field.required)

View File

@ -226,24 +226,27 @@ def test_should_onetoone_reverse_convert_model():
assert isinstance(graphene_field, graphene.Dynamic) assert isinstance(graphene_field, graphene.Dynamic)
dynamic_field = graphene_field.get_type() dynamic_field = graphene_field.get_type()
assert isinstance(dynamic_field, graphene.Field) assert isinstance(dynamic_field, graphene.Field)
assert dynamic_field.type == A assert isinstance(dynamic_field.type, graphene.NonNull)
assert dynamic_field.of_type.type == A
@pytest.mark.skipif(ArrayField is MissingType, @pytest.mark.skipif(ArrayField is MissingType,
reason="ArrayField should exist") reason="ArrayField should exist")
def test_should_postgres_array_convert_list(): def test_should_postgres_array_convert_list():
field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100)) field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100))
assert isinstance(field.type, graphene.List) assert isinstance(field.type, graphene.NonNull)
assert field.type.of_type == graphene.String assert isinstance(field.type.of_type, graphene.List)
assert field.type.of_type.of_type == graphene.String
@pytest.mark.skipif(ArrayField is MissingType, @pytest.mark.skipif(ArrayField is MissingType,
reason="ArrayField should exist") reason="ArrayField should exist")
def test_should_postgres_array_multiple_convert_list(): def test_should_postgres_array_multiple_convert_list():
field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100))) field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100)))
assert isinstance(field.type, graphene.List) 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.String assert isinstance(field.type.of_type.of_type, graphene.List)
assert field.type.of_type.of_type.of_type == graphene.String
@pytest.mark.skipif(HStoreField is MissingType, @pytest.mark.skipif(HStoreField is MissingType,
@ -263,5 +266,6 @@ def test_should_postgres_json_convert_string():
def test_should_postgres_range_convert_list(): def test_should_postgres_range_convert_list():
from django.contrib.postgres.fields import IntegerRangeField from django.contrib.postgres.fields import IntegerRangeField
field = assert_conversion(IntegerRangeField, graphene.List) field = assert_conversion(IntegerRangeField, graphene.List)
assert isinstance(field.type, graphene.List) assert isinstance(field.type, graphene.NonNull)
assert field.type.of_type == graphene.Int assert isinstance(field.type.of_type, graphene.List)
assert field.type.of_type.of_type == graphene.Int

View File

@ -63,10 +63,10 @@ schema {
type Article implements Node { type Article implements Node {
id: ID! id: ID!
headline: String headline: String!
pubDate: DateTime pubDate: DateTime!
reporter: Reporter reporter: Reporter!
lang: ArticleLang lang: ArticleLang!
importance: ArticleImportance importance: ArticleImportance
} }
@ -104,12 +104,12 @@ type PageInfo {
} }
type Reporter { type Reporter {
id: ID id: ID!
firstName: String firstName: String!
lastName: String lastName: String!
email: String email: String!
pets: [Reporter] pets: [Reporter]
aChoice: ReporterA_choice aChoice: ReporterA_choice!
articles(before: String, after: String, first: Int, last: Int): ArticleConnection articles(before: String, after: String, first: Int, last: Int): ArticleConnection
} }