strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
415 stars 120 forks source link

Support django Choices for graphql enums #65

Closed deibeljc closed 1 year ago

deibeljc commented 3 years ago

With django 3.0 came the choices model field type See here. It would be nice if this integration converted those values into an enum for us so we don't have to declare the same enum twice and the source of truth can remain the model.

E.G.

Declaration in model

class PlayerTypes(models.TextChoices):
    FA = "FA", "Free Agent"
    PermFA = "PermFA", "Permanent Free Agent"
    Signed = "Signed", "Signed"
    Inactive = "Inactive", "Inactive"

Example usage?

@strawberry.django.type(models.Player)
class Player:
    name: auto
    type = strawberry.enum(models.PlayerTypes)

Upvote & Fund

Fund with Polar

LucidDan commented 2 years ago

So far, I've found this to work just fine, like so:

PlayerType = strawberry.enum(models.PlayerTypes)

@strawberry.django.type(models.Player)
class Player:
    name: auto
    type: PlayerType
la4de commented 2 years ago

@LucidDan, thanks for the tip.

@deibeljc, does this solution work for you?

capital-G commented 1 year ago

Works in the proposed way by @LucidDan but should be documented somewhere.