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.

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

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
dspacejscommented, Apr 18, 2019

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?

4reactions
picturedotscommented, Aug 1, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django ForeignKey field ignored by graphene-django when ...
It seems this is the problem, but it isn't. The problem is that you didn't register a Cable type. When you look into...
Read more >
An Introduction to GraphQL in Django | by Moeedlodhi - Medium
I'll be honest, The reason I looked into GraphQL is that a lot of jobs I was applying for had GraphQL as a...
Read more >
Graphene Documentation - Read the Docs
This is a powerful way for a Query to describe the exact data requirements for each Field. For each Field in our Schema,...
Read more >
graphene-django-extras - PyPI
This library add some extra funcionalities to graphene-django to ... to use as input on an Arguments class on traditional Mutations " model...
Read more >
graphene-django-extras - Bountysource
We have started working on migration from Travis to GitHub Action. We need few things from you. Update/missing GitHub Actions environment variables with ......
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