Skip to content

Null value from non-required enum field raises exception #201

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
picturedots opened this issue Jul 26, 2018 · 6 comments
Closed

Null value from non-required enum field raises exception #201

picturedots opened this issue Jul 26, 2018 · 6 comments

Comments

@picturedots
Copy link

I've been searching for the underlying cause of the error in graphene-django that breaks Django choice fields when the value is blank: graphql-python/graphene-django#474 . This is a regression, new since graphql-core 2.1.

The validation for an enum value new in version 2.1 doesn't seem to take into account whether or not the field is allowed to have a null value before raising an exception.

From https://github.com/graphql-python/graphql-core/blob/fa4eeda36029680205e20059379e89189b946032/graphql/execution/executor.py#L605

if serialized_result is None:
    raise GraphQLError(
        ('Expected a value of type "{}" but ' + "received: {}").format(
            return_type, result
        ),
        path=path,
    )

Should this exception be the exception shouldn't be raised unconditionally? The graphql source has the same pattern in it but perhaps catches the error ... https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L967

@atomboulian
Copy link

I'm experiencing the same issue. Please help <3

@adamkowalczyk
Copy link

adamkowalczyk commented Oct 12, 2018

Also experiencing this issue I think.

After upgrading from 2.0.0 to 2.1.0 an enum key with a null value no longer works as an argument.
e.g.

Argument "argumentName" has invalid value NONE.
Expected type "enum_name", found NONE.

Where NONE is the key in the enum, not the value i.e. NONE = None

Seems like this resolved issue from graphql-js might be related.

@gabelimon
Copy link

I'm experiencing this issue as well. Is there a recommended way to let enum fields be None/null? Example

import pytz
import graphene
from graphene_django.converter import convert_choice_name
from enum import Enum


OptionsEnum = Enum(value='TimezoneOptions',
                   names=[(convert_choice_name(tz), tz)
                          for tz in pytz.common_timezones])


TimezoneOptions = graphene.Enum.from_enum(
    OptionsEnum, description=lambda tz: tz and tz.value)


def timezone_resolver(model, _):
    tz = model.timezone
    return TimezoneOptions.get(tz.zone if hasattr(tz, 'zone') else tz)

# Part of my UserNode, it's not everything
class UserNode(DjangoObjectType):
    timezone = graphene.Field(TimezoneOptions,
                              resolver=timezone_resolver,
                              required=False)

@jarcoal
Copy link

jarcoal commented Dec 17, 2019

I'm curious if one of the maintainers could weigh in on this. We have a lot of places in our codebase where we'd like to have nullable enums, but if this isn't getting fixed we'll probably go another direction.

@Cito
Copy link
Member

Cito commented Dec 17, 2019

@jarcoal: This repository is for v2 of graphql-core only which is not under active development any more. Current development in v3 has moved to https://github.com/graphql-python/graphql-core-next.

@jarcoal
Copy link

jarcoal commented Dec 17, 2019

@Cito thanks for the heads up, I'll start tracking graphene's progress on switching over to that.

@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

6 participants