Skip to content

I would like my enum input values to be the enum instance instead of the enum values #1151

Closed
@radix

Description

@radix

Is 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions