duffelhq / paginator

Cursor-based pagination for Elixir Ecto

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggestion: Change metadata

bruno-b-martins opened this issue Β· comments

TL;DR

Make after and before always present and add has_next_page and has_previous_page booleans to metada.


Hello there πŸ‘‹

I've recently started using this library to cursor paginate a list in a small Phoenix + Vue project and bumped into an issue that required a lot of work-arounds when using this in an infinite scroll scenario.

Problem

Picture this:
We want to paginate through 5 records ([1, 2, 3, 4, 5]) 3 at a time (limit: 3).
So, the first query returns something like this:

entries: [1, 2, 3], metadata: { after: 3, before: nil, ... }

On the client-side application I push the 3 entries to an items array and store the after cursor in a pagination object.
The second query then will ask for 3 records after the cursor 3 and paginatior returns the following:

entries: [4, 5], metadata: { after: nil, before: 4, ... }

On the client-side application I push the 2 entries to the items array and now the after cursor is null so if I store it and use it, the next query will return the first 3 entries again. I can't use the before cursor as well because the next query would then return me the 5th item again.

I built a bit of a complex work-around on the client-side where I fetch the before the last and the last items just to get the cursor of the last item.

Suggestion

Make the after and before cursors be always present so that the consumers of the API always have the beginning and end of the page.
Add two different metadata properties to show that there is a next page and a previous page.
I suggest has_next_page and has_previous_page mimicking graphQL's relay style pagination - https://relay.dev/graphql/connections.htm#sec-undefined.PageInfo

What do you think?

Add two different metadata properties to show that there is a next page and a previous page.
I suggest has_next_page and has_previous_page mimicking graphQL's relay style pagination -

Came here to ask for the same functionality. This would simplify graphql-consuming code on the client.