DEV Community

Discussion on: GraphQL Pagination

chihab profile image
Chihab Otmani • Edited on

Regarding timestamps, that would be a good solution indeed:

  • if the application does not have a lot of traffic that might introduce timestamp collisions (which is the case of my crappy apps ;))
  • if you already have a timestamp in your database table/collection or have the ability to add one (think about read-only legacy databases)
  • to avoid collisions on timestamps and be able to query next elements without duplicates, timestamps would need to be created in sequence with a random suffix based on time, not sure that this is always provided OOB by db engines.

Actually, if supported, row index is pretty straightforward, you simply add it to your select and you get an index of each row in the result, that would be the cursor value I guess.
Example here for postgresql:
Do you see any issue on using ROW_NUMBER()?

PS: Just rambling on here, I'll probably use timestamps on my apps, as you've suggested ;)

Thread Thread
jefferyhus profile image
El Housseine Jaafari Author

The only issue I see with using ROW_NUMBER is that you will have to always select all results from a table, assign a row number then only pull the results you want. This is exactly how limit & offset works. So it isn't going to be of any help if your database is growing fast.

As I said if you include microseconds in your timestamp value, there mostly will be no collision.

Thread Thread
chihab profile image
Chihab Otmani • Edited on

You're right! You'd just have to have these micro-timestamps in your database and have end-users tolerant to potential (even rare) collisions otherwise I guess you'd have to deal with less performant fetching strategies.