I would like to use dynamically generated enums with strawberry.
This is possible with graphene and with the standard enum library.
graphene.Enum
LanguageCode = graphene.Enum(
"LanguageCode",
[(lang[0].replace("-", "_").upper(), lang[0]) for lang in settings.LANGUAGES],
)
enum.Enum
LanguageCode = enum.Enum(
"LanguageCode",
{lang[0].replace("-", "_").upper(): lang[0] for lang in settings.LANGUAGES},
type=str,
)
I would like for strawberry to work with an enum that is created dynamically like above, by calling (rather than subclassing) the enum class. In documented examples, the strawberry.enum
decorator only goes on class definitions.
It seems like instead of using strawberry.enum
as a decorator, I might be able to use it as follows for a dynamic enum:
LanguageCode = gql.enum(
Enum(
"LanguageCode",
{lang[0].replace("-", "_").upper(): lang[0] for lang in settings.LANGUAGES},
type=str,
)
)
But this might not be valid. And in this case, if I then use LanguageCode
for type annotations, type checkers will complain, Illegal type annotation: variable not allowed unless it is a type alias
.
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too