Serializing Cursors

Hello,

I have an HTTP endpoint which returns the result of a paginate call.
I would like to somehow serialize the next cursor to a format that I can pass as a query string parameter in the URL of the next request.

My next cursor looks something like:

[
  1597205896295000,
  Ref(Collection("posts"), "273622809807159820"),
  Ref(Collection("posts"), "273622809807159820")
]

I have tried to base64 encode it like this:

new Buffer(JSON.stringify(after)).toString('base64')

and decoding with:

JSON.parse(new Buffer(after, 'base64').toString('utf8'))

However, when I give the decoded array to my paginate call I get the following error:

Ref expected, Object provided.

Am I going about this in the best way?

Thanks

You are very close :slight_smile: You need to use parseJSON from the FaunaDB JS driver when decoding. See this: https://github.com/shiftx/faunadb-graphql-lib/blob/master/src/types/GraphQLFaunaCursorType.ts

2 Likes

Brilliant thank you! Works perfectly!

I do feel a little dirty importing from an internal module… Probably worth thinking about exporting it in the actual module :slightly_smiling_face:

Agree, I have seen it requested multiple times before

@eigilsagafos @alaister and others, if you did not see, parseJSON is now exported from the js driver :raised_hands: