How to expand an array of references with different types in Sanity.io

14 replies
Last updated: Mar 15, 2021
Help! i have an array of references of different types, how do i get an array with the expanded contents?
homepage': *[_type == 'settings'][0].homepage[]{
          // how do i expand the references here? it's an array of different kinds of references},
AI Update

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 object
  • coalesce returns 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 thread
14 replies
You can add
->
after any property you want to expand. For example:

'homepage': *[_type == 'settings'][0].homepage[] {
  image,     // Does not follow the reference
  image->,   // Follows the reference
},
hm what i meant was if its an
array
with multiple reference types to be part of the array. how can i expand e.g. a reference of type
news
(with its own sets of fields) and
fruits
? I want to honor the order of the array as well
do i do

*[_type == 'settings'][0].homepageSelection[]{
    // what do i put here because the output is just _key, _ref, and _type: 'reference' ?
}
From your code:

*[_type == 'settings'][0].homepageSelection[]
should give you the same as:


*[_type == 'settings'][0] {
  homepageSelection[]
}
which you can replace with:


*[_type == 'settings'][0] {
  homepageSelection[]->
}
to resolve the references in the homepageSelection array.

I’m sorry, I don’t quite follow your question about news and fruits.
hm yeah i cannot get the
->
to work, it just gives me an empty object
Would you be willing to post that part of your schema? Or the JSON from that document (from the document, go to the three dots in the top-right corner and choose Inspect).
"homepage": [
    {
      "_key": "2cd627b15193",
      "_ref": "d20348f4-3ca2-4059-a45a-2595928dbc87",
      "_type": "reference"
    },
    {
      "_key": "de795aaa238e",
      "_ref": "04f03539-4da8-45ee-b790-77b97f2983f0",
      "_type": "reference"
    }
  ]
Hmmm… that’s odd. I have a similar snippet of code where this:

*[_type == "post"][3] {
  involved[]
}
returns virtually the same thing as what you posted and


*[_type == "post"][3] {
  involved[]->
}
gives me all the properties on each of the actual documents referenced in the
involved
array.
AH
i found what was wrong i think T_T
would it bug out IF one of the
of: [ {type: 'reference'}]
was missing a
name
?
Your array needs a name but the members of the array (i.e., the stuff in
of: []
) and the types being referenced in an array (i.e., the stuff in
to: []
) shouldn’t need it.
The only other thing I could think is if this is a mix of references and non-references (i.e.,
of: []
includes
type: reference
and some other type(s)). If that’s the case, we could configure your query based on the type.
ah! that might be the case. good to keep in mind 🙏 I've sine then reconfigured the schema and how querying the data for use in the page, and now i think it's working.
also super thanks
user A
, today I learned that its possible to just do
someReference[]->
within and it;ll just follow

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?