Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum.from_enum() fails with Django IntegerChoices #1541

Open
yoshiohasegawa opened this issue Apr 2, 2024 · 0 comments
Open

Enum.from_enum() fails with Django IntegerChoices #1541

yoshiohasegawa opened this issue Apr 2, 2024 · 0 comments
Labels

Comments

@yoshiohasegawa
Copy link

Current Behavior

I am working in a Django Python codebase that has a GraphQL schema. A lot of TextChoices are used in our GraphQL schema and, to properly represent these we use the provided graphene.Enum.from_enum() function.

This same approach doesn't seem to work with Django's IntegerChoices. It seems when graphene generates a GrapheneEnumType, the values are created with the IntegerChoices member names. This makes it so the IntegerChoices value cannot be represented with the generated GrapheneEnumType.

This results in an error like the following:
"Enum 'Status' cannot represent value: 2"

Expected Behavior

A value from a Django IntegerChoices object is usable to get the appropriate GrapheneEnumType value name.

motivation

To allow for graphene.Enum.from_enum() to be usable by Django's IntegerChoices class.

Environment

  • Graphene Version: 3.3
  • Graphene Django Version: 3.2.0
  • Graphene Version: 3.3
  • Django Version: 4.1.12
  • Python Version: 3.10

Code Example

class Status(models.IntegerChoices):
    COMPLETED = 1, "Completed"
    IN_PROGRESS = 2, "In progress"
    ERRORED = 9, "Errored"


StatusChoices = graphene.Enum.from_enum(Status)

class Process(graphene.ObjectType):
    reference = graphene.String(description="The process reference.")
    status = graphene.Field(
        StatusChoices,
        description="Current process status.",
    )

This results in an error like the following:
"Enum 'Status' cannot represent value: 2"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant