Paginating indexes with after

Hi,

I’m using results of a sorted index to render a search results table. My index has 11 values + the ref as the last item, so it returns each item as an array of 12.

I’ve struggled figuring out how to paginate the index with after but this question on SO indicated I should use the values as well with the ref. I managed it to work but my gut telling me I’m doing something hacky/wrong:

Using Javascript driver with FQL at both client and server:

  1. Client (browser) receives results with after being an array(12)
  2. User clicks Load More
  3. Client does JSON.stringify(after) and passes it to API on server side
  4. Server parses received param using JSON.parse and got an array
  5. Server uses the same array but drops last element, and reconstruct it like: [...array(11), q.Ref(q.Collection("coll"), ref["@ref"].id)]
  6. Pass the obtained value as after

Now, as I mentioned this works, but my questions:

  1. Is JSON stringify/parse dance safe? Of course, I can pass those 11 values separately alongside with a ref but It would make the design brittle as it would unnecessarily couple the cursor to the values of index.
  2. I have already the ref as an object on the last element of the array. Can use it directly instead of extracting the id from it and basically reconstructing the same thing?

Hopefully I’m missing something quite obvious and I could simplify some bits of the flow.

Thanks

You are close to what I do as well. If you use parseJSON from the faunadb js driver you will have a solution that works for any index without having to reconstruct fauna types manually. I also base64 encode/decode it. See this example: https://github.com/shiftx/faunadb-graphql-lib/blob/master/src/types/GraphQLFaunaCursorType.ts

2 Likes