question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

graphene3: enum doesn't resolve to value

See original GitHub issue

Using enums as inputFields leads to errors

SalutationEnum = graphene.Enum('SalutationEnum', [('Man', 0), ('Woman', 1)])

class MyModel(Model):
  salutation = models.IntegerField()

class SomeInput(graphene.InputObjectType):
    salutation = SalutationEnum(required=True)

class MyMutation(graphene.Mutation):
    ...
    class Arguments:
        input = SomeInput(required=True)
    ...

    mutate(self, info, input):
        ...
        myModel.salutation = input.salutation
        myModel.save()

leads to

"message": "Field 'salutation' expected a number but got <SalutationEnum.Man: 0>.",

I think the problem is, that SalutationEnum is not resolved to its value (this worked in graphene 2.x) but resolved to a SalutationEnum

salutation: SalutationEnum{
  name: 'Man'
  value: 0
}

This code would work:

 myModel.salutation = input.salutation.value
 myModel.save()

But I don’t think it is intended to call value on each enum?

Edit: Just found #1151 - looks like this is intended??

graphene: 3.0.0b5

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Speedy1991commented, Nov 4, 2020

Just tried your example and I can confirm this is working

Just debuged deep into this process and found that I wrote a wrong value into the DB when I tried around with the enums.

That was really confusing because the database value was MemberRoleEnum.Administrator and not admin.

Btw. great work @jkimbo & contributors - looking foward the 3.0 release

1reaction
jkimbocommented, Oct 21, 2020

Ah ok I see. You can also check if the value is an enum before trying to access .value.

Anyway closing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Graphene: Enum argument doesn't seem to work
The issue here is that we can assign an arbitrary value for each enum value, but this value is only used internally by...
Read more >
Enums - Graphene-Python
An Enum is a special GraphQL type that represents a set of symbolic names (members) bound to unique, constant values. Definition¶. You can...
Read more >
How to use the graphene.Enum function in graphene - Snyk
Use Snyk Code to scan source code in minutes - no build needed - and fix issues ... v.value) for v in type.choices))...
Read more >
Graphene Documentation
It's possible to add a description to an enum value, for that the ... Enum internally (or a backport if that's not available)...
Read more >
Graphene Documentation - Read the Docs
The first parameter of a resolver method (parent) is the value object returned from the resolver of the parent field. If there is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found