Skip to content

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

Fix pagination with 'last' arg

Krzysztof Świątekcc81f20d812b
diff --git a/saleor/graphql/core/connection.py b/saleor/graphql/core/connection.py
index 1265a6bd5d..bb5685adb8 100644
--- a/saleor/graphql/core/connection.py
+++ b/saleor/graphql/core/connection.py
@@ -158,7 +158,7 @@ def _get_edges_for_connection(edge_type, qs, args, sorting_fields):
cursor = after or before
requested_count = first or last
- if before:
+ if last:
start_slice, end_slice = 1, None
else:
start_slice, end_slice = 0, requested_count
@@ -216,6 +216,8 @@ def connection_from_queryset_slice(
)
qs = qs.filter(filter_kwargs)
qs = qs[:end_margin]
+ print(end_margin)
+ print(qs)
edges, page_info = _get_edges_for_connection(edge_type, qs, args, sorting_fields)
return connection_type(edges=edges, page_info=page_info_type(**page_info),)
diff --git a/saleor/graphql/utils/sorting.py b/saleor/graphql/utils/sorting.py
index d41d4985b0..412221c2c1 100644
--- a/saleor/graphql/utils/sorting.py
+++ b/saleor/graphql/utils/sorting.py
@@ -23,7 +23,7 @@ def _sort_queryset_by_attribute(queryset, sorting_attribute, sorting_direction):
def sort_queryset_for_connection(iterable, args):
sort_by = args.get("sort_by")
- reversed = True if "before" in args else False
+ reversed = True if "last" in args else False
if sort_by:
iterable = sort_queryset(queryset=iterable, sort_by=sort_by, reversed=reversed)
else:
@@ -106,9 +106,7 @@ def sort_queryset_by_default(
direction = "-" if "-" in default_ordering[0] else ""
if reversed:
reversed_direction = REVERSED_DIRECTION[direction]
- default_ordering = [
- f"{reversed_direction}{field}" for field in ordering_fields
- ]
+ default_ordering = [f"{reversed_direction}{field}" for field in ordering_fields]
order_by = {"field": ordering_fields, "direction": direction}
return queryset.order_by(*default_ordering), order_by