Skip to content

Missing return in GraphQLEnumType.serialize #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
congocongo opened this issue Jul 23, 2018 · 4 comments
Closed

Missing return in GraphQLEnumType.serialize #199

congocongo opened this issue Jul 23, 2018 · 4 comments

Comments

@congocongo
Copy link

Guess there must be a return for PyEnum type

https://github.com/graphql-python/graphql-core/blob/02605b1adce7b287fa9ee6beacd735882954159a/graphql/type/definition.py#L518-L528

@congocongo
Copy link
Author

I meant if we initialize the enumeration by native Enum, then serializes function returns None instead members name.

for example:

import collections
from graphql.pyutils.compat import Enum as PyEnum
from graphql.type.definition import GraphQLEnumType, GraphQLEnumValue


class Color(PyEnum):
    RED = 'RED'
    GREEN = 'GREEN'
    BLUE = 'BLUE'


# init as Enum members
enum_type = GraphQLEnumType('Color', values={
                                             'RED': GraphQLEnumValue(Color.RED),
                                             'GREEN': GraphQLEnumValue(Color.GREEN),
                                             'BLUE': GraphQLEnumValue(Color.BLUE)
                                            })

print(enum_type.serialize(Color.RED))  # None
print(enum_type.serialize(Color.GREEN))  # None
print(enum_type.serialize(Color.BLUE))  # None


# correct serialize method
def serialize(self, value):
    # type: (Union[str, PyEnum]) -> Optional[str]
    if isinstance(value, (collections.Hashable, PyEnum)):
        enum_value = self._value_lookup.get(value)
        if enum_value:
            return enum_value.name

    return None

GraphQLEnumType.serialize = serialize

print(enum_type.serialize(Color.RED))  # RED
print(enum_type.serialize(Color.GREEN))  # GREEN
print(enum_type.serialize(Color.BLUE))  # BLUE

@janosroden
Copy link

janosroden commented Feb 20, 2019

This PR #198 solves this issue

@rafales
Copy link

rafales commented Jul 18, 2019

Any news on this? It breaks using standard enum module.

@Cito
Copy link
Member

Cito commented Jul 19, 2019

Has been merged now, will be in next patch release.

@Cito Cito closed this as completed Jan 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants