Join our next Developer Virtual Meetup – Live Q&A, Project Showcase, Swag raffle, and more 🔥

Querying with GROQ in Sanity.io Slack thread

61 replies
Last updated: May 7, 2020
Hi, I was wondering if anyone could help me with GROQ?
Apr 15, 2020, 7:39 PM
It’s easier if you tell us what we can help you with :)
Apr 15, 2020, 7:41 PM
Okay so basically I'm trying to search my data set for certain criterias
Apr 15, 2020, 7:42 PM
but my dataset has references to other documents that I need to query also
Apr 15, 2020, 7:42 PM
for instance I have a documents that have _type == "dress"
Apr 15, 2020, 7:43 PM
and inside the dress documents there's a fabric key that has a _ref to a fabric document htat has a name
Apr 15, 2020, 7:43 PM
I want to query by the name of the dress and the name of the fabric
Apr 15, 2020, 7:43 PM
does that make sense?
Apr 15, 2020, 7:43 PM
like is there anyway I can join all of this stuff and then query off of that query result?
Apr 15, 2020, 7:44 PM
You can probably do this?

*[_type == “dress”]{
  “fabric”: fabric->name
}
Apr 15, 2020, 7:45 PM
yeah that's for the join, right?
Apr 15, 2020, 7:46 PM
You can learn more about joins here https://www.sanity.io/docs/groq-joins
Apr 15, 2020, 7:46 PM
I basically want to do like a search though like `[_type === "dress" && fabricname == "name"]
Apr 15, 2020, 7:46 PM
[_type === "dress" && fabricname == "name"]
Apr 15, 2020, 7:46 PM
I understand how to do the joins
Apr 15, 2020, 7:46 PM
[_type === "dress" && fabric->name == "name"]
Apr 15, 2020, 7:46 PM
I want to be able to do conditionals on a document and the document that are referenced
Apr 15, 2020, 7:46 PM
Same syntax :)
Apr 15, 2020, 7:47 PM
I can do that without joining in the first place?
Apr 15, 2020, 7:47 PM
Yup!
Apr 15, 2020, 7:47 PM
Ok, gonna try it real quick
Apr 15, 2020, 7:47 PM
Covered in the “semijoins” section here https://www.sanity.io/docs/groq-joins#semijoins-662d6d368945
Apr 15, 2020, 7:48 PM
Thank you sir
Apr 15, 2020, 7:49 PM
What if my document has an array of properties which has a property that references another document?
Apr 15, 2020, 7:59 PM
this works for the join
Apr 15, 2020, 7:59 PM
colors[]{color->{name}}
Apr 15, 2020, 7:59 PM
but what would be the syntax for that for a conditional?
Apr 15, 2020, 8:00 PM
colors[].color->name doesn't work
Apr 15, 2020, 8:00 PM
*[“red” in colors[].color->name]

Provided the
_ref
is on the
code
key
Apr 15, 2020, 8:04 PM
cool
Apr 15, 2020, 8:12 PM
so -> always looks for a _ref?
Apr 15, 2020, 8:13 PM
Correct!
Apr 15, 2020, 8:33 PM
Quick one more question
Apr 15, 2020, 9:15 PM
so all of this worked and here is my result
Apr 15, 2020, 9:15 PM
defaultImage: {
  
asset: {
   
url: '<https://cdn.sanity.io/images/6ci6a8f7/production/3abd50a00ca8269ae3e0fe33a64103e0c2099bc2-1200x1638.jpg>'
  
}
 
}
Apr 15, 2020, 9:15 PM
is there anyway to take that url out of that nested structure and have it on a parent property?
Apr 15, 2020, 9:15 PM
"imageUrl": defaultImage.asset.url

Apr 15, 2020, 9:17 PM
or probably:
"imageUrl": defaultImage.asset->url

Apr 15, 2020, 9:18 PM
Hey Knut, thanks for much for yur time
Apr 16, 2020, 3:56 PM
your*
Apr 16, 2020, 3:56 PM
I just have one more question
Apr 16, 2020, 3:56 PM
I have the following part in my query
Apr 16, 2020, 4:24 PM
colors[]{color->{name,"url": image{asset->{url}}}}
Apr 16, 2020, 4:24 PM
this makes a property called colors which is an array of the following objects
Apr 16, 2020, 4:24 PM
{ color: { name: 'Auburn Moon', url: [Object] } }
Apr 16, 2020, 4:25 PM
I just want it to be
Apr 16, 2020, 4:25 PM
colors: [{ name 'Auburn Moon', url: 'string' }, ....]
Apr 16, 2020, 4:25 PM
nevermind, I figured it out! thx
Apr 16, 2020, 4:31 PM
oh, so I have this:
"images": [defaultImage.asset->url]
Apr 16, 2020, 4:32 PM
how can I have it return an empty array if nothign is in there? instead of [null]
Apr 16, 2020, 4:32 PM
try
"images": coalesce([defaultImage.asset->url], [])

Apr 16, 2020, 5:00 PM
cool, ty
Apr 16, 2020, 7:15 PM
Hi, is there a limit to the number of request we can hit
Apr 17, 2020, 6:02 PM
Like do you recommend making a backend endpoint to hit your endpoint and add a caching service to limit the amount of times the sanity endpoint gets hit?
Apr 17, 2020, 6:11 PM
Here’s the details - we also offer caching on CDN https://www.sanity.io/docs/technical-limits
Apr 17, 2020, 6:19 PM
Hi Knut, I was wondering if there's a way to remove case sensitivity while searching for something in a query?
May 4, 2020, 10:23 PM
Not at the moment, but I believe we’re working on a solution for that. Stay tuned!
May 5, 2020, 6:33 AM
ok, so what would be the best way to implement searching?
May 5, 2020, 6:33 AM
[for the future, it‘s better if you post new questions in the main thread]
May 5, 2020, 6:33 AM
kk
May 5, 2020, 6:34 AM
If you hit the wall with GROQ, you can also look at dedicated search services, like https://www.sanity.io/blog/indexing-in-algolia-using-serverless-functions-and-observables
May 5, 2020, 6:34 AM
Thanks for this, quick question. I have to run GROQ queries to get the data shaped into a certain format, how can I stream this with using the client rather than the export API so I can leverage observables?
May 7, 2020, 6: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?