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.

Choice fields automatically converted to enums

See original GitHub issue
class User(DjangoObjectType):
    class Meta:
        model = get_user_model()
        only_fields = ['signup_site_version']

class Query(graphene.ObjectType):
    user = graphene.Field(User)

    def resolve_user(self, args, context, info):
        return user_from_info(info)  # return django user from context

User model:

@python_2_unicode_compatible
class User(AbstractUser):
    (...)
    signup_site_version_choices = [(1, u'site 1'), (2, u'site 2')]
    signup_site_version = models.IntegerField(choices=signup_site_version_choices)

Query

query { 
  user {
    signupSiteVersion
  }
}

Result

{
  "data": {
    "user": {
      "signupSiteVersion": "A_1"
    }
  }
}

A copy-paste from where the weird value is born:

def convert_choice_name(name):
    name = to_const(force_text(name))
    try:
        assert_valid_name(name)
    except AssertionError:
        name = "A_%s" % name
    return name

We’ve had similar problems in the old graphene (< 1.0): https://github.com/graphql-python/graphene/issues/134 The output is “A_1”, because the field converter assumes django choices are tuples (id, name) where name is a constant name (not containing spaces etc.) or is convertible to a valid constant name. For some reason it is not successful while doing that for “site 1”, but that is not an issue. The problem is it assumes we would like to do that. In our django application (I suppose it is the norm) we use the second tuple element as a human-readable label. It means that in most cases replacing space with an underscore will do the work, but in the other names are still bound to be long and there is no character we can safely assume the name won’t contain (including hyphens, all native character varieties etc.). The Enum fields are certainly nice, as their values are schema-validated, to say the least, but I doubt automatic conversion should be the only possible behaviour.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:19
  • Comments:22 (7 by maintainers)

github_iconTop GitHub Comments

17reactions
BrianChapmancommented, Apr 14, 2017

Choices are also converted to uppercase, which makes it difficult to manage during mutations, especially if the choices were mixed upper and lower case. There should be an option to just leave your choices alone.

6reactions
jkimbocommented, Jul 13, 2019

@kaglowka v2.4.0 contains a new feature which allows you to turn off automatic enum creation on a case by case basis: http://docs.graphene-python.org/projects/django/en/latest/queries/#choices-to-enum-conversion

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use enums as a choice field in django model
Use .choices as a suitable value to pass to choices in a field ... By the way Djanog also supports the Python 3's...
Read more >
27910 (Allow using an Enum class in model Field choices)
Description. I want to limit the input to certain choices so I create a list/tuple of tuples/lists. Since Python 3.4 there is a...
Read more >
Language Guide (proto3) | Protocol Buffers - Google Developers
For enums, the default value is the first defined enum value, which must be 0. For message fields, the field is not set....
Read more >
enum — Support for enumerations — Python 3.11.1 ...
Using auto with IntEnum results in integers of increasing value, starting with 1 . Changed in version 3.11: __str__() is now int.__str__() ...
Read more >
Value Conversions - EF Core - Microsoft Learn
Value converters allow property values to be converted when ... but EF Core will actually do this automatically when the provider type is ......
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