Description
Consider the code below:
import enum
from ariadne import QueryType
from ariadne import EnumType
from ariadne import gql
from ariadne import graphql_sync
from ariadne import make_executable_schema
type_defs = gql("""
type Query{
test: E!
}
enum E {
A
B
C
}
""")
query_type = QueryType()
class E(enum.IntEnum):
A = 1
B = 2
C = 3
@query_type.field("test")
def resolve(root, info):
return E.A
schema = make_executable_schema(
type_defs,
query_type,
EnumType("E", E),
)
graphql_sync(
schema,
{"query": "query {test}"}
)
This is a simple resolver which returns an enum value. Everything works as expected - the python enum is converted into graphql enum. So far so good.
According to ariadne's documentation, we can even return value of enum's member and still get it working. Let's check that:
@query_type.field("test")
def resolve(root, info):
return 1
This also works as expected, the value 1
is recognised as a valid value for enum E
and everything is fine.
However, according to ariadne's documentation: "In this example we've used IntEnum, but custom enum can be any subtype of Enum type.". Let's try that one:
class E(enum.Enum):
A = "A"
B = "B"
C = "C"
@query_type.field("test")
def resolve(root, info):
return "A"
And this is where we're getting error: Enum 'E' cannot represent value: 'A'
. I suppose that my code is valid and the above example should work for value "A"
in the same manner it worked for value 1
in the example with enum.IntEnum
.