Skip to content

Commit 31fb2da

Browse files
committed
Move enum middleware into compat module
1 parent 1ba67ac commit 31fb2da

File tree

4 files changed

+37
-27
lines changed

4 files changed

+37
-27
lines changed

graphene/compat/__init__.py

Whitespace-only changes.

graphene/compat/middleware.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from graphql import OperationType
2+
3+
from ..utils.str_converters import to_snake_case
4+
from ..types.definitions import GrapheneEnumType
5+
from ..types.utils import get_underlying_type
6+
7+
8+
def enum_value_convertor_middleware(next, root, info, **args):
9+
"""
10+
Compatibility middleware for upgrading to v3:
11+
12+
Convert enums to their values for mutation inputs, like the behaviour in v2
13+
"""
14+
operation = info.operation.operation
15+
if operation == OperationType.MUTATION:
16+
input_arguments = info.parent_type.fields[info.field_name].args
17+
for arg_name, arg in input_arguments.items():
18+
_type = get_underlying_type(arg.type)
19+
if isinstance(_type, GrapheneEnumType):
20+
# Convert inputs to value
21+
arg_name = to_snake_case(arg_name)
22+
input_value = args.get(arg_name, None)
23+
if input_value and isinstance(
24+
input_value, _type.graphene_type._meta.enum
25+
):
26+
args[arg_name] = args[arg_name].value
27+
28+
return next(root, info, **args)

graphene/types/tests/test_enum.py

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from textwrap import dedent
2-
from graphql import OperationType
32

4-
from graphene.utils.str_converters import to_snake_case
3+
from graphene.compat.middleware import enum_value_convertor_middleware
54
from ..argument import Argument
6-
from ..definitions import GrapheneEnumType
75
from ..enum import Enum, PyEnum
86
from ..field import Field
97
from ..inputfield import InputField
@@ -398,12 +396,6 @@ class MyMutations(ObjectType):
398396
assert results.data["setFavColor"]["favColor"] == Color.RED.name
399397

400398

401-
def get_underlying_type(_type):
402-
while hasattr(_type, "of_type"):
403-
_type = _type.of_type
404-
return _type
405-
406-
407399
def test_enum_mutation_compat():
408400
from enum import Enum as PyEnum
409401

@@ -436,23 +428,6 @@ def mutate(self, info, fav_color):
436428
class MyMutations(ObjectType):
437429
set_fav_color = SetFavColor.Field()
438430

439-
def enum_compat_middleware(next, root, info, **args):
440-
operation = info.operation.operation
441-
if operation == OperationType.MUTATION:
442-
input_arguments = info.parent_type.fields[info.field_name].args
443-
for arg_name, arg in input_arguments.items():
444-
_type = get_underlying_type(arg.type)
445-
if isinstance(_type, GrapheneEnumType):
446-
# Convert inputs to value
447-
arg_name = to_snake_case(arg_name)
448-
input_value = args.get(arg_name, None)
449-
if input_value and isinstance(
450-
input_value, _type.graphene_type._meta.enum
451-
):
452-
args[arg_name] = args[arg_name].value
453-
454-
return next(root, info, **args)
455-
456431
schema = Schema(query=Query, mutation=MyMutations)
457432

458433
results = schema.execute(
@@ -461,7 +436,7 @@ def enum_compat_middleware(next, root, info, **args):
461436
favColor
462437
}
463438
}""",
464-
middleware=[enum_compat_middleware],
439+
middleware=[enum_value_convertor_middleware],
465440
)
466441
assert not results.errors
467442

graphene/types/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ def get_type(_type):
4141
if inspect.isfunction(_type) or isinstance(_type, partial):
4242
return _type()
4343
return _type
44+
45+
46+
def get_underlying_type(_type):
47+
"""Get the underlying type even if it is wrapped in structures like NonNull"""
48+
while hasattr(_type, "of_type"):
49+
_type = _type.of_type
50+
return _type

0 commit comments

Comments
 (0)