mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-22 13:59:51 +03:00
Remove enum compat middleware
This commit is contained in:
parent
0d083b1c66
commit
9f28d2fde4
|
@ -1,47 +0,0 @@
|
||||||
from graphql import OperationType
|
|
||||||
|
|
||||||
from ..utils.str_converters import to_snake_case
|
|
||||||
from ..types.definitions import GrapheneEnumType, GrapheneInputObjectType
|
|
||||||
from ..types.utils import get_underlying_type
|
|
||||||
|
|
||||||
|
|
||||||
def convert_enum_args(args, input_arguments):
|
|
||||||
new_args = {}
|
|
||||||
|
|
||||||
for arg_name, arg in input_arguments.items():
|
|
||||||
_type = get_underlying_type(arg.type)
|
|
||||||
|
|
||||||
arg_name = to_snake_case(arg_name)
|
|
||||||
input_value = args.get(arg_name, None)
|
|
||||||
|
|
||||||
if isinstance(_type, GrapheneEnumType):
|
|
||||||
# Convert inputs to value
|
|
||||||
if input_value and isinstance(input_value, _type.graphene_type._meta.enum):
|
|
||||||
new_args[arg_name] = input_value.value
|
|
||||||
else:
|
|
||||||
new_args[arg_name] = input_value
|
|
||||||
elif isinstance(_type, GrapheneInputObjectType):
|
|
||||||
_input_arguments = get_underlying_type(arg.type).fields
|
|
||||||
input_type = input_value.get_type()
|
|
||||||
new_args[arg_name] = input_type(
|
|
||||||
**convert_enum_args(input_value, _input_arguments)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
new_args[arg_name] = input_value
|
|
||||||
|
|
||||||
return new_args
|
|
||||||
|
|
||||||
|
|
||||||
def enum_value_convertor_middleware(next, root, info, **args):
|
|
||||||
"""
|
|
||||||
Compatibility middleware for upgrading to v3:
|
|
||||||
|
|
||||||
Convert enums to their values for mutation inputs, like the behaviour in v2
|
|
||||||
"""
|
|
||||||
operation = info.operation.operation
|
|
||||||
if operation == OperationType.MUTATION:
|
|
||||||
input_arguments = info.parent_type.fields[info.field_name].args
|
|
||||||
|
|
||||||
new_args = convert_enum_args(args, input_arguments)
|
|
||||||
|
|
||||||
return next(root, info, **new_args)
|
|
|
@ -1,7 +1,5 @@
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
from graphene.compat.middleware import enum_value_convertor_middleware
|
|
||||||
|
|
||||||
from ..argument import Argument
|
from ..argument import Argument
|
||||||
from ..enum import Enum, PyEnum
|
from ..enum import Enum, PyEnum
|
||||||
from ..field import Field
|
from ..field import Field
|
||||||
|
@ -473,101 +471,3 @@ def test_mutation_enum_input_type():
|
||||||
assert result.data == {"createPaint": {"color": "RED"}}
|
assert result.data == {"createPaint": {"color": "RED"}}
|
||||||
|
|
||||||
assert color_input_value == RGB.RED
|
assert color_input_value == RGB.RED
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_enum_input_compatability_middleware():
|
|
||||||
"""Test the `enum_value_convertor_middleware`"""
|
|
||||||
|
|
||||||
class RGB(Enum):
|
|
||||||
"""Available colors"""
|
|
||||||
|
|
||||||
RED = 1
|
|
||||||
GREEN = 2
|
|
||||||
BLUE = 3
|
|
||||||
|
|
||||||
color_input = None
|
|
||||||
|
|
||||||
class CreatePaint(Mutation):
|
|
||||||
class Arguments:
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
def mutate(_, info, color):
|
|
||||||
nonlocal color_input
|
|
||||||
color_input = color
|
|
||||||
return CreatePaint(color=color)
|
|
||||||
|
|
||||||
class MyMutation(ObjectType):
|
|
||||||
create_paint = CreatePaint.Field()
|
|
||||||
|
|
||||||
class Query(ObjectType):
|
|
||||||
a = String()
|
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutation)
|
|
||||||
result = schema.execute(
|
|
||||||
""" mutation MyMutation {
|
|
||||||
createPaint(color: RED) {
|
|
||||||
color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
""",
|
|
||||||
middleware=[enum_value_convertor_middleware],
|
|
||||||
)
|
|
||||||
assert not result.errors
|
|
||||||
assert result.data == {"createPaint": {"color": "RED"}}
|
|
||||||
|
|
||||||
assert color_input == 1
|
|
||||||
assert type(color_input) == int
|
|
||||||
|
|
||||||
|
|
||||||
def test_mutation_enum_input_compatability_middleware_input_type():
|
|
||||||
"""Test the `enum_value_convertor_middleware`"""
|
|
||||||
|
|
||||||
class RGB(Enum):
|
|
||||||
"""Available colors"""
|
|
||||||
|
|
||||||
RED = 1
|
|
||||||
GREEN = 2
|
|
||||||
BLUE = 3
|
|
||||||
|
|
||||||
class SecondColorInput(InputObjectType):
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
class ColorInput(InputObjectType):
|
|
||||||
color_input = SecondColorInput(required=True)
|
|
||||||
|
|
||||||
color_input_value = None
|
|
||||||
|
|
||||||
class CreatePaint(Mutation):
|
|
||||||
class Arguments:
|
|
||||||
color_input = ColorInput(required=True)
|
|
||||||
|
|
||||||
color = RGB(required=True)
|
|
||||||
|
|
||||||
def mutate(_, info, color_input):
|
|
||||||
nonlocal color_input_value
|
|
||||||
color_input_value = color_input.color_input.color
|
|
||||||
return CreatePaint(color=color_input_value)
|
|
||||||
|
|
||||||
class MyMutation(ObjectType):
|
|
||||||
create_paint = CreatePaint.Field()
|
|
||||||
|
|
||||||
class Query(ObjectType):
|
|
||||||
a = String()
|
|
||||||
|
|
||||||
schema = Schema(query=Query, mutation=MyMutation)
|
|
||||||
result = schema.execute(
|
|
||||||
""" mutation MyMutation {
|
|
||||||
createPaint(colorInput: { colorInput: { color: RED } }) {
|
|
||||||
color
|
|
||||||
}
|
|
||||||
}
|
|
||||||
""",
|
|
||||||
middleware=[enum_value_convertor_middleware],
|
|
||||||
)
|
|
||||||
assert not result.errors
|
|
||||||
assert result.data == {"createPaint": {"color": "RED"}}
|
|
||||||
|
|
||||||
assert color_input_value == 1
|
|
||||||
assert type(color_input_value) == int
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user