Graphene-django: How to create a field to expose Model Fields' choices

Created on 20 Jul 2018  路  3Comments  路  Source: graphql-python/graphene-django

I have a model field with choices:

CHOICES = [
  ('foo', 'Foo'),
  ('bar', 'Bar'),
]

class MyModel(models.Model):
    ham = models.CharField(max_length=2, choices=CHOICES)

So, in the schema I have the model and its choices. But can I expose the choices ?
I tried this but without success:

MyModelHamType = registry.get_converted_field(models.MyModel._meta.get_field('ham')).get_type()

class Query(object):
    choices = graphene.Field(MyModelHamType)

    def resolve_choices(self, info, **kwargs):
        # What to do here
        return MyModelHamType

The code above gives the following thing:
Request :

query {
  hams
}

Response:

{
  "errors": [
    {
      "message": "Expected a value of type \"MyModelHam\" but received: MyModelHam",
      "path": [
        "hams"
      ]
    }
  ],
  "data": {
    "hams": null
  }
}

It seems that as the object aren't the of the same type (Pythonicly saying), Graphene doesn't want to handle.


I could recreate myself a MyModelHamType with Enum, but it will be duplicate with the existing one autocreated and already in schema.

question

Most helpful comment

@sciyoshi Any news ?

All 3 comments

@sciyoshi Any news ?

Let's take this over to #67. Closing this down.

@ZuluPro were you able to fix this issue? Could you explain how?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

timothyjlaurent picture timothyjlaurent  路  3Comments

hyusetiawan picture hyusetiawan  路  4Comments

licx picture licx  路  3Comments

MythicManiac picture MythicManiac  路  3Comments

dan-klasson picture dan-klasson  路  4Comments