Skip to content

Commit

Permalink
Add flag for using enum names or members as values (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cito committed Jan 13, 2022
1 parent 9c02a9f commit a3e7b48
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/graphql/type/definition.py
Expand Up @@ -1091,14 +1091,18 @@ def assert_union_type(type_: Any) -> GraphQLUnionType:

class GraphQLEnumTypeKwargs(GraphQLNamedTypeKwargs, total=False):
values: GraphQLEnumValueMap
names_as_values: Optional[bool]


class GraphQLEnumType(GraphQLNamedType):
"""Enum Type Definition
Some leaf values of requests and input values are Enums. GraphQL serializes Enum
values as strings, however internally Enums can be represented by any kind of type,
often integers. They can also be provided as a Python Enum.
often integers. They can also be provided as a Python Enum. In this case, the flag
`names_as_values` determines what will be used as internal representation. The
default value of `False` will use the enum values, the value `True` will use the
enum names, and the value `None` will use the members themselves.
Example::
Expand Down Expand Up @@ -1132,6 +1136,7 @@ def __init__(
self,
name: str,
values: Union[GraphQLEnumValueMap, Mapping[str, Any], Type[Enum]],
names_as_values: Optional[bool] = False,
description: Optional[str] = None,
extensions: Optional[Dict[str, Any]] = None,
ast_node: Optional[EnumTypeDefinitionNode] = None,
Expand All @@ -1158,10 +1163,13 @@ def __init__(
f"{name} values must be an Enum or a mapping"
" with value names as keys."
)
values = cast(Dict, values)
values = cast(Dict[str, Any], values)
else:
values = cast(Dict, values)
values = {key: value.value for key, value in values.items()}
values = cast(Dict[str, Enum], values)
if names_as_values is False:
values = {key: value.value for key, value in values.items()}
elif names_as_values is True:
values = {key: key for key in values}
values = {
assert_enum_value_name(key): value
if isinstance(value, GraphQLEnumValue)
Expand Down
45 changes: 45 additions & 0 deletions tests/type/test_definition.py
@@ -1,3 +1,4 @@
from enum import Enum
from math import isnan, nan
from typing import cast, Dict

Expand Down Expand Up @@ -1057,6 +1058,50 @@ def rejects_a_union_type_with_incorrect_extension_ast_nodes():


def describe_type_system_enums():
def defines_an_enum_using_a_dict():
enum_type = GraphQLEnumType("SomeEnum", {"RED": 1, "BLUE": 2})
assert enum_type.values == {
"RED": GraphQLEnumValue(1),
"BLUE": GraphQLEnumValue(2),
}

def defines_an_enum_using_an_enum_value_map():
red, blue = GraphQLEnumValue(1), GraphQLEnumValue(2)
enum_type = GraphQLEnumType("SomeEnum", {"RED": red, "BLUE": blue})
assert enum_type.values == {"RED": red, "BLUE": blue}

def defines_an_enum_using_a_python_enum():
colors = Enum("Colors", "RED BLUE")
enum_type = GraphQLEnumType("SomeEnum", colors)
assert enum_type.values == {
"RED": GraphQLEnumValue(1),
"BLUE": GraphQLEnumValue(2),
}

def defines_an_enum_using_values_of_a_python_enum():
colors = Enum("Colors", "RED BLUE")
enum_type = GraphQLEnumType("SomeEnum", colors, names_as_values=False)
assert enum_type.values == {
"RED": GraphQLEnumValue(1),
"BLUE": GraphQLEnumValue(2),
}

def defines_an_enum_using_names_of_a_python_enum():
colors = Enum("Colors", "RED BLUE")
enum_type = GraphQLEnumType("SomeEnum", colors, names_as_values=True)
assert enum_type.values == {
"RED": GraphQLEnumValue("RED"),
"BLUE": GraphQLEnumValue("BLUE"),
}

def defines_an_enum_using_members_of_a_python_enum():
colors = Enum("Colors", "RED BLUE")
enum_type = GraphQLEnumType("SomeEnum", colors, names_as_values=None)
assert enum_type.values == {
"RED": GraphQLEnumValue(colors.RED),
"BLUE": GraphQLEnumValue(colors.BLUE),
}

def defines_an_enum_type_with_a_description():
description = "nice enum"
enum_type = GraphQLEnumType(
Expand Down

0 comments on commit a3e7b48

Please sign in to comment.