Add custom function setting

This commit is contained in:
Jonathan Kim 2020-01-22 10:31:27 +00:00
parent f9252e3c4e
commit ecdbae3c8a
3 changed files with 15 additions and 10 deletions

View File

@ -70,7 +70,11 @@ def convert_choices_to_named_enum_with_descriptions(name, choices):
def generate_enum_name(django_model_meta, field):
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
if graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME and callable(
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME
):
name = graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME(field)
elif graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING is True:
name = "DjangoModel{app_label}{object_name}{field_name}Choices".format(
app_label=to_camel_case(django_model_meta.app_label.title()),
object_name=django_model_meta.object_name,

View File

@ -38,6 +38,7 @@ DEFAULTS = {
"CAMELCASE_ERRORS": False,
# Set to True to enable v3 naming convention for choice field Enum's
"DJANGO_CHOICE_FIELD_ENUM_V3_NAMING": False,
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
}
if settings.DEBUG:

View File

@ -514,14 +514,14 @@ class TestDjangoObjectType:
query: Query
}
enum DjangoModelTestsPetModelKindChoices {
enum CustomEnumKind {
CAT
DOG
}
type PetModelKind {
id: ID!
kind: DjangoModelTestsPetModelKindChoices!
kind: CustomEnumKind!
}
type Query {
@ -531,15 +531,13 @@ class TestDjangoObjectType:
)
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_V3_NAMING = False
def test_django_objecttype_choices_override_enum(self, PetModel):
def convert_choice(model, field_name, **kwargs):
return convert_choice_field_to_enum(
model._meta.get_field(field_name), **kwargs
)
def test_django_objecttype_choices_custom_enum_name(self, PetModel):
def custom_name(field):
return f"CustomEnum{field.name.title()}"
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = custom_name
class PetModelKind(DjangoObjectType):
kind = Field(convert_choice(PetModel, "kind", name="CustomEnumName"))
class Meta:
model = PetModel
fields = ["id", "kind"]
@ -570,3 +568,5 @@ class TestDjangoObjectType:
}
"""
)
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME = None