Is there a GROQ Query where References in a PortableText Array of Blocks Are Also Resolved?
Yes! You can resolve references within PortableText arrays using the @ operator combined with conditionals. The @ operator refers to the root value of the current scope, which is perfect for handling mixed content where some items are references and others aren't.
Here's the pattern you'll want to use:
*[_type == "yourDocType"] {
content[]{
...,
_type == 'yourReferenceType' => {
'resolved': @->
}
}
}For example, if you have a PortableText array with custom reference blocks mixed with regular text spans, you can do:
{
"content": content[]{
...,
_type == 'orderReceiptPlaceholderRef' => {
'referenceData': @->
}
}
}The key points:
- Use
@->instead of just->- The@operator targets the current item in the array iteration, then->dereferences it - Conditionals preserve non-reference items - Items that don't match your reference type condition will pass through with the
...(spread operator) - Project the resolved reference - You typically want to give the resolved data a field name like
'referenceData': @->
This approach works because:
children[]->would try to dereference everything, leaving non-references as nullchildren[]alone includes everything but doesn't resolve references- The conditional with
@->selectively resolves only the reference types while preserving everything else
You can find more details about GROQ conditionals in the query cheat sheet and learn about the reference access operator (->) in the GROQ joins specification.
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.