Expanding nested reference in GROQ query for array within flexible content

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.
AI Update

Looking at your GROQ query, the structure appears correct for expanding nested references. The issue you're experiencing is likely due to how the workGallery object is structured in your schema. Let me help you troubleshoot this.

Your query structure looks good:

*[_type== "home"] {
  pageSections[] {
    ...,
    testimonial->,
    works[] {
      ...,
      work->
    },
  }
}

The reference access operator (->) should work for nested arrays just like it does for your testimonial reference. Here are the most common reasons why it might not be working:

1. The work field might not be a reference type

Check your schema definition for the workGallery object. The work field inside the works array needs to be defined as a reference type:

{
  name: 'works',
  type: 'array',
  of: [{
    type: 'object',
    fields: [{
      name: 'work',
      type: 'reference',  // Must be type 'reference'
      to: [{type: 'work'}]
    }]
  }]
}

2. Try using conditional expansion

Sometimes when dealing with flexible content or union types in arrays, you need to be more explicit. Try this variation:

*[_type== "home"] {
  pageSections[] {
    ...,
    _type == "singleTestimonial" => {
      testimonial->
    },
    _type == "workGallery" => {
      works[] {
        ...,
        work->
      }
    }
  }
}

3. Check if the references are actually populated

Add a filter to see if the work references exist:

*[_type== "home"] {
  pageSections[] {
    ...,
    works[defined(work)] {
      ...,
      work->
    }
  }
}

4. Inspect the raw data structure

Try querying without the dereference first to see what the actual structure looks like:

*[_type== "home"] {
  pageSections[] {
    _type,
    works[] {
      work
    }
  }
}

This will show you if work contains a _ref field (which means it's a proper reference) or if it's structured differently.

The most likely culprit is that the work field isn't defined as a reference type in your schema, or there's a mismatch between your schema definition and the actual document structure. Check your schema first, and if that looks correct, use the debugging queries above to see what the actual data structure looks like.

Show original thread
4 replies
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. ”
It looks like the
works[]
array holds just the reference? if that’s the case, this may work,

*[_type== "home"] {
  pageSections[] {
    ...,
    testimonial->,
    works[]->
}
}
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!
user P
no problem! glad it worked for you 🙂

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?