question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default resolver does not work for Enums

See original GitHub issue

If no explicit resolver is given, graphene nicely resolves fields by trying to access the attribute of the same name on the current node. This does not work for Enum’s though, as illustrated by the following example:

import graphene


class SomeEnum(graphene.Enum):
    Bar = 0
    Baz = 1


class Query(graphene.ObjectType):

    foo = graphene.Field(SomeEnum)
    biz = graphene.Field(graphene.String)


class SomeObject:
    def __init__(self):
        self.foo = SomeEnum.Bar
        self.biz = 'foobar'


if __name__ == '__main__':
    obj = SomeObject()
    schema = graphene.Schema(Query)
    query = '{foo,biz}'
    result = schema.execute(query, root_value=obj)
    print(result.data['foo'])
    print(result.data['biz'])

This should print

Bar
foobar

is does however print

None
foobar

I would have expected this to either returnenum.value or raise an exception, instead of silently returning None.

The obvious solution is to add a custom resolver, of course:

class Query(graphene.ObjectType):
    foo = graphene.Field(SomeEnum)
    biz = graphene.Field(graphene.String)
    
    def resolve_foo(root, *args, **kwargs):
        return root.foo.value

I’d think we should either remove the implicit default-resolver altogether, or take care that all graphql-types are handled properly.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
lincolnqcommented, Oct 18, 2017

I also have this problem, and it’s a big blocker for me. However, I don’t like the suggested PR. I think it would be better to fix it at the GraphQLEnumType level (because this is really not an issue with the resolver – it’s an issue with the enum type itself not recognizing instances of enum as valid values), but I’m not sure how exactly is the best way to do it.

Example that clarifies that it’s not a resolver issue:

import graphene as gql

class Currency(gql.Enum):
    USD = 10
    GBP = 20

class Query(gql.ObjectType):
    currency1 = Currency()
    currency2 = Currency()
    def resolve_currency1(self, info):
        """Return a Currency value."""
        return 10
    def resolve_currency2(self, info):
        """Return a Currency instance."""
        return Currency.USD

schema = gql.Schema(query=Query)
assert schema.execute('{currency1}').data['currency1'] == 'USD' # works
assert schema.execute('{currency2}').data['currency2'] == 'USD' # fails, it's None
0reactions
ProjectCheshirecommented, Mar 11, 2019

Resolving per above and lack of further activity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't custom value of graphql enum - Stack Overflow
The resolver is the only thing impacted by providing internal values for each enum value. What doesn't change: How the enum value is...
Read more >
What you need to know about GraphQL enums
Learn how GraphQL enums can help you build more robust and discoverable APIs, create simple interfaces, maintain slim resolvers, and more.
Read more >
Enum Class (System) - Microsoft Learn
If there is no default case, consider using an enumerated constant whose value is zero to specify the case that is not represented...
Read more >
How To Serialize and Deserialize Enums with Jackson
How to serialize and deserialize an Enum as a JSON Object using ... By default, Jackson will represent Java Enums as a simple...
Read more >
typescript-resolvers - GraphQL Code Generator
By default apollo-server will not work with generated resolvers signature. If you are using Apollo Server with TypeScript, note that you need to ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found