Combining multiple GROQ queries in single Sanity client.fetch()
Yes, you can absolutely combine multiple GROQ queries into a single request! The key is understanding that the entire structure needs to be one GROQ query string that returns an object with multiple properties, not a JavaScript object containing separate query strings.
Based on the GROQ Query Cheat Sheet, here's the pattern for combining queries:
const combinedQuery = groq`{
"partnerData": *[_type == "partner" && slug.current == "${slug}"][0]{
name,
slug,
h1,
h2,
logo,
"logoAspectRatio": logo.asset->metadata.dimensions.aspectRatio,
undergrad_cosigned,
undergrad_noncosigned,
undergrad_outcomes,
mba,
medical,
dental,
law,
grad
},
"rateData": *[_type == "rates" && name == "December"]
}`;
// Single fetch call
const data = await client.fetch(combinedQuery);
// Access the results
const partnerData = data.partnerData;
const rateData = data.rateData;This works with both client.fetch() and usePreview() because you're passing a single query string that constructs an object containing the results of multiple queries.
Why this approach is better:
- Single network request - reduces latency and overhead
- Better performance - one round trip to the Content Lake
- Atomic execution - both queries run together
The documentation specifically shows this pattern:
{
'featuredMovie': *[_type == 'movie' && title == 'Alien'][0],
'scifiMovies': *[_type == 'movie' && 'sci-fi' in genres]
}So your instinct was correct! Just remember: it's one complete GROQ query string that builds an object, not separate JavaScript strings.
Show original thread5 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.