在 mongodb + graphql api 上工作,似乎无论我如何配置分页,它总是为每个边返回相同的光标。最重要的是,我尝试改变分页的方向,但它没有反映在结果中。
这是全局配置:
api_platform:
show_webby: false
enable_swagger: false
enable_swagger_ui: false
enable_re_doc: false
enable_entrypoint: false
enable_docs: false
enable_profiler: false
graphql:
graphql_playground:
enabled: false
collection:
pagination:
enabled: false
资源:
<?php
declare(strict_types=1);
namespace App\Document;
use ApiPlatform\Doctrine\Odm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Odm\Filter\RangeFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GraphQl\QueryCollection;
use App\Repository\StatusRepository;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use Doctrine\ODM\MongoDB\Types\Type;
#[ODM\Document(repositoryClass: StatusRepository::class)]
#[ODM\UniqueIndex(keys: ['vessel.id' => 'asc', 'time' => 'asc'])]
#[ApiResource(
operations: [],
paginationViaCursor: [
['field' => 'numericFieldForCursorBasedPagination', 'direction' => 'DESC']
],
paginationEnabled: true,
paginationPartial: true,
paginationType: 'cursor',
graphQlOperations: [
new QueryCollection(
security: 'is_granted("ROLE_CURRENT_STATUS_READ")',
name: 'current',
),
],
)]
#[ApiFilter(RangeFilter::class, properties: ['numericFieldForCursorBasedPagination'])]
#[ApiFilter(OrderFilter::class, properties: ['numericFieldForCursorBasedPagination' => 'DESC'])]
class Status
{
public function __construct(
#[ODM\Id(type: Type::STRING, strategy: 'NONE')]
private readonly string $id,
#[ODM\Field(type: Type::INT)]
private readonly int $numericFieldForCursorBasedPagination,
) {
}
public function getId(): string
{
return $this->id;
}
public function getNumericFieldForCursorBasedPagination(): int
{
return $this->numericFieldForCursorBasedPagination;
}
}
结果:
{
"data": {
"currentStatuses": {
"totalCount": 10,
"pageInfo": {
"startCursor": "MA==",
"endCursor": "NA==",
"hasPreviousPage": false,
"hasNextPage": true
},
"edges": [
{
"cursor": "MA==",
"node": {
"id": "\/api\/statuses\/018d78c4-2042-7ae0-9134-f394ab2d9f48",
"_id": "018d78c4-2042-7ae0-9134-f394ab2d9f48",
"numericFieldForCursorBasedPagination": 5,
}
},
{
"cursor": "MQ==",
"node": {
"id": "\/api\/statuses\/018d78c4-20cd-7475-bc9c-1b24571c7a7d",
"_id": "018d78c4-20cd-7475-bc9c-1b24571c7a7d",
"numericFieldForCursorBasedPagination": 4,
}
},
{
"cursor": "Mg==",
"node": {
"id": "\/api\/statuses\/018d78c4-20d1-7183-b73a-e89a67bf30cb",
"_id": "018d78c4-20d1-7183-b73a-e89a67bf30cb",
"numericFieldForCursorBasedPagination": 12,
}
},
{
"cursor": "Mw==",
"node": {
"id": "\/api\/statuses\/018d78c4-20d4-7619-8681-47e221006801",
"_id": "018d78c4-20d4-7619-8681-47e221006801",
"numericFieldForCursorBasedPagination": 19,
}
},
{
"cursor": "NA==",
"node": {
"id": "\/api\/statuses\/018d78c4-20d7-7e7f-a661-95abdba696ff",
"_id": "018d78c4-20d7-7e7f-a661-95abdba696ff",
"numericFieldForCursorBasedPagination": 11,
}
}
]
}
}
}
如您所见,未考虑分页方向。无论链接到什么数据,每条边的光标始终相同(MA==、MQ==、Mg==、...)。我认为光标将基于
paginationViaCursor
中设置的字段,还是我错了?
实际上,api平台graphql还没有实现基于游标的分页。这些选项存在,但已在此 PR 中删除 https://github.com/api-platform/core/pull/5333