graphql-python / graphql-python/graphene

Enum.from_enum() fails with Django IntegerChoices

Abierto
#1,541 0 comentarios 1 reacción 0 asignados Ver en GitHub
🐛 bug
Lenguaje dominante
Python
Estrellas
8.2k
Forks
818
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

## 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
```python
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"`

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Línea de trabajo

Start at graphene.Enum.from_enum() and the generated GrapheneEnumType behavior, using the provided Django IntegerChoices example to reproduce the failure. Compare the handling of TextChoices with IntegerChoices; the work is done when values such as 1, 2, and 9 can be represented by the generated Graphene enum without the reported error.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
django, graphql, python
Área
api, backend
Tipo de issue
Error
Dificultad
3/5
Tiempo estimado
1-2 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
45/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.