Skip to content

Commit

Permalink
fix: Fix auto enum value allowed chars (#494)
Browse files Browse the repository at this point in the history
* fix: use str() to trigger eventual django's gettext_lazy string

* fix: auto enum convert choice value to valid GraphQL enum name by replacing invalid chars by _

* fix: underscores are allowed at start

* fix: avoid auto enum name conflict
  • Loading branch information
fabien-michel authored Mar 5, 2024
1 parent fc23726 commit 475b03f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
19 changes: 14 additions & 5 deletions strawberry_django/fields/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import decimal
import enum
import inspect
import re
import uuid
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -447,6 +448,18 @@ def resolve_model_field_type(
field_type = getattr(model_field, "_strawberry_enum", None)
if field_type is None:
meta = model_field.model._meta

enum_choices = {}
for c in model_field.choices:
# replace chars not compatible with GraphQL naming convention
choice_name = re.sub(r"^[^_a-zA-Z]|[^_a-zA-Z0-9]", "_", c[0])
# use str() to trigger eventual django's gettext_lazy string
choice_value = EnumValueDefinition(value=c[0], description=str(c[1]))

while choice_name in enum_choices:
choice_name = choice_name + "_"
enum_choices[choice_name] = choice_value

field_type = strawberry.enum( # type: ignore
enum.Enum( # type: ignore
"".join(
Expand All @@ -457,11 +470,7 @@ def resolve_model_field_type(
"Enum",
),
),
{
# use str() to trigger eventual django's gettext_lazy string
c[0]: EnumValueDefinition(value=c[0], description=str(c[1]))
for c in model_field.choices
},
enum_choices,
),
description=(
f"{meta.verbose_name} | {model_field.verbose_name}"
Expand Down
20 changes: 20 additions & 0 deletions tests/test_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Choice(models.TextChoices):
A = "a", "A description"
B = "b", "B description"
C = "c", gettext_lazy("C description")
D = "12d-d'éléphant_🐘", "D description"
E = "_2d_d__l_phant__", "E description"


class IntegerChoice(models.IntegerChoices):
Expand Down Expand Up @@ -113,6 +115,8 @@ def obj(self) -> ChoicesType:
A
B
C
D
E
}
type ChoicesType {
Expand Down Expand Up @@ -259,6 +263,8 @@ def obj(self) -> ChoicesType:
A
B
C
D
E
}
type ChoicesType {
Expand Down Expand Up @@ -297,6 +303,12 @@ def obj(self) -> ChoicesType:
"""C description"""
c
"""D description"""
_2d_d__l_phant__
"""E description"""
_2d_d__l_phant___
}
'''

Expand Down Expand Up @@ -360,6 +372,8 @@ def obj(self) -> ChoicesWithExtraFieldsType:
A
B
C
D
E
}
type ChoicesWithExtraFieldsType {
Expand Down Expand Up @@ -400,6 +414,12 @@ def obj(self) -> ChoicesWithExtraFieldsType:
"""C description"""
c
"""D description"""
_2d_d__l_phant__
"""E description"""
_2d_d__l_phant___
}
Expand Down

0 comments on commit 475b03f

Please sign in to comment.