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.

How would one implement totalCount or count on DjangoFilterConnectionField?

See original GitHub issue

With Connection, one can implement a count on edges like so:

from graphene import Connection, ConnectionField, Node, Int
from graphene_django import DjangoObjectType
from ..models import Place

class Thing_Type(DjangoObjectType):
    class Meta:
        model = Thing
        interfaces = (Node, )


class Thing_Connection(Connection):
    class Meta:
        node = Thing_Type
    count = Int()

    def resolve_count(root, info):
        return len(root.edges)


class Query(object):
    things = ConnectionField(Thing_Connection)

    def resolve_things(root, info, **kwargs):
        return Thing.objects.all()
   return len(root.edges)

Given that DjangoFilterConnectionField won’t accept a Connection, but requires a DjangoObjectType, how would one implement an equivalent count?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

38reactions
changelingcommented, May 14, 2019

@phalt, never mind. Got it. I’ll add this to the wiki FAQ once editing is enabled. Turns out the trick is to subclass Connection and declare that as a connection_class on the node type. DjangoFilterConnectionField then uses that declared connection_class class seamlessly, like so:

from graphene import ObjectType, Connection, Node, Int
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from ..models import Place


class ExtendedConnection(Connection):
    class Meta:
        abstract = True

    total_count = Int()
    edge_count = Int()

    def resolve_total_count(root, info, **kwargs):
        return root.length
    def resolve_edge_count(root, info, **kwargs):
        return len(root.edges)


class PlaceType(DjangoObjectType):
    class Meta:
        model = Place
        filter_fields = {
            'id':  ['exact', 'icontains'],
            'name': ['exact', 'icontains', 'istartswith', 'iendswith'],
            'date': ['exact', 'icontains', 'istartswith', 'iendswith'],
            'date_sort': ['exact', 'icontains', 'istartswith', 'iendswith'],
        }
        interfaces = (Node, )
        connection_class = ExtendedConnection


class Query(ObjectType):
    places = DjangoFilterConnectionField(PlaceType)

This allows, in my example here, querying:

{
  places(first: 2, name_Icontains: "Dallas" after:"YXJyYXljb25uZWN0aW9uOjE1") {
    totalCount
    edgeCount
    edges {
      cursor 
      node {
        id  
        name
      }
    }
  }
}

Which returns:

{
{
  "data": {
    "places": {
      "totalCount": 23,
      "edgeCount": 2,
      "edges": [
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjE2",
          "node": {
            "id": "UGxhY2VUeXBlOjUxOA==",
            "name": "Dallas, Texas, United States"
          }
        },
        {
          "cursor": "YXJyYXljb25uZWN0aW9uOjE3",
          "node": {
            "id": "UGxhY2VUeXBlOjU0Nw==",
            "name": "Dallas, Alabama, United States"
          }
        }
      ]
    }
  }
}
8reactions
slorg1commented, Oct 22, 2020

Is this an official resolution to the issue? It looks like a very clever hack around the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Send the response as count of members who have liked a ...
This is how I resolved it. import graphene from graphene import relay class CountableConnectionBase(relay.Connection): class Meta: abstract ...
Read more >
DjangoFilterConnectionField query for all records-django
Are you looking for getting all of the fields within a model as follows? from graphene_django import DjangoObjectType from model.path import Model class ......
Read more >
Count The Number Of Events and Venues - Django ... - YouTube
In this video I 'll show you how to add object model counts for our Event, Venue, and User models with Django and...
Read more >
Get total count in pagination query - Discuss Dgraph
Given the example above, let says the total count is 150, I want to implement pagination in my website, 10 products per page....
Read more >
How To Implement A Graphenedjango Pagination Feature For ...
An example GraphQL pagination API for Relay using Graphene and Django + a Next.js React UI GitHub saltycrane/graphenerelaypaginationexample: An. from django.
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