Joint session with Vercel: How to build intelligent storefronts (May 15th)

GROQ: Resolve only reference elements in array, keep objects intact

10 repliesLast updated: Nov 29, 2025

Hi! I have a question about
GROQ
and
references

I have this query:

*[_type == "page" && pageMetadata.slug == 'test'][0]{
    pageBuilderComponents
}
It returns an array. The problem is that
some (but not all) of these elements are references, while the rest are objects. I would like to resolve the references.
I have tried
pageBuilderComponents[]->
. This will resolve the references correctly, but those elements which were already objects, will now become
null
.
How can I resolve only those elements that are references?
AI Update

Perfect! The article actually covers your exact use case. When you have an array with mixed content (some references, some inline objects), you can use the coalesce() function with the @ operator.

Here's the solution:

*[_type == "page" && pageMetadata.slug == 'test'][0]{
  pageBuilderComponents[]{
    ...coalesce(@->, @)
  }
}

How this works:

This pattern is mentioned in the Reference Access Operator documentation as the recommended approach for handling mixed content types in arrays.

Alternative approach if you want more explicit control:

*[_type == "page" && pageMetadata.slug == 'test'][0]{
  pageBuilderComponents[]{
    _type,
    select(
      defined(_ref) => @->,
      !defined(_ref) => @
    )
  }
}

This uses select() to check if the element has a _ref field (indicating it's a reference) and conditionally resolves it. However, the coalesce approach is cleaner and more idiomatic for this use case.

Show original thread
10 replies

Was this answer helpful?

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.

Related contributions