type Query {
posts: [Post!]! @paginate(resolver: "App\\GraphQL\\Queries\\Posts")
}
自订解析程式函式可能如下所示:
namespace App\GraphQL\Queries;
use Illuminate\Pagination\LengthAwarePaginator;
final class Posts
{
/**
* @param null $root Always null, since this field has no parent.
* @param array{} $args The field arguments passed by the client.
* @param \Nuwave\Lighthouse\Support\Contracts\GraphQLContext $context Shared between all fields.
* @param \GraphQL\Type\Definition\ResolveInfo $resolveInfo Metadata for advanced query resolution.
*/
public function __invoke($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): LengthAwarePaginator
{
//...apply your logic
return new LengthAwarePaginator([
[
'id' => 1,
'title' => 'Flying teacup found in solar orbit',
],
[
'id' => 2,
'title' => 'What actually is the difference between cookies and biscuits?',
],
], 2, 15);
}
}
2条答案
按热度按时间8hhllhi21#
幸运的是,最近添加了这样一个功能:https://github.com/nuwave/lighthouse/pull/2232。此PR添加了对从
@paginator
指令中的选项resolver
返回Paginator
中的数据的支持。您可以提供自己的函数,通过直接返回
\Illuminate\Contracts\Pagination\Paginator
示例中的数据来解析字段。这与
builder
和model
互斥。与scopes
和生成器参数(如@eq
)不兼容。自订解析程式函式可能如下所示:
(The文档当前没有得到正确更新,这就是为什么你可能没有发现这一点。我正在恢复部署。)
mec1mxoz2#
是的https://lighthouse-php.com/5/api-reference/directives.html#custom-builder:)
第一个