djangographene-pythongraphene-django

Graphene Enums not working when using without creating a new class


From the documentation, it's evident that we are not bound to create new classes for using Enums.

I have the following code snippet:

from graphene import Enum, InputObjectType

GRAPH_TYPES = [
    ('step', 'Step graph'),
    ('bar', 'Bar graph'),
    ('line', 'Line graph'),
    ('dot', 'Dot graph'),
]


class DataType(Enum):
    VELOCITY = 'velocity'
    ACCELERATION = 'accelration'


class SomeInput(InputObjectType):
    data_type = DataType('DataTypeEnum')
    graph_type = Enum('GraphTypeEnum', GRAPH_TYPES)

When I head over to GraphiQL, I am able to see SomeInput but graph_type is missing inside.

Package versions:


Solution

  • For anyone who stumbles upon this, it turns out to be something related to the initialization of the declared Enum.

    The inline declaration Enum('GraphTypeEnum', GRAPH_TYPES) has to be updated like this:

    Enum('GraphTypeEnum', GRAPH_TYPES)().