wwwision/relay-pagination
Composer 安装命令:
composer require wwwision/relay-pagination
包简介
Simple pagination implementing the Cursor Connections Specification, see https://relay.dev/graphql/connections.htm
README 文档
README
Simple pagination implementing the Cursor Connections Specification, see https://relay.dev/graphql/connections.htm
Installation
Install via composer:
composer require wwwision/relay-pagination
Usage
$loader = # ... instance of \Wwwision\RelayPagination\Loader\Loader $resultsPerPage = 5; // edges to load per page $paginator = new Paginator($loader); $firstPage = $paginator->first($resultsPerPage); foreach ($firstPage as $edge) { // $edge->cursor; contains the cursor string // $edge->node; contains the payload } // $firstPage->pageInfo; contains an object with pagination information
Next page(s)
Every connection contains information about next and previous pages.
To navigate to a succeeding page, you can use the endCursor of the previous connection as input for the after argument:
if ($firstPage->pageInfo->hasNextPage) { $secondPage = $paginator->first($resultsPerPage, $firstPage->pageInfo->endCursor); // ... }
Backwards navigation
To navigate to a preceeding page, the startCursor can be passed to the last() method likewise:
if ($secondPage->pageInfo->hasPreviousPage) { $firstPage = $paginator->last($resultsPerPage, $secondPage->pageInfo->startCursor); // ... }
Reversed order
The ordering of edges is the same whether forward or backward navigation is used (as defined in the specification).
To navigate through results in reversed order, the reversed() method of the Paginator can be used:
$loader = new ArrayLoader(range('a', 'e')); $paginator = (new Paginator($loader))->reversed(); $page1 = $paginator->first(3); Assert::same(['e', 'd', 'c'], array_map(fn($edge) => $edge->node, $page1->toArray())); Assert::false($page1->pageInfo->hasPreviousPage); Assert::true($page1->pageInfo->hasNextPage); $page2 = $paginator->first(3, $page1->pageInfo->endCursor); Assert::same(['b', 'a'], array_map(fn($edge) => $edge->node, $page2->toArray())); Assert::true($page2->pageInfo->hasPreviousPage); Assert::false($page2->pageInfo->hasNextPage);
Loaders
This package comes with four adapters (aka "loaders"):
ArrayLoader
The ArrayLoader allows to paginate arbitrary arrays.
Note: This loader is mainly meant for testing and debugging purposes and it should not be used for large datasets because the whole array has to be loaded into memory, obviously.
Usage
$arrayLoader = new ArrayLoader($arbitraryArray);
Note: The specified array can be associative, but the keys will be lost during pagination since the ArrayLoader only works with the array values in order to guarantee deterministic ordering.
CallbackLoader
The CallbackLoader invokes closures to determine pagination results.
Note: This loader is mainly meant for rapid development, testing & debugging purposes. Usually you'd want to create a custom implementation of the Loader interface instead
Usage
$callbackLoader = new CallbackLoader( fn(int $limit, string $startCursor = null) => Edges::fromRawArray(...), fn(int $limit, string $endCursor = null) => Edges::fromRawArray(...) );
DbalLoader
The DbalLoader allows to paginate arbitrary DBAL results.
Note: This loader requires the doctrine/dbal package to be installed:
composer require doctrine/dbal
Usage
$queryBuilder = (new QueryBuilder($dbalConnection)) ->select('*') ->from('some_table'); $dbalLoader = new DbalLoader($queryBuilder, 'id');
OrmLoader
The OrmLoader allows to paginate arbitrary Doctrine ORM results.
Note: This loader requires the doctrine/orm package to be installed:
composer require doctrine/orm
Usage
$queryBuilder = $entityManager->createQueryBuilder() ->select('e') ->from(SomeEntity::class, 'e'); $ormLoader = new OrmLoader($queryBuilder, 'id');
Convert nodes
A node is the only untyped property in this package since the loaders define the structure & type of nodes.
For the DbalLoader a node is an associative array containing the raw result from the corresponding database row for example.
In order to make it easier to re-use loaders a Node Converter can be specified that is applied to all results before it is returned:
$paginator = (new Paginator($someLoader)) ->withNodeConverter(fn(string $node) => json_decode($node));
The above example expects the nodes to contain a valid JSON string. The same mechanism can be used in order to convert database results to a dedicated domain model instance:
$paginator = (new Paginator($dbalLoader)) ->withNodeConverter(fn(array $row) => MyModel::fromDatabaseRow($row));
Examples
wwwision/relay-pagination 适用场景与选型建议
wwwision/relay-pagination 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.66k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 06 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wwwision/relay-pagination 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wwwision/relay-pagination 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-06