🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Trouble constructing a groq query to expand a nested reference to a 'work' document.

4 replies
Last updated: Mar 17, 2023
I'm having trouble constructing a groq query to expand a nested reference to a 'work' document. I have and array of flexible objects on the 'home' document named 'pageSections', one of which is a 'workGallery' that includes an array of 'works' each of which reference a 'work' document.

*[_type== "home"] {

pageSections[] {

...,

testimonial->,

works[] {

...,

work->

},

}

}

I have another section named singleTestimonial that references a 'testimonial' document which I can expand. But it doesn't work with the 'work' one. I'm at a bit of a loss.
Mar 16, 2023, 1:54 AM
This is what I get if I just query the pageSections in the vision playground:
[…] 1 item
0:{…} 1 property
pageSections:[…] 3 items
0:{…} 6 properties
_key:c8d750fa9d9e
_type:workGallery
color:{…} 2 properties
testimonial:null
title:Work
works:[…] 2 items
0:{…} 3 properties
_key:39263f5a6a9a
_ref:d38f6b0f-d7cf-48a0-ac0e-7a0e97799e6e
_type:reference
1:{…} 3 properties
1:{…} 8 properties
_key:124f360e1ca4
_type:threeColList
color:{…} 2 properties
introTitle:We design websites, publications and brands.We develop for mobile, tablet and desktop devices.
listOne:[…] 3 items
listTwo:[…] 3 items
testimonial:null
title:Services
2:{…} 4 properties
_key:cf18bfa2328e
_type:singleTestimonial
color:{…} 2 properties
testimonial:{…} 10 properties
_createdAt
:2023-03-09T19:12:50Z_id:558cb736-980c-4463-8a9b-e6ca4c6afbef
_rev:7l6vPAu5IoEF7LxhjFh3lC
_type:testimonial
_updatedAt
:2023-03-09T19:12:50ZshortQuote:“They were especially good at bringing our organisation and our customers experiences together...and translating that into a website that delivers a great experience. ”
Mar 16, 2023, 2:15 AM
It looks like the
works[]
array holds just the reference? if that’s the case, this may work,

*[_type== "home"] {
  pageSections[] {
    ...,
    testimonial->,
    works[]->
}
}
Mar 16, 2023, 12:02 PM
Hey
user J
you legend! That worked perfectly. I think I had in my head I was expanding the reference within rather than the whole array. Thanks so much for the help!
Mar 17, 2023, 1:26 AM
user P
no problem! glad it worked for you 🙂
Mar 17, 2023, 1:29 AM

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?