DEV Community

Daniel da Rocha
Daniel da Rocha

Posted on

2 2

GraphQL pagination

I've been playing a lot with GraphQL and Apollo lately, but I am usually unsure about best practices when it comes to querying the server and getting results back.

Now, say I have a list of locations which I want to display in a paginated list:

Let's say I decide to use Apollo's pagination capabilities and get 10 locations at a time:

  • If I want to filter by city using a dropdown with all cities in the DB, the paginated results will return me only the cities within those 10 locations. Should I then make a separate query for that (eg. allCities {...} )?
  • Then if I select a city as a filter, I should make yet another new query filtered by the city, right?

Or should I just ditch Apollo's pagination and query all locations, and do all the pagination and filtering in the client? In that case, how much is too much data for an initial load?

Or am I missing something?

Please help!

Thanks!!
Greetings from Berlin...

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil β€” patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (3)

Collapse
 
gugadev profile image
gugadev β€’

I think the best and performant way to do this is through cursors. Making pagination at client side doesn't have so much sense due the nature of GraphQL of get only the data that you need. There is no the best overall solution when we talk about this topic, you should pick the best way that works for you.

Collapse
 
danroc profile image
Daniel da Rocha β€’

It seems like an interesting paradox: while you only get the data you need, you end up calling the server more times with specific requests?

Collapse
 
gugadev profile image
gugadev β€’

Of course, yes. However, it doesn't matter. Always bet on small chunks of data when you need it instead one big request. It's a good practice for web performance and UX. You could make a single request for thousand of records and process it inside a worker but the negative impact on slows networks still there.

World's Largest Hackathon: AI Translation with Lingo

The Lingo team joins us to explore how developers can translate their Bolt projects using AI. We'll discuss the future of building global apps, why more teams are turning to AI for localization, and what it means for your workflow. Don’t miss it!

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❀️