Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add totalCount field in products connection

Marcin Gębalad96774a7df6a
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index fd2afb79f3..4eabd85cd9 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -8,7 +8,7 @@ from graphene_django.debug import DjangoDebug
from ..product.models import (AttributeChoiceValue, Category, Product,
ProductAttribute, ProductImage, ProductVariant)
from .scalars import AttributesFilterScalar
-from .utils import DjangoPkInterface, get_object_or_none
+from .utils import DjangoPkInterface, connection_with_count, get_object_or_none
class ProductType(DjangoObjectType):
@@ -45,6 +45,9 @@ class ProductType(DjangoObjectType):
return self.get_absolute_url()
+ProductType.Connection = connection_with_count(ProductType)
+
+
class CategoryType(DjangoObjectType):
url = graphene.String()
products = relay.ConnectionField(
diff --git a/saleor/graphql/utils.py b/saleor/graphql/utils.py
index 7f7d6751b7..88f18e72a8 100644
--- a/saleor/graphql/utils.py
+++ b/saleor/graphql/utils.py
@@ -1,4 +1,5 @@
import graphene
+
from django.shortcuts import _get_queryset
@@ -24,3 +25,17 @@ class DjangoPkInterface(graphene.Interface):
def resolve_pk(self, args, context, info):
return self.pk
+
+
+def connection_with_count(_type):
+ class Connection(graphene.relay.Connection):
+ total_count = graphene.Int()
+
+ class Meta:
+ name = _type._meta.name + 'Connection'
+ node = _type
+
+ def resolve_total_count(self, args, context, info):
+ return len(self.edges)
+
+ return Connection