Help needed with expanding and filtering content objects in a GROQ query.
I can see the issue you're running into! This is a common challenge when working with arrays that contain both inline objects and references. Let me break down what's happening and how to fix it.
The Problem
When you use content[]->, GROQ tries to dereference every item in the array. Items that are references (like your properties) get properly dereferenced, but items that are already inline objects (like textSection, vimeo, imageSection) return empty objects because there's nothing to dereference.
The Solution
You need to handle mixed content types in your array. The best approach is to use conditional dereferencing with the @ operator (which refers to the current item):
*[_type == "route" && slug.current == $slug][0]{
page-> {
...,
content[] {
...,
_type == "properties" => @->,
cta {
...,
route->
},
ctas[] {
...,
route->
}
}
}
}This uses GROQ's conditional syntax: when _type == "properties" is true, it dereferences with @->, otherwise it just spreads the existing object with ....
Alternative: Using select()
Another approach is using the select() function, which can be cleaner for this use case:
*[_type == "route" && slug.current == $slug][0]{
page-> {
...,
content[] {
...,
select(
_type == "properties" => @->,
@ // Return the item as-is for non-references
),
cta {
...,
route->
},
ctas[] {
...,
route->
}
}
}
}Why Your _key Was Missing
When you used the separate 'properties': content[_type == 'properties']-> approach, you lost the _key because you were creating a new array projection outside the original content array context. The _key exists on the reference object in the content array, not on the referenced document itself.
The Why
The reference access operator (->) in GROQ is designed to dereference references by internally executing a subquery to fetch the referenced document. When you write content[]->, you're telling GROQ "dereference everything in this array," which doesn't work for mixed content. By using conditional logic, you're saying "only dereference items that are actually references," which preserves your inline objects while expanding your properties references.
This should give you a single content array with all items properly expanded, maintaining their _key values, and working correctly with your RenderSections component!
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.