nathanl

nathanl

How to set up cursor-based pagination with Absinthe?

Does anybody have a good guide for setting up cursor-based pagination, as recommended in The GraphQL docs, using Absinthe?

Most Liked

nathanl

nathanl

I created a library (only on GitHub for now) to support keyset pagination with absinthe_relay. It’s basically a drop-in replacement in your resolver. An example of paginating a usersConnection:

# in your resolver function
AbsintheRelayKeysetConnection.from_query(
  ecto_users_query,
  &MyRepo.all/1,
  # these args would come from the API user
  %{
    sorts: [%{name: :desc}, %{id: :desc}],
    first: 10,
    after: "0QTwn5SRWyJNbyIsMjZd"
  },
  # you would hard-code this configuration
  %{unique_column: :id}
)

The reason a unique_column is specified here is to be a “tie breaker” to ensure a unique value for sorting and paginating. See the moduledocs for more details.

If using this, you might put (for example) under your usersConnection:

arg(:sorts, list_of(:user_order_by))

You’d use user_order_by to define the ways you allow sorting for that record type. (You might want to consider what index support you have or need.)

  input_object :user_order_by do
    @desc "By email"
    field(:email, :sort_direction)

    @desc "By first name"
    field(:first_name, :sort_direction)

   # etc
  end

:sort_direction is a simple enum type:

  enum :sort_direction do
    @desc "Ascending (for example, 1,2,3 or a,b,c)."
    value(:asc)
    @desc "Descending (for example, 3,2,1 or c,b,a)."
    value(:desc)
  end

@benwilson512 I’d love to contribute this to absinthe_relay | Hex if you’re interested. If not, I might release it as a separate package.

jeroenvisser101

jeroenvisser101

Hey Nathan! That library is great! I’ve some changes I plan to send as a PR (behaviour for Cursor to allow custom cursor implementations, making and marking the default Cursor as “tamper-resistant”, rather than tamper-proof, using Ecto’s dynamic/2 to allow > 3). Still working on some things, including adding tests for a NaiveDateTime sort, and I’m thinking of allowing the key for sorts to be configurable, although this could also be changed in the resolvers before calling from_query/4.

I’m also planning some tests on nullable columns, to see how it affects sorting (IIRC, == nil isn’t allowed in Ecto, and is_nil/1 should be used instead). Maybe this will be outside the scope though, given that asc and desc don’t cover the cases for all possible sorts:

  • :asc
  • :asc_nulls_last
  • :asc_nulls_first
  • :desc
  • :desc_nulls_last
  • :desc_nulls_first

And I’m not sure if it makes sense to allow all these cases.

nathanl

nathanl

FYI - this library is now published at absinthe_relay_keyset_connection | Hex

slouchpie

slouchpie

I don’t have a guide but I recently made a demo project that had cursor based pagination using Relay. Absinthe Relay image upload with Expo React Native

Where Next?

Popular in Questions Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement