How to expand an array of references with different types in Sanity.io
14 replies
Last updated: Mar 15, 2021
L
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
L
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
L
hm what i meant was if its an
do i do
arraywith 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
L
From your code:
should give you the same as:
which you can replace with:
to resolve the references in the homepageSelection array.
I’m sorry, I don’t quite follow your question about news and fruits.
*[_type == 'settings'][0].homepageSelection[]
*[_type == 'settings'][0] { homepageSelection[] }
*[_type == 'settings'][0] { homepageSelection[]-> }
I’m sorry, I don’t quite follow your question about news and fruits.
Mar 15, 2021, 7:40 PM
L
hm yeah i cannot get the
->to work, it just gives me an empty object
Mar 15, 2021, 7:44 PM
L
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
L
"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
L
Hmmm… that’s odd. I have a similar snippet of code where this:
returns virtually the same thing as what you posted and
gives me all the properties on each of the actual documents referenced in the
*[_type == "post"][3] { involved[] }
*[_type == "post"][3] { involved[]-> }
involvedarray.
Mar 15, 2021, 7:57 PM
L
AH
Mar 15, 2021, 8:01 PM
L
i found what was wrong i think T_T
Mar 15, 2021, 8:01 PM
L
would it bug out IF one of the
of: [ {type: 'reference'}]was missing a
name?
Mar 15, 2021, 8:01 PM
L
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
L
The only other thing I could think is if this is a mix of references and non-references (i.e.,
of: []includes
type: referenceand some other type(s)). If that’s the case, we could configure your query based on the type.
Mar 15, 2021, 8:11 PM
L
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
L
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.