Conditional filtering in Next.js function for fetching posts
3 replies
Last updated: Jun 26, 2024
L
I have a function in Next JS for fetching posts.
I'm conditionally passing in an "_id" depending on whether I want to fetch only posts that have a certain category. This works great for when it's passed in, but breaks when nothing is passed in.
It's specifically
Can I make this filtering conditional, and stop it from doing this filter when nothing is passed as an ID?
export async function getPosts(start: number = 0, end: number = 9, _id?: string) { const getPostsQuery = groq` // Get posts That are in the selected category And order them by published date *[_type == "post" && $categoryId in categories[]._ref] | order(publishedDate desc) { title, slug { current }, publishedDate, shortDescription, cardImage { ..., asset-> { ..., } } }[$start...$end] ` return client.fetch<GetPostsQueryResult>( getPostsQuery, { start, end, categoryId: _id }, { next: { tags: ['posts'], revalidate: 60, }, } ) }
It's specifically
$categoryId in categories[]._refthis part of the query that is fetching based on the passed ID.
Can I make this filtering conditional, and stop it from doing this filter when nothing is passed as an ID?
Jun 25, 2024, 2:03 PM
L
I've managed to do it by altering it to this:
For me this, this is a bit of a hacky solution. Can this be done in a more "natural" way?
export async function getPosts(start: number = 0, end: number = 9, _id?: string) { const getPostsQuery = groq` // Get posts That are in the selected category And order them by published date *[_type == "post" ${_id ? ' && $categoryId in categories[]._ref' : ''}] | order(publishedDate desc) { title, slug { current }, publishedDate, shortDescription, cardImage { ..., asset-> { ..., metadata { ..., } } } }[$start...$end] ` return client.fetch<GetPostsQueryResult>( getPostsQuery, { start, end, categoryId: _id ? _id : 'undefined' }, { next: { tags: ['posts'], revalidate: 60, }, } ) }
Jun 25, 2024, 2:13 PM
L
Do you know when the new sanity typegen will have support for this? As currently, using string interpolation isn't supported and stops types from being generated
Jun 26, 2024, 7:22 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.