<question>Destructuring array of references inside fieldset/nested object in GROQ</question>
Based on your data structure, the issue is that content is not a reference (it doesn't have _ref), it's a nested object. You can't use the dereference operator -> on regular objects, only on references.
Here's how to properly query your data:
*[_type == "stylist" && name == "Alex Clark"] {
name,
"services": content.services[]-> {
name,
// any other fields from the service document
}
}The key difference is:
- Use dot notation (
content.services) to access fields inside nested objects - Use the dereference operator (
->) only on the actual references inside the array - Use quotes to create a custom field name in your projection
If you want to keep the entire content structure but expand the services, you can use the spread operator:
*[_type == "stylist" && name == "Alex Clark"] {
name,
content {
businessName,
emailAddress,
phoneNumber,
suiteNumber,
hours,
"services": services[]-> {
name,
// other service fields
}
}
}Or if you want to expand services while keeping all other content fields as-is:
*[_type == "stylist" && name == "Alex Clark"] {
name,
content {
...,
"services": services[]-> {
name
}
}
}The ... (spread operator) includes all fields from the content object, and then you override just the services array with the dereferenced version.
The fundamental rule here from the GROQ projections documentation: -> only works on references (objects with _ref), while dot notation traverses into regular nested objects. Your content object is just a regular nested object (even though it has _type: "object"), so you need to use content.services to access the array before dereferencing each item.
Show original thread4 replies
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.