How to expand an array of references with different types in Sanity.io
Perfect! For an array of references of different types, you can use the dereference operator (->) directly on the array items. Here's how to expand those references:
*[_type == 'settings'][0].homepage[]{
@->
}The @ symbol represents the current item in the array, and -> dereferences it. This will resolve all references in the array regardless of their type.
If your array might contain a mix of references and inline objects (not just references), you'll want to use coalesce to handle both cases:
*[_type == 'settings'][0].homepage[]{
...,
"content": coalesce(@->, @)
}This pattern works by:
@->attempts to dereference if the item is a reference@returns the item as-is if it's already an objectcoalescereturns the first non-null value
If you only want to expand items that are actually references, you can add a conditional check using the defined() function:
*[_type == 'settings'][0].homepage[]{
defined(@._ref) => @->,
!defined(@._ref) => @
}Or even simpler, if you want to select specific fields from the dereferenced documents:
*[_type == 'settings'][0].homepage[]->{
_type,
title,
// whatever fields you need
}The key is that the reference access operator (->) works with any reference type - it doesn't matter if your array contains references to different document types. Each reference will be resolved to its full document, and you can then project the fields you need from each one.
Show original thread14 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.