Help with GROQ

61 replies
Last updated: May 7, 2020
Hi, I was wondering if anyone could help me with GROQ?
AI Update

Hey! Happy to help you with GROQ! It's Sanity's query language for working with content, and once you get the hang of it, it's really powerful.

What is GROQ?

GROQ (Graph-Relational Object Queries) is Sanity's open-source query language designed specifically for filtering, fetching, projecting, joining, and transforming JSON documents from your Content Lake. Think of it as a way to ask for exactly the content you need, shaped exactly how you want it.

Basic Structure

The fundamental pattern of a GROQ query looks like this:

*[filter]{projection}
  • * = all documents (that you have permission to read)
  • [filter] = which documents you want (optional)
  • {projection} = how to shape the results (optional)

Quick Example

Here's a practical example that fetches directors born since 1970 with their movies:

*[_type == "director" && birthYear >= 1970]{
  name,
  birthYear,
  "movies": *[_type == "movie" && director._ref == ^._id]
}

Key Things to Know

It's a JSON superset: If you know JSON, you already understand part of GROQ's syntax. Any valid JSON is a valid GROQ query.

Pipeline-based: GROQ queries work as pipelines where results flow through each component. You can chain filters and projections together.

Whitespace doesn't matter: Format your queries however you like. You can also add comments with //.

Learning Resources

Sanity has great resources to help you learn:

What specific aspect of GROQ are you trying to work with? The basics above should get you started, but there's a lot more depth available depending on what you're building!

Show original thread
61 replies
It’s easier if you tell us what we can help you with :)
Okay so basically I'm trying to search my data set for certain criterias
but my dataset has references to other documents that I need to query also
for instance I have a documents that have _type == "dress"
and inside the dress documents there's a fabric key that has a _ref to a fabric document htat has a name
I want to query by the name of the dress and the name of the fabric
does that make sense?
like is there anyway I can join all of this stuff and then query off of that query result?
You can probably do this?

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

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

or probably:
"imageUrl": defaultImage.asset->url

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

cool, ty
Hi, is there a limit to the number of request we can hit
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?
Here’s the details - we also offer caching on CDN https://www.sanity.io/docs/technical-limits
Hi Knut, I was wondering if there's a way to remove case sensitivity while searching for something in a query?
Not at the moment, but I believe we’re working on a solution for that. Stay tuned!
ok, so what would be the best way to implement searching?
[for the future, it‘s better if you post new questions in the main thread]
kk
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
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?

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.

Was this answer helpful?