I would like my enum input values to be the enum instance instead of the enum values
See original GitHub issueIs there a way for me to do this?
Here is some example code.
from enum import Enum, auto
from graphene import Enum as GQLEnum, ObjectType, Schema, String
from graphene.relay import ClientIDMutation
from graphene.test import Client
class EnumThing(Enum):
a = auto()
b = auto()
GQLEnumThing = GQLEnum.from_enum(EnumThing)
class TestMut(ClientIDMutation):
class Input:
enumthing = GQLEnumThing(required=True)
enumtype = String()
@classmethod
def mutate_and_get_payload(cls, root, info, enumthing, client_mutation_id=None):
print("enumthing is", repr(enumthing), type(enumthing))
return cls(enumtype=type(enumthing).__name__)
class Mutations(ObjectType):
testmut = TestMut.Field()
schema = Schema(mutation=Mutations, auto_camelcase=False)
client = Client(schema)
mutation = '''
mutation whatever {
testmut(input: {enumthing: a}) {
enumtype
}
}
'''
print(client.execute(mutation))
When I run this, I get the following output:
enumthing is 1 <class 'int'>
{'data': OrderedDict([('testmut', OrderedDict([('enumtype', 'int')]))])}
Instead of getting the integer 1
passed to my mutation function, I would like to have EnumThing.a
passed, which is an instance of EnumThing
. I haven’t figured out where in graphene this translation of the literal a
to the value 1
is actually happening (I would expect an access of the .value
attribute on the enum somewhere).
Why? because I don’t really care about the integer 1
– that’s just something generated by Python. If I log the value of this enum, I want to see that it’s a EnumThing.a
, not the integer 1
. If I pass this thing to the rest of my codebase which is expecting it to be an instance of EnumThing
, it breaks. So I end up converting it back to the instance from the integer that Graphene gave me.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
@radix yep this is a change that we are going to make as part of v3. I’ve created a PR for it here: https://github.com/graphql-python/graphene/pull/1153
This was resolved in #1153