laravel-lighthouse

Pagination with custom field resolver directive


What I am trying to achieve is the following: I want a query that searches a model, this query can be complex and thus needs a separate resolver or some place where I can put my logic.

What goes wrong: This is my query

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

This is my resolver

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);
}

The output now is just a list of 25 items with no info on pagination. When I try to add the @paginate directive I get the following error

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

How can I add my custom logic but still have the pagination?


Solution

  • You can't combine @field with @paginate, but you can combine @paginate with @builder.

    If you still need a custom resolver, you can make your own custom directive, like https://lighthouse-php.slack.com/archives/CB28A070S/p1574102072286100?thread_ts=1574101207.285300