Index
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
@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, )
class Meta:
model = Product
filter_fields = {
'name': ['exact', 'icontains', 'istartswith'],
}
interfaces = (relay.Node, )
Test it!