Batch multiple queries in a single API call and drop in queryParams using the structure above.
61 replies
Last updated: Mar 24, 2022
M
Is there any ability to batch multiple queries into a single api call?
Mar 24, 2022, 4:04 AM
B
{"query1":*[_type == "post"]{title, content}, "query2":*[_type=="category"]{title}}
Mar 24, 2022, 4:06 AM
M
oh nice. Thanks
user Q
!Mar 24, 2022, 4:08 AM
B
š
Mar 24, 2022, 4:09 AM
M
actually follow up question, what would be the proper way to drop in queryParams using the structure above?
Mar 24, 2022, 4:11 AM
B
What do you mean?
Mar 24, 2022, 4:12 AM
M
i think i got it, was referring to something like this
Mar 24, 2022, 4:12 AM
M
{ "query1":*[_type == "post" && slug == $post_slug]{title, content}, "query2":*[_type=="category" && slug == $cat_slug]{title} }
Mar 24, 2022, 4:14 AM
B
I think that should work, make sure you serialize it before sending
Mar 24, 2022, 4:15 AM
M
.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
M
Iām thinking that should work right? āļø
Mar 24, 2022, 4:16 AM
B
const res = fetch ('<https://foobar.api.sanity.io/v2021-10-21/data/query/production?query=>' + encodeURIComponent('QUERY HERE'))
Mar 24, 2022, 4:19 AM
B
this is essentially how I do it
Mar 24, 2022, 4:19 AM
B
Need to encode the query as a URIcomponent
Mar 24, 2022, 4:20 AM
M
Ok cool, thanks a lot appreciate the thoughts on this
Mar 24, 2022, 4:22 AM
B
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
M
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
B
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
B
^ This works fine in the sanity client
Mar 24, 2022, 4:27 AM
M
oh i see, youāre using projections⦠from your prior example was doing it a bit different
Mar 24, 2022, 4:29 AM
M
like taking a deep query and dropping into an object
Mar 24, 2022, 4:29 AM
M
{ myBigQuery: *[ā¦]{ a bunch of stuff}, myOtherBigQuery: etc⦠}
Mar 24, 2022, 4:30 AM
M
All makes sense ā sorry for the confusion
Mar 24, 2022, 4:31 AM
B
No problem š
Mar 24, 2022, 4:31 AM
M
Coming from the GQL world, GROQ is quite impressive
Mar 24, 2022, 4:31 AM
B
I remember the gql wordpress days...š¤®
Mar 24, 2022, 4:32 AM
M
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
M
The level of control and customization is unmatched
Mar 24, 2022, 4:32 AM
B
It's incredible how intuitive it can be
Mar 24, 2022, 4:32 AM
B
But it also means I need to learn react, so...lol
Mar 24, 2022, 4:33 AM
M
Yep
Mar 24, 2022, 4:33 AM
M
Thankfully, I know the react world like the back of my hand so this has been smooth sailing
Mar 24, 2022, 4:33 AM
M
next.js + sanity = š
Mar 24, 2022, 4:34 AM
B
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
M
ooh yeah I bet. Svelte is awesome, Iāve played around with it a bit
Mar 24, 2022, 4:35 AM
M
Just curious, do you do any freelance/contract work?
Mar 24, 2022, 4:36 AM
B
Have you used gatsby? That one looks intriguing
Mar 24, 2022, 4:36 AM
B
Yeah I'm a freelancer actually
Mar 24, 2022, 4:36 AM
M
Looks like your mountain time?
Mar 24, 2022, 4:36 AM
B
yeah
Mar 24, 2022, 4:36 AM
M
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
M
If youāre interested, shoot me a portfolio site and/or a linkedin profile so we can connect
Mar 24, 2022, 4:38 AM
M
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
M
Anyhoo, sorry to get off topic just wanted to throw that out there
Mar 24, 2022, 4:39 AM
M
If youāre interested feel free to reach out to miked@droplab.com
Mar 24, 2022, 4:39 AM
B
Ooh thats cool, sounds like a fun set of clients
Mar 24, 2022, 4:39 AM
M
Yeah the entertainment stuff is fun for sure
Mar 24, 2022, 4:40 AM
B
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
M
š
Mar 24, 2022, 4:41 AM
M
Are you in the US or CA ?
Mar 24, 2022, 4:41 AM
B
I'm curious, what is that cursor effect on the home page? three.js?Canada
Mar 24, 2022, 4:41 AM
M
Thatās a shader we wrote, via three.js
Mar 24, 2022, 4:42 AM
M
Site is SUPER old
Mar 24, 2022, 4:42 AM
M
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
M
Good problem to have I guess
Mar 24, 2022, 4:43 AM
B
Hah, yes, good problem.
Mar 24, 2022, 4:43 AM
M
Are you KinLabs ?
Mar 24, 2022, 4:46 AM
B
thats me!
Mar 24, 2022, 4:47 AM
M
Cool, pinged you on LinkedIn
Mar 24, 2022, 4:48 AM
M
Definitely hit me up when you have site sorted out and/or wanna chat about some freelance opportunities
Mar 24, 2022, 4:49 AM
B
Sounds good, I'm definitely interested.
Mar 24, 2022, 4:49 AM
M
š
Mar 24, 2022, 4:50 AM
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.