Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Batch multiple queries in a single API call and drop in queryParams using the structure above.

61 replies
Last updated: Mar 24, 2022
Is there any ability to batch multiple queries into a single api call?
Mar 24, 2022, 4:04 AM
{"query1":*[_type == "post"]{title, content}, "query2":*[_type=="category"]{title}}
Mar 24, 2022, 4:06 AM
oh nice. Thanks
user Q
!
Mar 24, 2022, 4:08 AM
šŸ™‚
Mar 24, 2022, 4:09 AM
actually follow up question, what would be the proper way to drop in queryParams using the structure above?
Mar 24, 2022, 4:11 AM
What do you mean?
Mar 24, 2022, 4:12 AM
i think i got it, was referring to something like this
Mar 24, 2022, 4:12 AM
{
   "query1":*[_type == "post" && slug == $post_slug]{title, content}, 
   "query2":*[_type=="category" && slug == $cat_slug]{title}
}
Mar 24, 2022, 4:14 AM
I think that should work, make sure you serialize it before sending
Mar 24, 2022, 4:15 AM
.fetch({
   "query1":*[_type == "post" && slug == $post_slug]{title, content}, 
   "query2":*[_type=="category" && slug == $cat_slug]{title}
},{ $post_slug:'foo', $cat_slug:'bar'});

Mar 24, 2022, 4:15 AM
Iā€™m thinking that should work right? ā˜ļø
Mar 24, 2022, 4:16 AM
const res = fetch ('<https://foobar.api.sanity.io/v2021-10-21/data/query/production?query=>' + encodeURIComponent('QUERY HERE'))
Mar 24, 2022, 4:19 AM
this is essentially how I do it
Mar 24, 2022, 4:19 AM
Need to encode the query as a URIcomponent
Mar 24, 2022, 4:20 AM
Ok cool, thanks a lot appreciate the thoughts on this
Mar 24, 2022, 4:22 AM
Np. Basically you need to find your API address in the studio, then append your URI encoded query to the end of it.
Mar 24, 2022, 4:23 AM
This is a smart way to do it, just ran a test and it looks like you canā€™t do multiple queries like this using the sanity client
Mar 24, 2022, 4:26 AM
What do you mean?

{"sidebar":*[_type == "sidebar"]{title, content, "fileURL":pdf.asset->url},"contact":*[_type=="contact"][0],"pages":*[_type=="pages"]{title, "slug":slug.current},"settings":*[_type=="siteSettings"][0]{...,"logos":{"primary":logo1.asset->url, "secondary":logo2.asset->url},sponsors[]{title,"url":logo.asset->url}}}
Mar 24, 2022, 4:27 AM
^ This works fine in the sanity client
Mar 24, 2022, 4:27 AM
oh i see, youā€™re using projectionsā€¦ from your prior example was doing it a bit different
Mar 24, 2022, 4:29 AM
like taking a deep query and dropping into an object
Mar 24, 2022, 4:29 AM
{ myBigQuery: *[ā€¦]{ a bunch of stuff}, myOtherBigQuery: etcā€¦ }
Mar 24, 2022, 4:30 AM
All makes sense ā€” sorry for the confusion
Mar 24, 2022, 4:31 AM
No problem šŸ™‚
Mar 24, 2022, 4:31 AM
Coming from the GQL world, GROQ is quite impressive
Mar 24, 2022, 4:31 AM
I remember the gql wordpress days...šŸ¤®
Mar 24, 2022, 4:32 AM
This is my first go on a real project with Sanity and honestly I canā€™t imagine ever using another CMS after this
Mar 24, 2022, 4:32 AM
The level of control and customization is unmatched
Mar 24, 2022, 4:32 AM
It's incredible how intuitive it can be
Mar 24, 2022, 4:32 AM
But it also means I need to learn react, so...lol
Mar 24, 2022, 4:33 AM
Yep
Mar 24, 2022, 4:33 AM
Thankfully, I know the react world like the back of my hand so this has been smooth sailing
Mar 24, 2022, 4:33 AM
next.js + sanity = šŸ˜
Mar 24, 2022, 4:34 AM
Hah, lucky you. I should learn react anyways so it's nbd. Sanity + Svelte has been like walking on water from a dev experience standpoint
Mar 24, 2022, 4:34 AM
ooh yeah I bet. Svelte is awesome, Iā€™ve played around with it a bit
Mar 24, 2022, 4:35 AM
Just curious, do you do any freelance/contract work?
Mar 24, 2022, 4:36 AM
Have you used gatsby? That one looks intriguing
Mar 24, 2022, 4:36 AM
Yeah I'm a freelancer actually
Mar 24, 2022, 4:36 AM
Looks like your mountain time?
Mar 24, 2022, 4:36 AM
yeah
Mar 24, 2022, 4:36 AM
Nice, well I have a web dev shop in LA and we are on the hunt for some solid frontend devs as the project load has been growing
Mar 24, 2022, 4:37 AM
If youā€™re interested, shoot me a portfolio site and/or a linkedin profile so we can connect
Mar 24, 2022, 4:38 AM
We do a lot of entertainment stuff, and starting to get into brand work, and are itching to start doing some Web3 stuff
Mar 24, 2022, 4:38 AM
Anyhoo, sorry to get off topic just wanted to throw that out there
Mar 24, 2022, 4:39 AM
If youā€™re interested feel free to reach out to miked@droplab.com
Mar 24, 2022, 4:39 AM
Ooh thats cool, sounds like a fun set of clients
Mar 24, 2022, 4:39 AM
Yeah the entertainment stuff is fun for sure
Mar 24, 2022, 4:40 AM
I'm just making some changes to my business site, once I get it up I'll send you a link and stuff
Mar 24, 2022, 4:40 AM
šŸ‘
Mar 24, 2022, 4:41 AM
Are you in the US or CA ?
Mar 24, 2022, 4:41 AM
I'm curious, what is that cursor effect on the home page? three.js?Canada
Mar 24, 2022, 4:41 AM
Thatā€™s a shader we wrote, via three.js
Mar 24, 2022, 4:42 AM
Site is SUPER old
Mar 24, 2022, 4:42 AM
Weā€™ve been so busy, havenā€™t had time to update ā€” been trying for a couple years now lol
Mar 24, 2022, 4:43 AM
Good problem to have I guess
Mar 24, 2022, 4:43 AM
Hah, yes, good problem.
Mar 24, 2022, 4:43 AM
Are you KinLabs ?
Mar 24, 2022, 4:46 AM
thats me!
Mar 24, 2022, 4:47 AM
Cool, pinged you on LinkedIn
Mar 24, 2022, 4:48 AM
Definitely hit me up when you have site sorted out and/or wanna chat about some freelance opportunities
Mar 24, 2022, 4:49 AM
Sounds good, I'm definitely interested.
Mar 24, 2022, 4:49 AM
šŸ‘
Mar 24, 2022, 4:50 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.

Was this answer helpful?