Search code examples
paginationgraphqlrelayjs

Why is there no 'position' argument in Relay+GraphQL connections?


GraphQL and Relay has a robust pagination algorithm which enables easy pagination for the end user, allowing pagination even in unbounded and order-independent results.

However, I have a use case that I'm not really sure how to go about doing in GraphQL and relay, and it's quite easy that I'm sure I just missed something.

How do I, for example, get the 5th item (and only the 5th item), if my list is ordered (by, say, an orderBy argument)?


Solution

  • This not very well documented, but here's how to do it.

    query {
      allPeople(first: 5, last: 1) {
        edges {
          node {
            name
          }
        }
      }
    }
    

    First you select first: 5 to get the first 5 people in the list. Then, do last:1 which gets the last person from that subset. In other words - get the fifth person.

    If you do (first: 5, last: 2) you would get the 4th and the 5th person in the list.

    Demo (if it returns an error - manually re-type the word query in the query and it will work). Then, try again without first and last to see the whole list and you'll see that Leia is 5th.