How to jump to specific page with absinthe relay pagination?

I have pagination on root object. As the data is huge, I want the users to have the ability to jump to any arbitrary page. For reference this is what my query looks like right now.

If you’re using a Relay Connection (which you should be), you can use the :after or :before pagination args:

https://hexdocs.pm/absinthe_relay/Absinthe.Relay.Connection.html

My first thought was this only. But relay connection takes global id (cursor) as argument, i don’t have that for random page. Only adjacent pages can be fetch by this.

CMIIW, but relay pagination was focusing on after/before of entities. It only provide a way to get some entity after/before from a reference you have getted from previous entity.

That being said, you could actually implement arbitrary pagination in relay connection (though it might not be the most effecient), you just skip your query (after) with page_size x page num, so if you want to go 4th page with page size 30, you would skip set after to 120

also, another hack, current Absinthe.Relay.Connection actually using offset pagination internally rather than cursor (keyset) pagination, so if you are able to get offset data (which, again, is page_size * page_num), you could use Absinthe.Relay.Connection.offset_to_cursor to transform offset to cursor, like

offset = page * first - 1

    after_cursor =
      case offset do
        -1 -> nil
        _ -> Absinthe.Relay.Connection.offset_to_cursor(offset)
      end

    new_p_args =
      p_args
      |> Map.delete(:page)
      |> Map.put(:after, after_cursor)

Warning though, this is implementation detail, it might break on newer release on Absinthe.Relay.Connection

1 Like