Is There a Way to Dereference (->) Only References?

3 replies
Last updated: Mar 2, 2021
if i have an array that includes both objects and references, is there any way to dereference (
->
) only the references, while leaving the other elements intact?
AI Update

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.

My
navigation
schema is an array of
references
to pages or url-based links (i.e. possibly external):

export default {
  name: "navigation",
  title: "Navigation",
  type: "object",
  fields: [
    {
      type: "array",
      name: "navigation",
      of: [
        {
          title: "Link to page",
          type: "reference",
          to: [{ type: "page" }],
        },
        { title: "External link", type: "navLink" },
        {
          title: "Dropdown menu",
          type: "navigation",
        },
      ],
    },
  ],
For this, we can ignore the nested array children, I just want to get the info on
page
references, while also having the
navLinks
not be sent to null when I deference all children (i.e.
navigation[] ->
.
I guess the overarching questions is how to use
select()
or other features to conditionally dereference/return projections
😮 i assumed that
_type == ___ =>
statements needed to be in a select! thanks

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.

Was this answer helpful?