Front-end engineer asks for help with pagination for Gatsby and createResolvers

15 replies
Last updated: Aug 30, 2021
user Y
! I was just wondering if I could bug you for a moment? I’m a front-end engineer working with Gatsby and learning about createResolvers, I read your article on creating taxonomy pages and was wondering if you could give me a nudge in the right direction-- I’ve used your exact implementation for our client’s category pages and now I’m working out how to do pagination for 12 posts on each page. Given the posts are generated with createResolvers it changes things a bit, so I would need to implement pagination in createResolvers as well right? As opposed to the normal way in the gatsby-node file and adding the limit and skip fields to the page query?
Aug 28, 2021, 10:16 AM
I read this too and started down this road, HOWEVER, there are a few ways to accomplish this based on your needs... and auto-generating a page for each category may not be it. Options...
1. Generate tag pages automatically with a list of related content (blog post above).
2. Create a collection page type, admins pick posts/etc to feature and control other unique page settings.
3. Create a query component that admins can add to an array of page section objects.
I’m still trying to decide, will probably use at least two of these :)
Aug 28, 2021, 10:36 PM
user Q
thanks for the input! For context: the mockup/spec requires a blog system with post types that have categories (guide, review, etc), where you can click on the category tags in the post and it will take you to a page that lists all posts that have that tag, on that page is a section up top where the admin picks featured posts of that category, and below that section is the list of all posts of that category which is paginated with 12 posts on each page. Creating the post resolver in the category query achieves the creation of the page itself, I’ve included an array of posts in the category schema to achieve the featured posts section of the page, and my line of thinking is that implementing the pagination like described here should still be achievable if I tweak the resolver correctly with the proper limit and skip fields and that’s where I’m stuck
Aug 28, 2021, 11:01 PM
Gotcha. Totally reasonable. Eventually folks want to customize their category pages, but a blog is a blog.
Aug 30, 2021, 1:03 AM
Can you share a gist with the code you have? PS: I’m implementing almost the same thing and will need pagination too 🙂
Aug 30, 2021, 1:05 AM
user Q
Yeah man! I’ve not implemented my solution yet but I’m at work now and will be doing it today. We’ve decided to implement pagination client-side rather than in the query/gatsby-node, since in our case there’s not a ton of data expected. For scalability it’d be nice to do it “properly” but it’s such an awkward implementation here.
Aug 30, 2021, 6:30 PM
I suppose you can always add body fields, etc to the category/tag doc schema and make those pages more interesting.
Aug 30, 2021, 8:01 PM
I should have asked… are your tags docs? (rather than select list options)
Aug 30, 2021, 8:02 PM
They’re docs called “category’ that the user can add references to in each blog post, then a page is automatically generated for each category and I used the above article to add the posts resolver to each category. In the category schema I’ve got a title, seo description, seo keywords, slug, and featured posts which is an array of post references. So at build time, pages for each post/category are generated, and posts are queried in each associated category. The category titles are listed in the post template as little ’pill” tags you can click on to go to the category page and see the associated posts (which I’ll paginate with useState or add a “show more” button with a function that grabs 12 more items each time it’s pressed)
Aug 30, 2021, 8:08 PM
Aug 30, 2021, 8:09 PM
Imma do exactly this… except the
is on the post.
Aug 30, 2021, 8:10 PM
Thanks! Yeah this was a lot of work
Aug 30, 2021, 8:10 PM
That’s a great idea! Didn’t think of that, could add it as a boolean
Aug 30, 2021, 8:11 PM
Aug 30, 2021, 8:11 PM
The problem is you have to query featured and all posts in parallel, so you have to do some pruning after the query is done… which could be a problem for pagination.
Aug 30, 2021, 8:12 PM
I haven’t solved this yet.
Aug 30, 2021, 8:12 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?