Skip to content

Commit c416a2b

Browse files
Flauschbaellchenfiraskafrikiendang
authored
Provide setting to enable/disable converting choices to enums globally (#1477)
Co-authored-by: Firas Kafri <[email protected]> Co-authored-by: Kien Dang <[email protected]>
1 parent feb7252 commit c416a2b

File tree

5 files changed

+135
-4
lines changed

5 files changed

+135
-4
lines changed

Diff for: docs/settings.rst

+9
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ Default: ``False``
142142
# ]
143143
144144
145+
``DJANGO_CHOICE_FIELD_ENUM_CONVERT``
146+
--------------------------------------
147+
148+
When set to ``True`` Django choice fields are automatically converted into Enum types.
149+
150+
Can be disabled globally by setting it to ``False``.
151+
152+
Default: ``True``
153+
145154
``DJANGO_CHOICE_FIELD_ENUM_V2_NAMING``
146155
--------------------------------------
147156

Diff for: graphene_django/converter.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,17 @@ def convert_choice_field_to_enum(field, name=None):
133133

134134

135135
def convert_django_field_with_choices(
136-
field, registry=None, convert_choices_to_enum=True
136+
field, registry=None, convert_choices_to_enum=None
137137
):
138138
if registry is not None:
139139
converted = registry.get_converted_field(field)
140140
if converted:
141141
return converted
142142
choices = getattr(field, "choices", None)
143+
if convert_choices_to_enum is None:
144+
convert_choices_to_enum = bool(
145+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT
146+
)
143147
if choices and convert_choices_to_enum:
144148
EnumCls = convert_choice_field_to_enum(field)
145149
required = not (field.blank or field.null)

Diff for: graphene_django/settings.py

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
# Max items returned in ConnectionFields / FilterConnectionFields
3131
"RELAY_CONNECTION_MAX_LIMIT": 100,
3232
"CAMELCASE_ERRORS": True,
33+
# Automatically convert Choice fields of Django into Enum fields
34+
"DJANGO_CHOICE_FIELD_ENUM_CONVERT": True,
3335
# Set to True to enable v2 naming convention for choice field Enum's
3436
"DJANGO_CHOICE_FIELD_ENUM_V2_NAMING": False,
3537
"DJANGO_CHOICE_FIELD_ENUM_CUSTOM_NAME": None,

Diff for: graphene_django/tests/test_types.py

+116
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,122 @@ class Query(ObjectType):
661661
}"""
662662
)
663663

664+
def test_django_objecttype_convert_choices_global_false(
665+
self, graphene_settings, PetModel
666+
):
667+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT = False
668+
669+
class Pet(DjangoObjectType):
670+
class Meta:
671+
model = PetModel
672+
fields = "__all__"
673+
674+
class Query(ObjectType):
675+
pet = Field(Pet)
676+
677+
schema = Schema(query=Query)
678+
679+
assert str(schema) == dedent(
680+
"""\
681+
type Query {
682+
pet: Pet
683+
}
684+
685+
type Pet {
686+
id: ID!
687+
kind: String!
688+
cuteness: Int!
689+
}"""
690+
)
691+
692+
def test_django_objecttype_convert_choices_true_global_false(
693+
self, graphene_settings, PetModel
694+
):
695+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT = False
696+
697+
class Pet(DjangoObjectType):
698+
class Meta:
699+
model = PetModel
700+
fields = "__all__"
701+
convert_choices_to_enum = True
702+
703+
class Query(ObjectType):
704+
pet = Field(Pet)
705+
706+
schema = Schema(query=Query)
707+
708+
assert str(schema) == dedent(
709+
"""\
710+
type Query {
711+
pet: Pet
712+
}
713+
714+
type Pet {
715+
id: ID!
716+
kind: TestsPetModelKindChoices!
717+
cuteness: TestsPetModelCutenessChoices!
718+
}
719+
720+
\"""An enumeration.\"""
721+
enum TestsPetModelKindChoices {
722+
\"""Cat\"""
723+
CAT
724+
725+
\"""Dog\"""
726+
DOG
727+
}
728+
729+
\"""An enumeration.\"""
730+
enum TestsPetModelCutenessChoices {
731+
\"""Kind of cute\"""
732+
A_1
733+
734+
\"""Pretty cute\"""
735+
A_2
736+
737+
\"""OMG SO CUTE!!!\"""
738+
A_3
739+
}"""
740+
)
741+
742+
def test_django_objecttype_convert_choices_enum_list_global_false(
743+
self, graphene_settings, PetModel
744+
):
745+
graphene_settings.DJANGO_CHOICE_FIELD_ENUM_CONVERT = False
746+
747+
class Pet(DjangoObjectType):
748+
class Meta:
749+
model = PetModel
750+
convert_choices_to_enum = ["kind"]
751+
fields = "__all__"
752+
753+
class Query(ObjectType):
754+
pet = Field(Pet)
755+
756+
schema = Schema(query=Query)
757+
758+
assert str(schema) == dedent(
759+
"""\
760+
type Query {
761+
pet: Pet
762+
}
763+
764+
type Pet {
765+
id: ID!
766+
kind: TestsPetModelKindChoices!
767+
cuteness: Int!
768+
}
769+
770+
\"""An enumeration.\"""
771+
enum TestsPetModelKindChoices {
772+
\"""Cat\"""
773+
CAT
774+
775+
\"""Dog\"""
776+
DOG
777+
}"""
778+
)
779+
664780

665781
@with_local_registry
666782
def test_django_objecttype_name_connection_propagation():

Diff for: graphene_django/types.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
def construct_fields(
26-
model, registry, only_fields, exclude_fields, convert_choices_to_enum
26+
model, registry, only_fields, exclude_fields, convert_choices_to_enum=None
2727
):
2828
_model_fields = get_model_fields(model)
2929

@@ -47,7 +47,7 @@ def construct_fields(
4747
continue
4848

4949
_convert_choices_to_enum = convert_choices_to_enum
50-
if not isinstance(_convert_choices_to_enum, bool):
50+
if isinstance(_convert_choices_to_enum, list):
5151
# then `convert_choices_to_enum` is a list of field names to convert
5252
if name in _convert_choices_to_enum:
5353
_convert_choices_to_enum = True
@@ -146,7 +146,7 @@ def __init_subclass_with_meta__(
146146
connection_class=None,
147147
use_connection=None,
148148
interfaces=(),
149-
convert_choices_to_enum=True,
149+
convert_choices_to_enum=None,
150150
_meta=None,
151151
**options,
152152
):

0 commit comments

Comments
 (0)