|
@@ -52,6 +52,20 @@ class CategoryProductsResolver implements QueryItemResolverInterface
|
|
|
$productArgs['reverse'] = false;
|
|
$productArgs['reverse'] = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // The product provider only understands Relay cursor args (after/before),
|
|
|
|
|
+ // so translate a direct `page` arg into an equivalent `after` cursor.
|
|
|
|
|
+ if (! empty($productArgs['page'])) {
|
|
|
|
|
+ $page = max(1, (int) $productArgs['page']);
|
|
|
|
|
+ $limit = max(1, (int) ($productArgs['first'] ?? $productArgs['last'] ?? 30));
|
|
|
|
|
+ $offset = ($page - 1) * $limit;
|
|
|
|
|
+
|
|
|
|
|
+ if ($offset > 0) {
|
|
|
|
|
+ $productArgs['after'] = base64_encode((string) ($offset - 1));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ unset($productArgs['page']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
$paginator = $this->productProvider->provide(
|
|
$paginator = $this->productProvider->provide(
|
|
|
$context['operation'],
|
|
$context['operation'],
|
|
|
[],
|
|
[],
|
|
@@ -85,24 +99,30 @@ class CategoryProductsResolver implements QueryItemResolverInterface
|
|
|
$count = count($result->products);
|
|
$count = count($result->products);
|
|
|
$result->page_info->has_next_page = ($offset + $count) < $result->total_count;
|
|
$result->page_info->has_next_page = ($offset + $count) < $result->total_count;
|
|
|
$result->page_info->end_cursor = $count > 0 ? $result->products[$count - 1]->cursor : null;
|
|
$result->page_info->end_cursor = $count > 0 ? $result->products[$count - 1]->cursor : null;
|
|
|
|
|
+ $result->page_info->current_page = $result->total_count > 0 ? (int) floor($offset / $limit) + 1 : 1;
|
|
|
|
|
+ $result->page_info->total_pages = $result->total_count > 0 ? (int) ceil($result->total_count / $limit) : 1;
|
|
|
|
|
|
|
|
return $result;
|
|
return $result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * Resolve the current page offset from Relay cursor args.
|
|
|
|
|
|
|
+ * Resolve the current page offset.
|
|
|
|
|
+ *
|
|
|
|
|
+ * Supports both Relay cursor args (after/before) and a direct `page` arg.
|
|
|
|
|
+ * When `page` is provided it takes precedence over after/before.
|
|
|
*/
|
|
*/
|
|
|
private function resolveOffset(array $args, int $total): int
|
|
private function resolveOffset(array $args, int $total): int
|
|
|
{
|
|
{
|
|
|
$limit = max(1, (int) ($args['first'] ?? $args['last'] ?? 30));
|
|
$limit = max(1, (int) ($args['first'] ?? $args['last'] ?? 30));
|
|
|
$offset = 0;
|
|
$offset = 0;
|
|
|
|
|
|
|
|
- if (! empty($args['after'])) {
|
|
|
|
|
|
|
+ if (! empty($args['page'])) {
|
|
|
|
|
+ $page = max(1, (int) $args['page']);
|
|
|
|
|
+ $offset = ($page - 1) * $limit;
|
|
|
|
|
+ } elseif (! empty($args['after'])) {
|
|
|
$decoded = base64_decode($args['after'], true);
|
|
$decoded = base64_decode($args['after'], true);
|
|
|
$offset = ctype_digit((string) $decoded) ? ((int) $decoded + 1) : 0;
|
|
$offset = ctype_digit((string) $decoded) ? ((int) $decoded + 1) : 0;
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (! empty($args['before'])) {
|
|
|
|
|
|
|
+ } elseif (! empty($args['before'])) {
|
|
|
$decoded = base64_decode($args['before'], true);
|
|
$decoded = base64_decode($args['before'], true);
|
|
|
$cursor = ctype_digit((string) $decoded) ? (int) $decoded : 0;
|
|
$cursor = ctype_digit((string) $decoded) ? (int) $decoded : 0;
|
|
|
$offset = max(0, $cursor - $limit);
|
|
$offset = max(0, $cursor - $limit);
|