graphql-python / graphql-core-legacy

GraphQL base implementation for Python (legacy version – see graphql-core for the current one)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Null value from non-required enum field raises exception

picturedots opened this issue · comments

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

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

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.

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)

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.

@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.

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