Skip to content

Error when trying to serialize a Python enum #73

Closed
@akdor1154

Description

@akdor1154

When returning a Python Enum-type object, I get an exception:
graphql.error.graphql_error.GraphQLError: Expected a value of type 'MyEnum' but received: <MyEnum instance>

Reproducible example:

from graphql import GraphQLSchema, graphql_sync, GraphQLObjectType, GraphQLField, GraphQLEnumType
from traceback import TracebackException
from enum import Enum

class MyEnum(Enum):
    Yes = 'YES'
    No = 'NO'

schema = GraphQLSchema(
    query=GraphQLObjectType(
        name='rootquery',
        fields={
            'testenum': GraphQLField(
                GraphQLEnumType('MyEnum', MyEnum)
            )
        }
    )
)
query = '{ testenum }'
root = {'testenum': MyEnum.Yes}
result = graphql_sync(schema, query, {'testenum': MyEnum.Yes})

try:
    assert result.data == {'testenum': 'YES'}
except:
    for e in result.errors:
        print(''.join(TracebackException.from_exception(e).format()))
    raise

Metadata

Metadata

Assignees

Labels

investigateNeeds investigaton or experimentation

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions