Lighthouse: I can't paginate with field directive

Created on 24 Jul 2019  Â·  5Comments  Â·  Source: nuwave/lighthouse

Describe the bug
I need query an users with specific role and I created a resolver for this, but create a conflict between 2 directives: field and paginate directives

I tried too use PaginatorField, without success

Expected behavior/Solution
I hope received paginated data
Steps to reproduce
Try query

donators(count: Int!, page: Int!): [User!]! @paginate @field(resolver: "App\\GraphQL\\Queries\\GetDonators@resolve")

Output/Logs

Click to expand
"message": "Node [donators] can only have one directive of type [Nuwave\Lighthouse\Support\Contracts\FieldResolver] but found []",
"exception": "Nuwave\Lighthouse\Exceptions\DirectiveException",

Environment

Lighthouse Version: 3.7
Laravel Version: 5.8

question

Most helpful comment

That's expected behaviour, and i think the error message is pretty clear. Both @paginate and @field are resolver directives, but there can only ever be one resolver for a field.

Try the builder argument of @paginate, it should give you all the flexibility you need. https://lighthouse-php.com/master/api-reference/directives.html#paginate

All 5 comments

That's expected behaviour, and i think the error message is pretty clear. Both @paginate and @field are resolver directives, but there can only ever be one resolver for a field.

Try the builder argument of @paginate, it should give you all the flexibility you need. https://lighthouse-php.com/master/api-reference/directives.html#paginate

Ok, how I paginate my data now? I need recover a model with specific scope.

You can use scopes as well. It's all in the docs.

GREAT!!!!, Sorry and thank you very much.

Maybe this will help someone:

type Query {
    "Geting list of employers"
    findEmployers(
        searchQuery: String
    ): [User!]!
    @paginate(defaultCount: 10, builder: "App\\GraphQL\\Builders\\EmployerBuilder@findEmployers")
    @softDeletes
}
<?php

namespace App\GraphQL\Builders;

use App\User;
use Illuminate\Database\Eloquent\Builder;

class EmployerBuilder
{
    public function findEmployers($root, array $args): Builder
    {
        return User::query()
            ->when($args['searchQuery'], function ($query, $searchQuery) {
                $query
                    ->where('first_name', 'like', "%$searchQuery%")
                    ->orWhere('middle_name', 'like', "%$searchQuery%")
                    ->orWhere('last_name', 'like', "%$searchQuery%");
            });
    }
}

Works like a charm! ✨

Was this page helpful?
0 / 5 - 0 ratings