How to fetch all references in GROQ block content query?

12 replies
Last updated: Feb 9, 2021
Any GROQ experts (probably don’t even have to be an expert!) around who could help a newbie out with a query question? I have a bunch of references in my block content and I am unable to get them all ☹️
AI Update

Getting all references from block content can definitely be tricky! The key issue is that references in annotations are stored in a nested structure - they're inside the markDefs array, which itself is inside each block in your content array.

Here's the GROQ syntax you need to dereference those references:

content[]{
  ...,
  markDefs[]{
    ...,
    _type == "internalLink" => {
      "href": "/" + @.reference->slug.current
    }
  }
}

The special syntax here is the @.reference-> part. Since you're dealing with references inside an array (markDefs) that's inside another array (blocks), you need to use the @ operator to properly dereference them.

For a complete example, here's a more robust query pattern:

*[_type == "yourDocType"] {
  _id,
  title,
  content[]{
    ...,
    markDefs[]{
      ...,
      _type == "internalLink" => {
        "slug": @.reference->slug.current,
        "title": @.reference->title
      }
    }
  }
}

This will expand your reference annotations and include the data you need from the referenced documents. You can add any other fields from the referenced document you need inside that projection.

For image blocks with references, you'd handle them similarly:

content[]{
  ...,
  _type == "image" => {
    ...,
    asset->
  }
}

There's an excellent detailed guide that walks through this entire process, including how to set up the schema, query the data, and render it in React. It specifically covers the nested array dereferencing challenge you're facing!

The key takeaway: use @.reference-> when dereferencing inside nested arrays in GROQ. Hope this helps! 🎉

Show original thread
12 replies
This sort of thing?

const homepageQuery = `//groq
  *[_type == "homepage"][0] {
    ...,
    blocks[] {
      ...,
      posts[] {
        ...,
        post->
      }
    }
  }
`;
That gets all the post objects referenced from a posts[] array in my document.
Normal blocks are rendering just fine, but I am unable to render them AND get a hold of the buildingblock-references
0: {_key: "571c17e5d4d2", _type: "block", children: Array(1), markDefs: Array(0), style: "h2"}
1: {_key: "deffe7dcdb0a", _type: "block", children: Array(1), level: 1, listItem: "number", …}
2: {_key: "9e355d4baab8", _type: "block", children: Array(1), level: 2, listItem: "number", …}
3: {_key: "f52de8ccd592", _type: "block", children: Array(1), level: 1, listItem: "number", …}
4: {_key: "0ce7a53656c5", _type: "block", children: Array(1), level: 2, listItem: "number", …}
5: {_key: "5b0104c3c7e4", _type: "block", children: Array(1), level: 2, listItem: "number", …}
6: {_key: "8fb2b9b448a8", _type: "block", children: Array(1), level: 1, listItem: "number", …}
7: {_key: "0e385b17e55d", _type: "block", children: Array(1), level: 2, listItem: "number", …}
8: {_key: "47d9f8e5b64e", _type: "inlinePeople", peopleRef: Array(3), title: "Meet the team"}
9: {_key: "fd2ee0ce98a6", _ref: "f0954de0-3a3b-4ea1-901c-1f2850493532", _type: "buildingblock"}
10: {_key: "9a25a1d9a9ba", _ref: "8efd8242-bd4d-4d55-a7e1-af7178a6db57", _type: "buildingblock"}
11: {_key: "97844e8e1a61", _type: "block", children: Array(1), markDefs: Array(0), style: "normal"}
12: {_key: "4a1b7b529a6f", _type: "block", children: Array(1), markDefs: Array(0), style: "h2"}
13: {_key: "a08140c8bf86", _type: "block", children: Array(1), markDefs: Array(0), style: "normal"}
14: {_key: "762a6569ec0b", _type: "block", children: Array(2), level: 1, listItem: "number", …}
15: {_key: "5c4e40b23a94", _type: "block", children: Array(2), level: 1, listItem: "number", …}
16: {_key: "56205fc3075d", _type: "block", children: Array(2), level: 1, listItem: "number", …}
17: {_key: "5c99568385ef", _type: "block", children: Array(1), markDefs: Array(0), style: "h3"}
18: {_key: "f72092918e98", _type: "block", children: Array(1), level: 1, listItem: "bullet", …}
19: {_key: "d186ce9b79df", _type: "block", children: Array(1), level: 2, listItem: "bullet", …}
20: {_key: "7ba3cb37b16c", _type: "block", children: Array(1), level: 2, listItem: "bullet", …}
21: {_key: "bb98c7669e24", _type: "block", children: Array(4), markDefs: Array(0), style: "normal"}
22: {_key: "b2f689d44481", _type: "block", children: Array(2), markDefs: Array(0), style: "normal"}
23: {_key: "cddb325863d1", _ref: "1e71f787-078b-4132-b06e-b205daba028d", _type: "buildingblock"}
24: {_key: "3bcb742b5d77", _type: "block", children: Array(1), markDefs: Array(0), style: "normal"}
user L
yeah something like that. Currently my query (not yealding results) is looking like this:
Edit: not yealding
desired result as far as my references go
*[ slug.current == $slug][0]{
        ...,
        body[] {
            ...,
            buildingblocks->,
            inlinePeople {
                ...,
                peopleRef[]->
            }
        }
    }

buildingblocks
is
type: reference

inlinePeople
is
type: object
containing
peopleRef
which is an array of references
None of your references are working?
If I change the query to

*[ slug.current == $slug][0]{
        ...,
        body[]->{
            ...,
            buildingblocks->,
            inlinePeople {
                ...,
                peopleRef[]->
            }
        }
    }
then my buildingblock-references are working, but nothing else is returned, just empty objects
If someone wants to help it would be good for them to see your schema.
You need to expand the person reference.

blockContent[] {
  ...,
  inlinePeople[] {
    ...,
    peopleRef[] {
      ...,
      person->
    }
  }
}
Hm. Still can’t get the working.
Somehow this worked for outputting all general blocks +
buildingblocks


body[] {
            ...,
            _type=="buildingblock"=>^->,
            
            inlinePeople[] {
                ...,
                peopleRef[] {
                  ...,
                  person->
                }
              }
        }
    }
is body an array of blockContent ?
edit: no sorry, I’m confusing you.it
is the blockcontent
name: 'body', type: 'blockContent'
so this gave me what I was afterIf someone would care to explain to me what I’m doing differently here, I’d highly appreciate it

  body [] {
  ...,
 	_type=='inlinePeople' => {
    peopleRef[]->
  },
	_type=='buildingblock'=>^->
}

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?