From b8f9fec674e8f58268e3368f9c38912b0d757279 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 22 Sep 2016 19:57:28 -0700 Subject: [PATCH] Added required conversion to model and form fields. Fixed #2 --- graphene_django/converter.py | 26 ++++++++++++------------- graphene_django/form_converter.py | 10 +++++----- graphene_django/tests/test_converter.py | 18 ++++++++++------- graphene_django/tests/test_types.py | 18 ++++++++--------- 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/graphene_django/converter.py b/graphene_django/converter.py index 88c32af..36d5a52 100644 --- a/graphene_django/converter.py +++ b/graphene_django/converter.py @@ -47,7 +47,7 @@ def convert_django_field_with_choices(field, registry=None): return named_choices_descriptions[self.name] 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) @@ -67,12 +67,12 @@ def convert_django_field(field, registry=None): @convert_django_field.register(models.FileField) @convert_django_field.register(UUIDField) 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) 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) @@ -81,7 +81,7 @@ def convert_field_to_id(field, registry=None): @convert_django_field.register(models.BigIntegerField) @convert_django_field.register(models.IntegerField) 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) @@ -91,18 +91,18 @@ def convert_field_to_boolean(field, registry=None): @convert_django_field.register(models.NullBooleanField) 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.FloatField) 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) 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) @@ -114,7 +114,7 @@ def convert_onetoone_field_to_djangomodel(field, registry=None): if not _type: return - return Field(_type) + return Field(_type, required=not field.null) return Dynamic(dynamic_type) @@ -149,7 +149,7 @@ def convert_relatedfield_to_djangomodel(field, registry=None): return if isinstance(field.field, models.OneToOneField): - return Field(_type) + return Field(_type, required=not field.field.null) if is_node(_type): return get_connection_field(_type) @@ -168,7 +168,7 @@ def convert_field_to_djangomodel(field, registry=None): if not _type: return - return Field(_type, description=field.help_text) + return Field(_type, description=field.help_text, required=not field.null) 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) if not isinstance(base_type, (List, NonNull)): 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(JSONField) 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) @@ -192,4 +192,4 @@ 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) - return List(inner_type, description=field.help_text) + return List(inner_type, description=field.help_text, required=not field.null) diff --git a/graphene_django/form_converter.py b/graphene_django/form_converter.py index 8778881..c87e325 100644 --- a/graphene_django/form_converter.py +++ b/graphene_django/form_converter.py @@ -34,13 +34,13 @@ def convert_form_field(field): @convert_form_field.register(forms.Field) @convert_form_field.register(UUIDField) 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.NumberInput) 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) @@ -56,16 +56,16 @@ def convert_form_field_to_nullboolean(field): @convert_form_field.register(forms.DecimalField) @convert_form_field.register(forms.FloatField) 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(GlobalIDMultipleChoiceField) 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(GlobalIDFormField) def convert_form_field_to_id(field): - return ID() + return ID(required=field.required) diff --git a/graphene_django/tests/test_converter.py b/graphene_django/tests/test_converter.py index 81402fc..d2f9b61 100644 --- a/graphene_django/tests/test_converter.py +++ b/graphene_django/tests/test_converter.py @@ -226,24 +226,27 @@ def test_should_onetoone_reverse_convert_model(): assert isinstance(graphene_field, graphene.Dynamic) dynamic_field = graphene_field.get_type() 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, reason="ArrayField should exist") def test_should_postgres_array_convert_list(): field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100)) - assert isinstance(field.type, graphene.List) - assert field.type.of_type == graphene.String + assert isinstance(field.type, graphene.NonNull) + assert isinstance(field.type.of_type, graphene.List) + assert field.type.of_type.of_type == graphene.String @pytest.mark.skipif(ArrayField is MissingType, reason="ArrayField should exist") def test_should_postgres_array_multiple_convert_list(): 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 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, @@ -263,5 +266,6 @@ def test_should_postgres_json_convert_string(): def test_should_postgres_range_convert_list(): from django.contrib.postgres.fields import IntegerRangeField field = assert_conversion(IntegerRangeField, graphene.List) - assert isinstance(field.type, graphene.List) - assert field.type.of_type == graphene.Int + assert isinstance(field.type, graphene.NonNull) + assert isinstance(field.type.of_type, graphene.List) + assert field.type.of_type.of_type == graphene.Int diff --git a/graphene_django/tests/test_types.py b/graphene_django/tests/test_types.py index 22f49c9..c0d88a2 100644 --- a/graphene_django/tests/test_types.py +++ b/graphene_django/tests/test_types.py @@ -63,10 +63,10 @@ schema { type Article implements Node { id: ID! - headline: String - pubDate: DateTime - reporter: Reporter - lang: ArticleLang + headline: String! + pubDate: DateTime! + reporter: Reporter! + lang: ArticleLang! importance: ArticleImportance } @@ -104,12 +104,12 @@ type PageInfo { } type Reporter { - id: ID - firstName: String - lastName: String - email: String + id: ID! + firstName: String! + lastName: String! + email: String! pets: [Reporter] - aChoice: ReporterA_choice + aChoice: ReporterA_choice! articles(before: String, after: String, first: Int, last: Int): ArticleConnection }