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.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
jkimbocommented, Mar 14, 2020

@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

1reaction
jkimbocommented, Oct 21, 2020

This was resolved in #1153

Read more comments on GitHub >

github_iconTop Results From Across the Web

Attaching Values to Java Enum - Baeldung
The Java enum type provides a language-supported way to create and use constant values. By defining a finite set of values, the enum...
Read more >
How to get an enum value from a string value in Java
An enum class automatically gets a static valueOf() method in the class when compiled. The valueOf() method can be used to obtain an...
Read more >
Java Enum Tutorial: 10 Examples of Enum in Java
Let's see an Enum example in Java to understand the concept better. In this example, we will use US Currency Coin as enumerable...
Read more >
enum in Java - GeeksforGeeks
Enums are used when we know all possible values at compile time, such as choices on a menu, rounding modes, command-line flags, etc....
Read more >
Beginner's Guide to Java eNum – Why and for What should I ...
You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible...
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