Skip to content

Commit

Permalink
Provide setting to enable/disable converting choices to enums globally
Browse files Browse the repository at this point in the history
  • Loading branch information
Flauschbaellchen authored and Markus Richter committed Nov 15, 2023
1 parent 4ac3f3f commit e9632d1
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,15 @@ Default: ``False``
# ]
``DJANGO_CHOICE_FIELD_ENUM_CONVERT``
--------------------------------------

When set to ``True`` Django choice fields are automatically converted into Enum types.

Can be disabled globally by setting it to ``False``.

Default: ``True``

``DJANGO_CHOICE_FIELD_ENUM_V2_NAMING``
--------------------------------------

Expand Down
2 changes: 2 additions & 0 deletions graphene_django/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
# Max items returned in ConnectionFields / FilterConnectionFields
"RELAY_CONNECTION_MAX_LIMIT": 100,
"CAMELCASE_ERRORS": True,
# Automatically convert Choice fields of Django into Enum fields
"DJANGO_CHOICE_FIELD_ENUM_CONVERT": True,
# Set to True to enable v2 naming convention for choice field Enum's
"DJANGO_CHOICE_FIELD_ENUM_V2_NAMING": False,
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,
Expand Down
116 changes: 116 additions & 0 deletions graphene_django/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,122 @@ class Query(ObjectType):
}"""
)

def test_django_objecttype_convert_choices_global_false(
self, graphene_settings, PetModel
):
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT = False

class Pet(DjangoObjectType):
class Meta:
model = PetModel
fields = "__all__"

class Query(ObjectType):
pet = Field(Pet)

schema = Schema(query=Query)

assert str(schema) == dedent(
"""\
type Query {
pet: Pet
}
type Pet {
id: ID!
kind: String!
cuteness: Int!
}"""
)

def test_django_objecttype_convert_choices_true_global_false(
self, graphene_settings, PetModel
):
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT = False

class Pet(DjangoObjectType):
class Meta:
model = PetModel
fields = "__all__"
convert_choices_to_enum = True

class Query(ObjectType):
pet = Field(Pet)

schema = Schema(query=Query)

assert str(schema) == dedent(
"""\
type Query {
pet: Pet
}
type Pet {
id: ID!
kind: TestsPetModelKindChoices!
cuteness: TestsPetModelCutenessChoices!
}
\"""An enumeration.\"""
enum TestsPetModelKindChoices {
\"""Cat\"""
CAT
\"""Dog\"""
DOG
}
\"""An enumeration.\"""
enum TestsPetModelCutenessChoices {
\"""Kind of cute\"""
A_1
\"""Pretty cute\"""
A_2
\"""OMG SO CUTE!!!\"""
A_3
}"""
)

def test_django_objecttype_convert_choices_enum_list_global_false(
self, graphene_settings, PetModel
):
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT = False

class Pet(DjangoObjectType):
class Meta:
model = PetModel
convert_choices_to_enum = ["kind"]
fields = "__all__"

class Query(ObjectType):
pet = Field(Pet)

schema = Schema(query=Query)

assert str(schema) == dedent(
"""\
type Query {
pet: Pet
}
type Pet {
id: ID!
kind: TestsPetModelKindChoices!
cuteness: Int!
}
\"""An enumeration.\"""
enum TestsPetModelKindChoices {
\"""Cat\"""
CAT
\"""Dog\"""
DOG
}"""
)


@with_local_registry
def test_django_objecttype_name_connection_propagation():
Expand Down
7 changes: 6 additions & 1 deletion graphene_django/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def __init_subclass_with_meta__(
connection_class=None,
use_connection=None,
interfaces=(),
convert_choices_to_enum=True,
convert_choices_to_enum=None,
_meta=None,
**options,
):
Expand Down Expand Up @@ -220,6 +220,11 @@ def __init_subclass_with_meta__(
stacklevel=2,
)

if convert_choices_to_enum is None:
convert_choices_to_enum = bool(
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT
)

django_fields = yank_fields_from_attrs(
construct_fields(model, registry, fields, exclude, convert_choices_to_enum),
_as=graphene.Field,
Expand Down

0 comments on commit e9632d1

Please sign in to comment.