Skip to content

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

Allow querying totalCount without pagination

Marcin Gębala86f7c46ac8c6
diff --git a/saleor/graphql/core/fields.py b/saleor/graphql/core/fields.py
index ba516ba1e1..dcdf6642e7 100644
--- a/saleor/graphql/core/fields.py
+++ b/saleor/graphql/core/fields.py
@@ -28,6 +28,21 @@ def convert_field_measurements(field, registry=None):
class PrefetchingConnectionField(DjangoConnectionField):
+ @classmethod
+ def connection_resolver(
+ cls, resolver, connection, default_manager, max_limit,
+ enforce_first_or_last, root, info, **args):
+
+ # Disable `enforce_first_or_last` if not querying for `edges`.
+ values = [
+ field.name.value
+ for field in info.field_asts[0].selection_set.selections]
+ if 'edges' not in values:
+ enforce_first_or_last = False
+
+ return super().connection_resolver(
+ resolver, connection, default_manager, max_limit,
+ enforce_first_or_last, root, info, **args)
@classmethod
def resolve_connection(cls, connection, default_manager, args, iterable):
diff --git a/tests/api/test_core.py b/tests/api/test_core.py
index d496ba4412..54b7023c8e 100644
--- a/tests/api/test_core.py
+++ b/tests/api/test_core.py
@@ -2,7 +2,8 @@ from unittest.mock import Mock
import graphene
from django.utils import timezone
-from tests.api.utils import get_graphql_content
+from tests.api.utils import (
+ _get_graphql_content_from_response, get_graphql_content)
from saleor.graphql.core.types import ReportingPeriod
from saleor.graphql.core.utils import clean_seo_fields, snake_to_camel_case
@@ -104,3 +105,36 @@ def test_reporting_period_to_date():
assert start_date.minute == 0
assert start_date.second == 0
assert start_date.microsecond == 0
+
+
+def test_require_pagination(api_client):
+ query = """
+ query {
+ products {
+ edges {
+ node {
+ name
+ }
+ }
+ }
+ }
+ """
+ response = api_client.post_graphql(query)
+ content = _get_graphql_content_from_response(response)
+ assert 'errors' in content
+ assert content['errors'][0]['message'] == (
+ 'You must provide a `first` or `last` value to properly paginate the '
+ '`products` connection.')
+
+
+def test_total_count_query(api_client, product):
+ query = """
+ query {
+ products {
+ totalCount
+ }
+ }
+ """
+ response = api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ assert content['data']['products']['totalCount'] == 1