Skip to content

Conversation

OwlyCode
Copy link
Contributor

This adds a standalone object to help on pagination. Its role is to convert GraphQL arguments from the ConnectionArgs to an offset/limit pair. For instance :

    public function resolveList($args)
    {
        $pagination = new EntityPaginator(function ($offset, $limit) {
            return $this->myRepository->getAll($offset, $limit); // Or Doctrine or whatever
        });
        
        // Make use of the 'first' and 'after' arguments
        return $pagination->forward($args);
    }

All you have to do is provide a callable that fetch the items you need to paginate.

For backward pagination, you have to provide an item count :

return $pagination->backward($args, $this->myRepository->count());

You can also have an automatic mode that detects if you go backward or forward :

return $pagination->auto($args, $this->myRepository->count());

To avoid computing a total that is uneeded for forward pagination, you can provide a callable that will only be resolved when going backward :

return $pagination->auto($args, [$this->myRepository, 'count']);

Copy link
Contributor

@mcg-web mcg-web left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for the contribution @OwlyCode :)

@mcg-web mcg-web merged commit 0d27381 into overblog:master Nov 22, 2016
@Nyalab
Copy link

Nyalab commented Nov 23, 2016

👏

@ooflorent
Copy link
Contributor

Damn! This is good.

*
* @return Argument
*/
private function protectArgs($args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toArgument would be a better name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private method we can renames it easily before next release without BC :trollface:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants