Search code examples
djangographene-pythongraphene-django

Create Custom object in graphene-django to return grouped counts


I'm trying to build a way to return grouped counts of another object based on specific filtering criteria. As an example, given the following model with the following sample data, I'd like to create a call that will return the following result:

The Model:

class Task(models.Model):
    class Meta:
        db_table = "task"

    id = models.UUIDField(
        db_column="task_id", primary_key=True, default=uuid.uuid4, editable=False
    )
    start_date = models.DateTimeField()
    due_date = models.DateTimeField()
    task_status = models.CharField(max_length=250)
    )

Sample Data:

id           start_date     due_date     task_status
624d8126...  2021-01-01     2021-03-02   in progress

171a7969...  2021-03-01     2021-02-28   assigned

92f6e493...  2021-04-01     2021-04-10   completed

a5722f6f...  2021-04-03     2021-04-08   assigned

e884efcb...  2021-05-01     2021-04-30   available

Desired Result (or something similar)

getTaskCounts
{
    taskCount
    {
      countType: "behind schedule",
      count: 2
    },
    taskCount
    {
      countType: "in progress",
      count:  2
    },
    taskCount
    {
      countType: "completed",
      count:  1
    }

}

Note I'm grouping the various statuses based on the desired result.


Solution

  • You can define custom object type like:

    class TaskCount(graphene.ObjectType):
        count_type = graphene.String()
        count = graphene.Int()
    

    and then define another object to return a grouped list of them like:

    from django.db.models import Count
    
    class TaskCounts(graphene.ObjectType):
        task_counts = graphene.List(TaskCount)
    
        def resolve_task_counts(self, info):
            # Query for grouping -- test in Django shell
            grouped_tasks = Task.objects
                .values('task_status')
                .annotate(count=Count('task_status'))
                .order_by()
            return [
                TaskCount(count_type=grouped_task.task_status, count=grouped_task.count) for grouped_task in grouped_tasks
             ]