Querying for referenced entities in Sanity.io

2 replies
Last updated: Jun 19, 2023
Hello everyone, is it possible to write such a query?At the moment, I have such an entity:

{
  _id: 1,
  buildings: [
    {
      _ref: 2,
      name: 'Second building',
    },
    {
      _ref: 3,
      ...
    }
  ]
}
and the entity with id "2" and "3" has the following structure:

{
  id: 2,
  name: 'Second building',
  additionalInfo: 'Additional info here
}
{
  id: 3,
  ...
}
Can i get all the fields of the entities referenced by the first object at once?
Jun 19, 2023, 3:31 PM
Hi
user P
. If you already have a query in place that returns the document with
_id: 1
, you could dereference the
buildings
array:

*[_id == 1]{
  ...,  // This returns all other fields on the document
  'buildings': buildings[]->,
}
Jun 19, 2023, 3:57 PM
user A
, thanks man, it helped me!
Jun 19, 2023, 3:59 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?