Skip to content

Instantly share code, notes, and snippets.

@a-type
Last active July 23, 2019 14:52
Show Gist options
  • Save a-type/f1dda6de337c379ce02f8e0eb44012f5 to your computer and use it in GitHub Desktop.
Save a-type/f1dda6de337c379ce02f8e0eb44012f5 to your computer and use it in GitHub Desktop.
ArangoDB Relay Cursor Query

Relay Cursor Connection Spec

This is a bit of a hack, because it's hard to calculate hasNextPage in pure AQL. Instead, I'm adding 1 to the number of results, then slicing the result set back down before returning it. If the result set is equal to first + 1, we have a next page.

This query can be run on the example "Social" graph. Try adjusting the first value to 1 to see pagination. Set after to the cursor of the previous result to go to the next page.

LET cursorField = "_id"
LET first = 1
LET after = null
LET listPlusOne = (
FOR friend_0, friend_edge IN ANY 'male/bob' relation
OPTIONS {bfs: true}
SORT friend_0[cursorField]
FILTER after == null || friend_0[cursorField] > after
LIMIT first + 1
LET friend_relay_cursor = friend_0[cursorField]
LET friend_relay_edge = MERGE(friend_edge, { cursor: friend_relay_cursor }, { node: friend_0 })
RETURN friend_relay_edge
)
LET hasNextPage = LENGTH(listPlusOne) == first + 1
LET list = SLICE(listPlusOne, 0, first)
RETURN {
edges: list,
pageInfo: {
hasNextPage: hasNextPage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment