Search code examples
djangographqlchoicefieldariadne-graphql

Ariadne graphql displaying Django choices in query


I have a django model with Integer Choice fields

class issue_status(models.IntegerChoices):
  '''Status'''

  Open = 1
  Pending = 2
  Completed = 3



  class Issue(models.Model):

    person = models.ForeignKey('Person', on_delete=models.CASCADE,
                               blank=False, null=False)
    name = models.CharField(max_length=100, null=True, blank=True)
    status = models.IntegerField(choices=issue_status.choices,
                                 blank=True, null=True)
    def __str__(self):
        return self.name

I have my graphql types and resolver defined like this

from ariadne import gql

type_defs = gql("""

enum Status {
  Open
  Pending
  Completed
 }

type Issue {
    id: ID
    person: Person!
    name: String
    status: Status
   }

input IssueInput {
    id: ID
    person: PersonInput!
    name: String
    status: Status
   }

input IssueUpdate {
    id: ID!
    person: PersonInput
    name: String
    status: Status
   }

type Query {
    Issues(id: ID, name:String, ): [Issue]
}
""")

query = QueryType()

@query.field("Issue")
def resolve_Issue(*_, name=None, id=None,):
    if name:
        filter = Q(name__icontains=name)
        return Issue.objects.filter(filter)
    if id:
        filter = Q(id__exact=id)
        return Issue.objects.filter(filter)
    return Issue.objects.all()

When I do a gql query I get null values in the status field:

{
  "data": {
    "Issues": [
      {
        "person": {
          "name": "Test1"
        },
        "id": "1",
        "name": "Things",
        "status": null
      },
      {
        "person": {
          "name": "Test1"
        },
        "id": "2",
        "name": "Clown and clown",
        "status": null
      }
    ]
  },
  "errors": [
    {
      "message": "Enum 'Status' cannot represent value: 1",
      "locations": [
        {
          "line": 9,
          "column": 5
        }
      ],
      "path": [
        "Issues",
        0,
        "status"
      ],
      "extensions": {
        "exception": null
      }
    },
    {
      "message": "Enum 'Status' cannot represent value: 1",
      "locations": [
        {
          "line": 9,
          "column": 5
        }
      ],
      "path": [
        "Issues",
        1,
        "status"
      ],
      "extensions": {
        "exception": null
      }
    }
  ]
}

How do I setup the Integer choices in Ariadne to return String values for the integer choices? In the docs there is an example using it in the resolver but I am not sure how to include or return it in the query:

import enum
from ariadne import EnumType
class Status(enum.IntEnum):
  Open = 1
  Pending = 2
  Completed = 3

Solution

  • The graphql for the enum must be defined:

    type_defs = gql("""
    
    enum Status {
      Open
      Pending
      Completed
     }
    
    """)
    

    Then the numerical choices should be defined and added to the make_executable_schema

    import enum
    from ariadne import EnumType
    
    class Status(enum.IntEnum):
        Open = 1
        Pending= 2
        Completed = 3
    
    status_enum = EnumType("Status", Status)
    
    schema = make_executable_schema(type_defs, query, mutation, status_enum)