Is There a Way to Dereference (->) Only References?
Yes, you can selectively dereference only the references in a mixed array! The documented approach is to use conditional projections based on the _type field.
When you have an array containing both inline objects and references, reference objects have _type: "reference" while inline objects have their own _type values. You can use this to conditionally apply the dereference operator:
*[_type == "yourDocType"] {
mixedArray[]{
_type == "reference" => @->,
@
}
}This pattern uses object-level conditional projections where:
@refers to the current element in the array- The first line checks if
_type == "reference"and if true, dereferences with@-> - The second line returns the object as-is for any other type
- Only the first matching condition is applied to each element
You can also use the select() function for more explicit conditional logic:
*[_type == "yourDocType"] {
mixedArray[]{
select(
_type == "reference" => @->,
@
)
}
}Both approaches check the _type field to determine whether to dereference. This is particularly useful for page builder arrays or any field that accepts both inline content blocks and references to other documents.
Why this works: References in Sanity are stored as objects with _type: "reference" and a _ref field containing the document ID. The reference access operator (->) only resolves these reference objects, so by checking _type == "reference" first, you ensure dereferencing only happens when appropriate.
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.