Pagination using "after" seems to ignore sorting

Hi, I’m running the following query

{
  me(func: uid(0x54812)) {
    uid
    name@en
    director.film (orderdesc: name@en) (first:6)  {
      uid
      name@en
    }
  }
}

then, using the uid of the last item returned, running this query

{
  me(func: uid(0x54812)) {
    uid
    name@en
    director.film (orderdesc: name@en) (first:6, after: 0x80d60)  {
      uid
      name@en
    }
  }
}

I would expect this 2nd query to return identical results to this query:

{
  me(func: uid(0x54812)) {
    uid
    name@en
    director.film (orderdesc: name@en) (first:6, offset: 6)  {
      uid
      name@en
    }
  }
}

however, it returns a mix of results including a few from the first query.

As stated in the title, it seems to me that the “after” operation is being performed before the sorting occurs, which is not what I would expect. To further support this, I notice that removing the “orderdesc” from the query will produce results as expected, and both the “offset” and “after” versions of the query will return identical results.

Is this a bug or intended behavior? If not a bug, can anyone help me get this working?

I’ve tried several variations of this, with the sorting/pagination at the root query, using variables, and a few others, but all seem to have the same behavior and I haven’t been able to get it working how I would like.

To anyone looking for a workaround for this, here is what I did:

{
  var (func: uid(0x80d60)) {
    aftername as name@en
  }
 
  me(func: uid(0x54812)) {
    uid
    name@en
    director.film (orderdesc: name@en) (first:6) @filter(lt(name@en, val(aftername)))  {
      uid
      name@en
    }
  }
}
1 Like

Sorry for the extremely late reply. This post went unnoticed, escaped from my vision.

That’s not a bug. It is related to the fact that UIDs are unsorted in Dgraph. And “after” param just works for UIDs not values like “name@en”.

Note Without a sort order specified, the results are sorted by uid , which is assigned randomly. So the ordering, while deterministic, might not be what you expected.

https://dgraph.io/docs/query-language/#pagination

Perhaps your workaround is the best way out for now. Or maybe open an issue requesting such support (use After based on sorting “on the fly”).

Cheers.