Skip to content
This repository has been archived by the owner on Feb 10, 2019. It is now read-only.

How to paginate when using laravel-graphql? #380

Open
zwl1619 opened this issue Sep 10, 2018 · 3 comments
Open

How to paginate when using laravel-graphql? #380

zwl1619 opened this issue Sep 10, 2018 · 3 comments

Comments

@zwl1619
Copy link

zwl1619 commented Sep 10, 2018

How to paginate when using laravel-graphql?

@spawnia
Copy link

spawnia commented Sep 11, 2018

I recommend using https://github.com/nuwave/lighthouse instead. It supports pagination out of the box, without writing any code https://lighthouse-php.netlify.com/docs/next/directives.html#paginate

@devlamine
Copy link

with Folkloreatelier / laravel-graphql It is not possible ?

@ivanfilatov
Copy link

ivanfilatov commented Oct 1, 2018

You can do like this.
paginateData, prepareRelations and prepareQuery are my custom functions inside this query class (actually, i wrote abstract class for query and my custom query class looks like setting some classnames into query variables, i can share it also)
The point is that in resolve() you should return an object with LengthAwarePaginator implemented.

<?php
declare(strict_types=1);

use App\GraphQL\Helpers\ResolveContext;
use Folklore\GraphQL\Support\Query;
use Folklore\GraphQL\Support\Traits\ShouldValidate;
use GraphQL;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

abstract class SomeQuery extends Query
{
    use ShouldValidate;

    public function type(): ObjectType
    {
        return GraphQL::pagination(GraphQL::type('SomeType'));
    }

    public function resolve(array $root, ?array $data, ResolveContext $context, ResolveInfo $resolveInfo): LengthAwarePaginator
    {
        $this->prepareRelations($resolveInfo);
        if ($this->prepareQuery($data)) {
            return $this->paginateData($context);
        } else {
            return new \Illuminate\Pagination\LengthAwarePaginator([], 0, $this->perPage);
        }
    }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants