【问题标题】:Pagination with custom field resolver directive使用自定义字段解析器指令进行分页
【发布时间】:2020-06-24 13:45:56
【问题描述】:

我想要达到的目标如下: 我想要一个搜索模型的查询,这个查询可能很复杂,因此需要一个单独的解析器或某个可以放置我的逻辑的地方。

出了什么问题: 这是我的查询

fighters(q: String): [FighterProfile] @field(resolver: "App\\Http\\Controllers\\FighterProfileController@search")

这是我的解析器

public function search($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) {
    $user = auth()->user();
    $q = isset($args['q']) ? $args['q'] : null;

    $query = FighterProfile::distinct();

    // admin and federation can see all fighters
    if ($user->type === 'member') {
        $fighterProfile = $user->fighterProfile;

        $query->where('club_id', $fighterProfile->club_id);
    }

    if ($q) {
        $query->where(function ($subQuery) use ($q) {
           $subQuery->where('first_name', 'LIKE', '%' . $q . '%')
           ->orWhere('last_name', 'LIKE', '%' . $q . '%');
        });
    }

    $query->where('type', 'fighter');
    return $query->paginate(25);
}

现在的输出只是一个包含 25 个项目的列表,没有分页信息。当我尝试添加 @paginate 指令时,我收到以下错误

Node [fighters] can only have one directive of type [Nuwave\\Lighthouse\\Support\\Contracts\\FieldResolver] but found []

如何添加我的自定义逻辑但仍然有分页?

【问题讨论】:

    标签: laravel-lighthouse


    【解决方案1】:

    您不能将@field@paginate 结合使用,但您可以将@paginate@builder 结合使用。

    如果您仍然需要自定义解析器,您可以制作自己的自定义指令,例如 https://lighthouse-php.slack.com/archives/CB28A070S/p1574102072286100?thread_ts=1574101207.285300

    【讨论】:

    • 成功了,谢谢!对于不需要急切数据的简单查询,您可以轻松使用@builder,但如果变得更复杂,则自定义指令可以完美运行
    • 嗨@enzo 你能加我吗?(er.sahilgupta1@gmail.com)
    • @SahilGupta 你可以加入 Lighthouse 的 Slack:join.slack.com/t/lighthouse-php/shared_invite/…(我在,你可以私信我)
    猜你喜欢
    • 2014-09-06
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多