mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-18 12:02:19 +03:00
Handle invalid enum values
This commit is contained in:
parent
806c957d2a
commit
7d4593fcfa
|
@ -45,8 +45,11 @@ class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType):
|
||||||
# Try and get enum by value
|
# Try and get enum by value
|
||||||
value = enum(value)
|
value = enum(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
# Try ang get enum by name
|
# Try and get enum by name
|
||||||
value = enum[value]
|
try:
|
||||||
|
value = enum[value]
|
||||||
|
except KeyError:
|
||||||
|
value = None
|
||||||
return super(GrapheneEnumType, self).serialize(value)
|
return super(GrapheneEnumType, self).serialize(value)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -321,6 +321,32 @@ def test_enum_resolver_compat():
|
||||||
assert results.data["colorByName"] == Color.RED.name
|
assert results.data["colorByName"] == Color.RED.name
|
||||||
|
|
||||||
|
|
||||||
|
def test_enum_resolver_invalid():
|
||||||
|
from enum import Enum as PyEnum
|
||||||
|
|
||||||
|
class Color(PyEnum):
|
||||||
|
RED = 1
|
||||||
|
GREEN = 2
|
||||||
|
BLUE = 3
|
||||||
|
|
||||||
|
GColor = Enum.from_enum(Color)
|
||||||
|
|
||||||
|
class Query(ObjectType):
|
||||||
|
color = GColor(required=True)
|
||||||
|
|
||||||
|
def resolve_color(_, info):
|
||||||
|
return "BLACK"
|
||||||
|
|
||||||
|
schema = Schema(query=Query)
|
||||||
|
|
||||||
|
results = schema.execute("query { color }")
|
||||||
|
assert results.errors
|
||||||
|
assert (
|
||||||
|
results.errors[0].message
|
||||||
|
== "Expected a value of type 'Color' but received: 'BLACK'"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_enum_mutation():
|
def test_enum_mutation():
|
||||||
from enum import Enum as PyEnum
|
from enum import Enum as PyEnum
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user