How to query nodes by most recent with pagination?

Suppose I have this set of nodes:

{ uid: '0x1' }
{ uid: '0x2' }
{ uid: '0x3' }
{ uid: '0x4' }
{ uid: '0x5' }
{ uid: '0x6' }
{ uid: '0x7' }
{ uid: '0x8' }

Is it possible to query two at a time in descending uid order, always picking up where I left off?

query 1 results: [{ uid: '0x8' }, { uid: '0x7' }]
query 2 results: [{ uid: '0x6' }, { uid: '0x5' }]
query 3 results: [{ uid: '0x4' }, { uid: '0x3' }]
...
new node is inserted: { uid: '0x9' }
...
query 4 results: [{ uid: '0x2' }, { uid: '0x1' }]

I tried using first with a negative number, but I can’t figure out how to use offset properly when first is negative.

I also tried using after, but I can’t figure out how to make it work in reverse (i.e. in my case 0x7 is “after” 0x8).

1 Like

Can you post an example of your query?

This query…

{
  posts(func: type(Post), offset: 0, first: -2) {
    uid
  }
}

…gives me this result:

"posts": [
  {
    "uid": "0x7"
  },
  {
    "uid": "0x8"
  }
]

It’s not in the order I want, but fine, it gave me the two newest posts and I can reverse the array in JavaScript. The question is: How do I get the next two posts, with uid 0x5 and 0x6? Advancing the offset by 2…

{
  posts(func: type(Post), offset: 2, first: -2) {
    uid
  }
}

…gives me the exact same result:

"posts": [
  {
    "uid": "0x7"
  },
  {
    "uid": "0x8"
  }
]