Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Fetching multiple queries at once with the Sanity client

24 replies
Last updated: Aug 18, 2022
Hey everyone. Is there a way to fetch multiple queries at once with the Sanity client? Let’s say I’ve this code:
  const firstQuery = `*[groq here][]{}`
  const secondQuery = `*[groq here][]{}`
  const thirdQuery = `*[groq here][]{}`

  const firstProp = await sanityClient.fetch(firstQuery)
  const secondProp = await sanityClient.fetch(secondQuery)
  const thirdProp = await sanityClient.fetch(thirdQuery)

  return {
    props: {
      firstProp,
      secondProp,
      thirdProp,
    },
  }
This seems wasteful somehow. Could I just fetch all three queries at the same time?
Aug 18, 2022, 1:44 PM
you make a query that looks like the following :
const allQueries =
{ firstQuery, secondQuery, thirdQuery }
and thenyou can fetch it with
sanityClient.fetch(allQueries)
and get the result will be an object with the result of each query in the correspondingly named field.
Aug 18, 2022, 1:47 PM
Awesome. I’ll give this a try. So this would return a single prop as an object, correct?
Aug 18, 2022, 1:54 PM
Yeah, it would return a single object, with the result of each query in the named field.You could define the object like this

const allQueries = { firstProp: firstQuery, secondProp: secondQuery, thirdProp: thirdQuery } 
const results = await sanityClient.fetch(allQueries }
return { props : results }
And that would be equivalent to your example-code.

The object you define your queries with is basically a javascript-object. You can name the fields anything you want, and Sanity will take the query in each field, and return a javascript object with the results of the query behind your chosen name in the object.
Aug 18, 2022, 1:58 PM
This is great. Excellent explanation! I’ll give this a try
Aug 18, 2022, 2:07 PM
So I’m getting a weird error:
ClientError: expected ']' following array body
Aug 18, 2022, 2:22 PM
So I’m getting a weird error:
ClientError: expected ']' following array body
The first query returns an array of objects (which is expected), while the other two just a single one.
Aug 18, 2022, 2:23 PM
Your query is invalid. 🙂
Aug 18, 2022, 2:28 PM
Hi
user F
. Thought about that, but the same query worked just fine using three separate fetches…
Aug 18, 2022, 2:31 PM
Show me?
Aug 18, 2022, 2:32 PM
Sure. This works:
  const firstQuery = `*[groq here][]{}`
  const secondQuery = `*[groq here][]{}`
  const thirdQuery = `*[groq here][]{}`
  
  const firstProp = await sanityClient.fetch(firstQuery)
  const secondProp = await sanityClient.fetch(secondQuery)
  const thirdProp = await sanityClient.fetch(thirdQuery)

  return {
    props: {
      firstProp,
      secondProp,
      thirdProp,
    }
  }
This does not:

const firstQuery = `*[groq here][]{}`
  const secondQuery = `*[groq here][]{}`
  const thirdQuery = `*[groq here][]{}`
 
  const allQueries = {
    firstProp: firstQuery,
    secondProp: secondQuery,
    thirdProp: thirdQuery,
  }

  const allProps = await sanityClient.fetch(allQueries)

  return {
    props: {
      allProps
    }
  }
Aug 18, 2022, 2:39 PM
Yes, because the fetch method doesn’t accept an object, it expects a string.
Aug 18, 2022, 2:41 PM
const { a, b, c } = await sanityClient.fetch(
  `{
    "a": *[…],
    "b": *[…],
    "c": *[…]
  }`
);

return {
  props: { a, b, c },
};
Aug 18, 2022, 2:42 PM
ahhhh! let’s try this
Aug 18, 2022, 2:44 PM
That was it! you’re a genius. I moved all the queries to a string as per your example and it worked.
Aug 18, 2022, 2:50 PM
Lovely. 😊
Aug 18, 2022, 2:52 PM
From your example, how would I pass a param to the query? This doesn’t seem to work:
const querya = `*[groq here][]{}`
const queryb = `*[groq here][]{}`
const queryc = `*[groq here][]{}`

const { a, b, c } = await sanityClient.fetch(
  `{
    "a": $querya,
    "b": $queryb,
    "c": $queryc
  }`, 
{
querya: querya,
queryb: queryb,
queryc: queryc
}
);

return {
  props: { a, b, c },
};
Aug 18, 2022, 2:59 PM
You seem to be mixing up dynamically constructing the query string with different variables, and passing parameters to your GROQ queries. The latter expect variables in certain places, you can’t pass a whole expression and expect it to work. 🙂
Aug 18, 2022, 7:47 PM
If you just want to construct your meta query from different variables, use interpolation or a template literal instead.
const query = `{
  "a": ${queryA},
  "b": ${queryB},
  "c": ${queryC}
}`
Aug 18, 2022, 7:47 PM
Yeah I guess I was. I was thinking about params and it was the wrong approach
Aug 18, 2022, 7:50 PM
However, if your queries do need params, you still can pass them as usual:
const queryA = `*[ _type == "post" && category == $category ]`
const queryB = `*[ _type == "author" ]`
const query = `{ "a": ${queryA}, "b": ${queryB} }`
const { a, b } = await client.fetch(query, { category: 'foobar' })

Aug 18, 2022, 7:53 PM
Here we have a meta query made of 2 sub-queries A and B. Sub-query A actually needs a category via the
$category
parameter. We pass that argument to the fetch as usual. 🙂
Aug 18, 2022, 7:54 PM
This is awesome, perfect for what I needed. Thanks
user F
! 🥇
Aug 18, 2022, 7:59 PM
Fab. 💚
Aug 18, 2022, 8:00 PM

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.

Was this answer helpful?