How to Write a GROQ Join Query for a Reference Type
1 replies
Last updated: Jul 21, 2021
AI Update
Glad you figured it out! For anyone else who comes across this thread with a similar issue, the solution is to fix the GROQ query syntax. The correct query should be:
*[_type == "homepage"] {
...,
"home_testimonials": home_testimonials[]->{
content,
jobTitle,
author
}
}The key differences:
- Use
home_testimonials[]->(the actual field name from your schema, not "testimonial") - The
->operator dereferences the reference to get the actual testimonial document - No need for a nested
testimonial->inside
This will expand each reference in the home_testimonials array and return the actual content, jobTitle, and author fields from each referenced testimonial document.
You can learn more about dereferencing in GROQ in the reference type documentation and the GROQ query cheat sheet.
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.