Return paginated data from rebing laravel graphql server

Viewed 251

I am using the rebing graphql laravel library and it works very fine returning json data. Currently I am trying to build a paginated table via reactjs frontend so I need to get a paginated data just the way it works normally with blade. I have used the paginate method in place of the get method but it somehow still gives same data format. This is what I have in my resolve method currently:

$models = YearModel
        ::with($with)
        ->select($select)
        ->where($where)
        ->orderByDesc('year_id');
    if (isset($args['limit'])) {
        $models = $models->paginate($args['limit']);
    } else {
        $models = $models->get();
    }
    return $models;

paginate and get returns same data to the frontend whereas I was expecting paginate to give some data which would include stuffs like currentPage, nextPage, previousPage as the LengthAwarePaginator class would normally do. So I believe the graphql library is somehow formating the paginated data as a normal data before returning it to the frontend. I am not sure why this is like this as I am new to graphql. Any idea or solution will be appreciated please. Thanks

1 Answers

I was able to get a paginated data response by changing my query return type from Type::nonNull(Type::listOf(Type::nonNull(GraphQL::type('YearModel')))) to GraphQL::paginate('YearModel') and modified my query this way:

query {
        yearModels (limit: 10) {
            data {
                id
                year
            }
            total
            per_page
            current_page
            from
            to
            last_page
            has_more_pages
        }
    }`

data, total, per_page, current_page, from, to, last_page, has_more_pages are fields coming from Rebing\GraphQL\Support\PaginationType getPaginationFields method.

Related