graphql-python / graphql-python/graphene-django

Option to display labels of Django Choices enumeration types

Open
#1,048 0 comments 0 reactions 0 assignees View on GitHub
✨enhancement
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

**Is your feature request related to a problem? Please describe.**
I would like to show "label" of Django choices [Enumeration type](https://docs.djangoproject.com/en/3.1/ref/models/fields/#enumeration-types) on my GraphQL endpoint.

For example:
the model
```python
class MyModel(models.Model):
class StatusType(m.TextChoices):
NEW = "new", "New"
NOTIFIED = "ntfd", "Notified"
RESOLVED = "rslv", "Resolved"

status = m.CharField(choices=StatusType.choices, default=StatusType.NEW)
```
result of GraphQL
```JSON
{
"data": {
"myModel": {
"status": "Resolved"
}
}
}
```
-> Returns "Resolved" instead of "RSLV" or "rslv"

**Describe the solution you'd like**
Adding an option to use "lable" of Enumeration, for example:
```python
class MyModelGraphQL(DjangoObjectType):
class Meta:
model = MyModel
use_enum_lables = True # or tuple of attr names
```

**Describe alternatives you've considered**
```python
class MyModelGraphQL(DjangoObjectType):
status = graphene.String()
class Meta:
model = MyModel

def resolve_status(self, info):
return self.get_status_display()
```
It works, but in my real project, there are multiple fields like this on one model and there are multiple models like this, too, so it would be definitely convenient to have an option to handle these altogether.
(And I'm guessing maybe the similar reasoning has been applied to `convert_choices_to_enum` option so we don't have to have `resolve_` methods one by one?)

**Additional context**
Not much, I just want to say thank you for this awesome library and the consideration on this!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.