mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-22 09:37:07 +03:00
Make v3 django choice field enum naming default (in v3) (#982)
Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
This commit is contained in:
parent
85976ffb1f
commit
17146f9b01
|
@ -142,20 +142,20 @@ Default: ``False``
|
||||||
# ]
|
# ]
|
||||||
|
|
||||||
|
|
||||||
``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING``
|
``DJANGO_CHOICE_FIELD_ENUM_V2_NAMING``
|
||||||
--------------------------------------
|
--------------------------------------
|
||||||
|
|
||||||
Set to ``True`` to use the new naming format for the auto generated Enum types from Django choice fields. The new format looks like this: ``{app_label}{object_name}{field_name}Choices``
|
Set to ``True`` to use the old naming format for the auto generated Enum types from Django choice fields. The old format looks like this: ``{object_name}_{field_name}``
|
||||||
|
|
||||||
Default: ``False``
|
Default: ``False``
|
||||||
|
|
||||||
|
|
||||||
``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME``
|
``DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME``
|
||||||
--------------------------------------
|
----------------------------------------
|
||||||
|
|
||||||
Define the path of a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
|
Define the path of a function that takes the Django choice field and returns a string to completely customise the naming for the Enum type.
|
||||||
|
|
||||||
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V3_NAMING`` setting is ignored.
|
If set to a function then the ``DJANGO_CHOICE_FIELD_ENUM_V2_NAMING`` setting is ignored.
|
||||||
|
|
||||||
Default: ``None``
|
Default: ``None``
|
||||||
|
|
||||||
|
|
|
@ -79,14 +79,14 @@ def generate_enum_name(django_model_meta, field):
|
||||||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
|
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
|
||||||
)
|
)
|
||||||
name = custom_func(field)
|
name = custom_func(field)
|
||||||
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
|
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V2_NAMING is True:
|
||||||
|
name = to_camel_case("{}_{}".format(django_model_meta.object_name, field.name))
|
||||||
|
else:
|
||||||
name = "{app_label}{object_name}{field_name}Choices".format(
|
name = "{app_label}{object_name}{field_name}Choices".format(
|
||||||
app_label=to_camel_case(django_model_meta.app_label.title()),
|
app_label=to_camel_case(django_model_meta.app_label.title()),
|
||||||
object_name=django_model_meta.object_name,
|
object_name=django_model_meta.object_name,
|
||||||
field_name=to_camel_case(field.name.title()),
|
field_name=to_camel_case(field.name.title()),
|
||||||
)
|
)
|
||||||
else:
|
|
||||||
name = to_camel_case("{}_{}".format(django_model_meta.object_name, field.name))
|
|
||||||
return name
|
return name
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,8 +35,8 @@ DEFAULTS = {
|
||||||
# Max items returned in ConnectionFields / FilterConnectionFields
|
# Max items returned in ConnectionFields / FilterConnectionFields
|
||||||
"RELAY_CONNECTION_MAX_LIMIT": 100,
|
"RELAY_CONNECTION_MAX_LIMIT": 100,
|
||||||
"CAMELCASE_ERRORS": True,
|
"CAMELCASE_ERRORS": True,
|
||||||
# Set to True to enable v3 naming convention for choice field Enum's
|
# Set to True to enable v2 naming convention for choice field Enum's
|
||||||
"DJANGO_CHOICE_FIELD_ENUM_V3_NAMING": False,
|
"DJANGO_CHOICE_FIELD_ENUM_V2_NAMING": False,
|
||||||
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
|
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -159,7 +159,7 @@ def test_field_with_choices_convert_enum():
|
||||||
|
|
||||||
graphene_type = convert_django_field_with_choices(field)
|
graphene_type = convert_django_field_with_choices(field)
|
||||||
assert isinstance(graphene_type, graphene.Enum)
|
assert isinstance(graphene_type, graphene.Enum)
|
||||||
assert graphene_type._meta.name == "TranslatedModelLanguage"
|
assert graphene_type._meta.name == "TestTranslatedModelLanguageChoices"
|
||||||
assert graphene_type._meta.enum.__members__["ES"].value == "es"
|
assert graphene_type._meta.enum.__members__["ES"].value == "es"
|
||||||
assert graphene_type._meta.enum.__members__["ES"].description == "Spanish"
|
assert graphene_type._meta.enum.__members__["ES"].description == "Spanish"
|
||||||
assert graphene_type._meta.enum.__members__["EN"].value == "en"
|
assert graphene_type._meta.enum.__members__["EN"].value == "en"
|
||||||
|
@ -344,9 +344,8 @@ def test_should_postgres_range_convert_list():
|
||||||
assert field.type.of_type.of_type == graphene.Int
|
assert field.type.of_type.of_type == graphene.Int
|
||||||
|
|
||||||
|
|
||||||
def test_generate_enum_name(graphene_settings):
|
def test_generate_enum_name():
|
||||||
MockDjangoModelMeta = namedtuple("DjangoMeta", ["app_label", "object_name"])
|
MockDjangoModelMeta = namedtuple("DjangoMeta", ["app_label", "object_name"])
|
||||||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = True
|
|
||||||
|
|
||||||
# Simple case
|
# Simple case
|
||||||
field = graphene.Field(graphene.String, name="type")
|
field = graphene.Field(graphene.String, name="type")
|
||||||
|
@ -362,3 +361,20 @@ def test_generate_enum_name(graphene_settings):
|
||||||
generate_enum_name(model_meta, field)
|
generate_enum_name(model_meta, field)
|
||||||
== "SomeLongAppNameSomeObjectFizzBuzzChoices"
|
== "SomeLongAppNameSomeObjectFizzBuzzChoices"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_generate_v2_enum_name(graphene_settings):
|
||||||
|
MockDjangoModelMeta = namedtuple("DjangoMeta", ["app_label", "object_name"])
|
||||||
|
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V2_NAMING = True
|
||||||
|
|
||||||
|
# Simple case
|
||||||
|
field = graphene.Field(graphene.String, name="type")
|
||||||
|
model_meta = MockDjangoModelMeta(app_label="users", object_name="User")
|
||||||
|
assert generate_enum_name(model_meta, field) == "UserType"
|
||||||
|
|
||||||
|
# More complicated multiple work case
|
||||||
|
field = graphene.Field(graphene.String, name="fizz_buzz")
|
||||||
|
model_meta = MockDjangoModelMeta(
|
||||||
|
app_label="some_long_app_name", object_name="SomeObject"
|
||||||
|
)
|
||||||
|
assert generate_enum_name(model_meta, field) == "SomeObjectFizzBuzz"
|
||||||
|
|
|
@ -128,8 +128,8 @@ def test_schema_representation():
|
||||||
editor: Reporter!
|
editor: Reporter!
|
||||||
|
|
||||||
\"""Language\"""
|
\"""Language\"""
|
||||||
lang: ArticleLang!
|
lang: TestsArticleLangChoices!
|
||||||
importance: ArticleImportance
|
importance: TestsArticleImportanceChoices
|
||||||
}
|
}
|
||||||
|
|
||||||
\"""An object with an ID\"""
|
\"""An object with an ID\"""
|
||||||
|
@ -153,7 +153,7 @@ def test_schema_representation():
|
||||||
scalar DateTime
|
scalar DateTime
|
||||||
|
|
||||||
\"""An enumeration.\"""
|
\"""An enumeration.\"""
|
||||||
enum ArticleLang {
|
enum TestsArticleLangChoices {
|
||||||
\"""Spanish\"""
|
\"""Spanish\"""
|
||||||
ES
|
ES
|
||||||
|
|
||||||
|
@ -162,7 +162,7 @@ def test_schema_representation():
|
||||||
}
|
}
|
||||||
|
|
||||||
\"""An enumeration.\"""
|
\"""An enumeration.\"""
|
||||||
enum ArticleImportance {
|
enum TestsArticleImportanceChoices {
|
||||||
\"""Very important\"""
|
\"""Very important\"""
|
||||||
A_1
|
A_1
|
||||||
|
|
||||||
|
@ -177,13 +177,13 @@ def test_schema_representation():
|
||||||
lastName: String!
|
lastName: String!
|
||||||
email: String!
|
email: String!
|
||||||
pets: [Reporter!]!
|
pets: [Reporter!]!
|
||||||
aChoice: ReporterAChoice
|
aChoice: TestsReporterAChoiceChoices
|
||||||
reporterType: ReporterReporterType
|
reporterType: TestsReporterReporterTypeChoices
|
||||||
articles(before: String = null, after: String = null, first: Int = null, last: Int = null): ArticleConnection!
|
articles(before: String = null, after: String = null, first: Int = null, last: Int = null): ArticleConnection!
|
||||||
}
|
}
|
||||||
|
|
||||||
\"""An enumeration.\"""
|
\"""An enumeration.\"""
|
||||||
enum ReporterAChoice {
|
enum TestsReporterAChoiceChoices {
|
||||||
\"""this\"""
|
\"""this\"""
|
||||||
A_1
|
A_1
|
||||||
|
|
||||||
|
@ -192,7 +192,7 @@ def test_schema_representation():
|
||||||
}
|
}
|
||||||
|
|
||||||
\"""An enumeration.\"""
|
\"""An enumeration.\"""
|
||||||
enum ReporterReporterType {
|
enum TestsReporterReporterTypeChoices {
|
||||||
\"""Regular\"""
|
\"""Regular\"""
|
||||||
A_1
|
A_1
|
||||||
|
|
||||||
|
@ -512,12 +512,12 @@ class TestDjangoObjectType:
|
||||||
|
|
||||||
type Pet {
|
type Pet {
|
||||||
id: ID!
|
id: ID!
|
||||||
kind: PetModelKind!
|
kind: TestsPetModelKindChoices!
|
||||||
cuteness: Int!
|
cuteness: Int!
|
||||||
}
|
}
|
||||||
|
|
||||||
\"""An enumeration.\"""
|
\"""An enumeration.\"""
|
||||||
enum PetModelKind {
|
enum TestsPetModelKindChoices {
|
||||||
\"""Cat\"""
|
\"""Cat\"""
|
||||||
CAT
|
CAT
|
||||||
|
|
||||||
|
@ -555,8 +555,6 @@ class TestDjangoObjectType:
|
||||||
def test_django_objecttype_convert_choices_enum_naming_collisions(
|
def test_django_objecttype_convert_choices_enum_naming_collisions(
|
||||||
self, PetModel, graphene_settings
|
self, PetModel, graphene_settings
|
||||||
):
|
):
|
||||||
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = True
|
|
||||||
|
|
||||||
class PetModelKind(DjangoObjectType):
|
class PetModelKind(DjangoObjectType):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = PetModel
|
model = PetModel
|
||||||
|
|
Loading…
Reference in New Issue
Block a user