graphene-django icon indicating copy to clipboard operation
graphene-django copied to clipboard

DRF ChoiceField does not work with Graphene-Django

Open jmp opened this issue 3 years ago • 3 comments

When using SerializerMutation with Graphene-Django 3.0.0b7, DRF's ChoiceField doesn't seem to work correctly:

  1. Two identical enum types (but with different names) are created in the GraphQL schema
  2. Using any of the choices in the mutation input results in an error like"\"foo.BAR\" is not a valid choice."

Minimal demo project

Reproduction steps:

  1. Clone the demo project:
    git clone [email protected]:jmp/graphene-django-enum-example.git
    
  2. Set up the project:
    python3 -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
    
  3. Generate the GraphQL schema:
    python manage.py graphql_schema --out schema.graphql
    
  4. Open schema.graphql and you can see an enum AppDummyFooChoices defined as
    enum AppDummyFooChoices {
      """First"""
      FIRST
    
      """Second"""
      SECOND
    
      """Third"""
      THIRD
    }
    
    In the schema, you can also see a duplicate enum defined as
    enum foo {
      """First"""
      FIRST
    
      """Second"""
      SECOND
    
      """Third"""
      THIRD
    }
    
  5. Run the project, navigate to http://127.0.0.1:8000/graphql and run the following query:
    mutation {
      dummyMutation(input: {
        foo: FIRST
      }) {
        id
        errors {
          field
          messages
        }
      }
    }
    
    You will get an error:
    {
      "data": {
        "dummyMutation": {
          "id": null,
          "errors": [
            {
              "field": "foo",
              "messages": [
                "\"foo.FIRST\" is not a valid choice."
              ]
            }
          ]
        }
      }
    }
    

Expected behavior:

  1. In the GraphQL schema, there should be only one enum type AppDummyFooChoices.
  2. There should be no error when trying to use the enum value FIRST in the mutation

Related issues:

  • #517

Environment:

  • Version: 3.0.0b7
  • Platform: macOS

jmp avatar Dec 04 '21 14:12 jmp

any progress for this issue?!

hishamkaram avatar Oct 03 '22 07:10 hishamkaram

Facing the same issue on v3.1.3

diogosilva30 avatar Oct 15 '23 00:10 diogosilva30

Workaround is to add convert_choices_to_enum = False to the meta class of both the ObjectType and Mutation classes for that model type.

keeth avatar Apr 03 '24 20:04 keeth