bianjunhui 1 день назад
Родитель
Сommit
7e40669e45

+ 1 - 0
config/api-platform.php

@@ -56,6 +56,7 @@ return [
         base_path('packages/Webkul/BagistoApi/src/Dto/ProductDetail/'),
         base_path('packages/Webkul/BagistoApi/src/Dto/CustomerOrder/'),
         base_path('packages/Webkul/BagistoApi/src/Dto/ProductSearch/'),
+        base_path('packages/Webkul/BagistoApi/src/Dto/CategoryProducts/'),
     ],
 
     'formats' => [

+ 24 - 0
packages/Webkul/BagistoApi/src/Dto/CategoryProducts/CategoryProductsPageInfoDto.php

@@ -0,0 +1,24 @@
+<?php
+
+namespace Webkul\BagistoApi\Dto\CategoryProducts;
+
+use ApiPlatform\Metadata\ApiResource;
+
+#[ApiResource(
+    operations: [],
+    graphQlOperations: [],
+    paginationEnabled: false,
+)]
+class CategoryProductsPageInfoDto
+{
+    /**
+     * Cursor of the last product in the current page (used as the `after` arg
+     * for the next page).
+     */
+    public ?string $end_cursor = null;
+
+    /**
+     * Whether another page exists after the current one.
+     */
+    public bool $has_next_page = false;
+}

+ 13 - 0
packages/Webkul/BagistoApi/src/Models/CategoryProducts.php

@@ -5,6 +5,7 @@ namespace Webkul\BagistoApi\Models;
 use ApiPlatform\Metadata\ApiProperty;
 use ApiPlatform\Metadata\ApiResource;
 use ApiPlatform\Metadata\GraphQl\Query;
+use Webkul\BagistoApi\Dto\CategoryProducts\CategoryProductsPageInfoDto;
 use Webkul\BagistoApi\Dto\ProductSearch\ProductSearchEdgeDto;
 use Webkul\BagistoApi\Resolver\CategoryProductsResolver;
 
@@ -51,4 +52,16 @@ class CategoryProducts
      */
     #[ApiProperty(readableLink: true)]
     public array $products = [];
+
+    /**
+     * Pagination metadata (endCursor / hasNextPage) so clients can stop paging
+     * before issuing an empty trailing request.
+     */
+    #[ApiProperty(readableLink: true)]
+    public CategoryProductsPageInfoDto $page_info;
+
+    public function __construct()
+    {
+        $this->page_info = new CategoryProductsPageInfoDto;
+    }
 }

+ 7 - 0
packages/Webkul/BagistoApi/src/Resolver/CategoryProductsResolver.php

@@ -63,6 +63,7 @@ class CategoryProductsResolver implements QueryItemResolverInterface
         $result->total_count = (int) $paginator->getTotalItems();
 
         $offset = $this->resolveOffset($args, $result->total_count);
+        $limit = max(1, (int) ($args['first'] ?? $args['last'] ?? 30));
 
         foreach ($paginator as $index => $product) {
             $edge = new ProductSearchEdgeDto;
@@ -71,6 +72,12 @@ class CategoryProductsResolver implements QueryItemResolverInterface
             $result->products[] = $edge;
         }
 
+        // Pagination metadata: lets the client know whether another page exists
+        // without issuing an extra (possibly empty) request.
+        $count = count($result->products);
+        $result->page_info->has_next_page = ($offset + $count) < $result->total_count;
+        $result->page_info->end_cursor = $count > 0 ? $result->products[$count - 1]->cursor : null;
+
         return $result;
     }