graphene-django: Django choice fields cannot be blank, even if field is not required -- graphene-django 2.1.0 / graphql-core 2.1 regression

This seems to be a regression in the upgrade of graphql-core to 2.1 together withj graphene-django to 2.1.0.

Prior to the upgrade, I could create an optional choice field in Django, and could query the value if the value of the choice field was blank.

For example, assume that I have a Django model MyModel with an optional choice field my_choices_field, and a query handler that would accept a query like

{
  myModel(id: 1) {
    id
    myChoicesField
  }
}

With the updated versions, if the value of my_choices_field is blank, this query throws a handled exception like:

 Traceback (most recent call last):
   File "/usr/local/lib/python3.7/site-packages/promise/promise.py", line 65, in try_catch
     return (handler(*args, **kwargs), None)
   File "/usr/local/lib/python3.7/site-packages/graphql/execution/executor.py", line 527, in <lambda>
     exe_context, return_type, field_asts, info, path, resolved
   File "/usr/local/lib/python3.7/site-packages/graphql/execution/executor.py", line 556, in complete_value
     return complete_leaf_value(return_type, path, result)
   File "/usr/local/lib/python3.7/site-packages/graphql/execution/executor.py", line 622, in complete_leaf_value
     path=path,
 graphql.error.base.GraphQLError: Expected a value of type "MyModelMyChoicesField" but received:

And the output of the query includes errors when the value of the field is blank:

{
  "errors": [
    {
      "message": "Expected a value of type \"MyModelMyChoicesField\" but received: ",
      "path": [
        "myModel",
        "myChoicesField"
      ]
    }
  ],
  "data": {
    "myModel": {
      "id": "1",
      "myChoicesField": null
    }
  }
}

I imagine the issue is related to the work in graphql-core to related to enum fields

Improved GraphQLEnumType serialization allowing python Enum values 0cef6fb 24687b6

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 2
  • Comments: 22 (4 by maintainers)

Most upvoted comments

I figured out a workaround. In this example gender is an Enum:

class UserNode(DjangoObjectType):
    gender = graphene.Field(graphene.String, source='gender')

    class Meta:
        model = User
        only_fields = ('id', 'gender')

Does anyone know when this bug will be fixed?

@atomboulian Yes I can confirm that it’s a CharField, For example, I have a field defined like

PERIODIC_INTERVAL_CHOICES = (('Weekly', 'Weekly'),
     ('Bi-Weekly', 'Bi-Weekly'),
      ('Monthly', 'Monthly'),
      ('Quarterly', 'Quarterly'),
      ('Semi-Annually', 'Semi-Annually'),
      'Annually', 'Annually'))

payment_frequency = models.CharField(
    blank=True,
    null=True,
    choices=PERIODIC_INTERVAL_CHOICES,
    max_length=13)

I get the error whenever fetching “payment_frequency” and there is a blank or null value for that object.

I also get the error if null=True is removed, which is actually the correct way to represent an empty value in this case.