Index

Graphene

  1. Queries
  2. migrations
  3. UserModel
  4. User Schema
  5. cors
  6. static
  7. headers
  8. payumoney
  9. imageupload
  10. Total count

Total count


Is totalCount not implemented on Relay? #162



https://github.com/graphql-python/graphene-django/issues/162


The solution worked for me.

Because this is scensial to paginate, I've done it as a Mixin to add it to multiple DjangoObjectType like this:

class TotalCountMixin(ObjectType):
    @classmethod
    def get_connection(cls):
        class CountableConnection(relay.Connection):
            total_count = Int()

            class Meta:
                name = '{}Connection'.format(cls._meta.name)
                node = cls

            @staticmethod
            def resolve_total_count(root, args, context, info):
                return root.length

        return CountableConnection

Then I added it to the DjangoObjectType, in your case it would be:

class ProductNode(DjangoObjectType, TotalCountMixin):
    class Meta:
        model = Product
        filter_fields = {
            'name': ['exact''icontains''istartswith'],
        }
        interfaces = (relay.Node, )

Test it!