Default resolver does not work for Enums
See original GitHub issueIf 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:
- Created 6 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Resolving per above and lack of further activity.