Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

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},
Mar 15, 2021, 7:18 PM
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
},
Mar 15, 2021, 7:26 PM
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' ?
}
Mar 15, 2021, 7:31 PM
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.
Mar 15, 2021, 7:40 PM
hm yeah i cannot get the
->
to work, it just gives me an empty object
Mar 15, 2021, 7:44 PM
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).
Mar 15, 2021, 7:44 PM
"homepage": [
    {
      "_key": "2cd627b15193",
      "_ref": "d20348f4-3ca2-4059-a45a-2595928dbc87",
      "_type": "reference"
    },
    {
      "_key": "de795aaa238e",
      "_ref": "04f03539-4da8-45ee-b790-77b97f2983f0",
      "_type": "reference"
    }
  ]
Mar 15, 2021, 7:47 PM
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.
Mar 15, 2021, 7:57 PM
AH
Mar 15, 2021, 8:01 PM
i found what was wrong i think T_T
Mar 15, 2021, 8:01 PM
would it bug out IF one of the
of: [ {type: 'reference'}]
was missing a
name
?
Mar 15, 2021, 8:01 PM
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.
Mar 15, 2021, 8:05 PM
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.
Mar 15, 2021, 8:11 PM
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.
Mar 15, 2021, 8:13 PM
also super thanks
user A
, today I learned that its possible to just do
someReference[]->
within and it;ll just follow
Mar 15, 2021, 8:14 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?