I have a database table reviews with id, rating where id is auto-incrementing and rating is an integer between 0 and 100.
I'm attempting to create cursor based pagination in a GraphQL API but am struggling to create the necessary queries for hasPreviousPage and hasNextPage.
Here is my data:
ID: 1, RATING: 50
ID: 2, RATING: 80
ID: 3, RATING: 20
ID: 4, RATING: 40
ID: 5, RATING: 60
Here's an example of a GQL query:
reviews(first: 3)
Which returns
ID: 1, RATING: 50
ID: 2, RATING: 80
ID: 3, RATING: 20
With pageInfo
hasPreviousPage: false
hasNextPage: true
The queries for pageInfo would be
hasPreviousPage = SELECT COUNT(*) > 0 FROM reviews WHERE id < 0;
hasNextPage = SELECT COUNT(*) > 0 FROM reviews WHERE id > 3;
My issue comes when sorting by rating. Making a query similar to before:
reviews(sort: "rating", first: 3)
Which returns
ID: 3, RATING: 20
ID: 4, RATING: 40
ID: 1, RATING: 50
With pageInfo
hasPreviousPage: false
hasNextPage: true
But how can I create the queries for hasPreviousPage and hasNextPage like I did before?
hasPreviousPage = SELECT COUNT(*) > 0 FROM reviews WHERE ???
hasNextPage = SELECT COUNT(*) > 0 FROM reviews WHERE ???
What should the WHERE clause be in this case? Does the query need to be a lot more complex with a sub query? I'm not sure what I'm missing.