👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Help with GROQ query for retrieving references in block content

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 ☹️
Feb 9, 2021, 8:35 AM
This sort of thing?

const homepageQuery = `//groq
  *[_type == "homepage"][0] {
    ...,
    blocks[] {
      ...,
      posts[] {
        ...,
        post->
      }
    }
  }
`;
Feb 9, 2021, 8:36 AM
That gets all the post objects referenced from a posts[] array in my document.
Feb 9, 2021, 8:37 AM
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"}
Feb 9, 2021, 8:37 AM
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
Feb 9, 2021, 8:58 AM
None of your references are working?
Feb 9, 2021, 9:02 AM
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
Feb 9, 2021, 9:06 AM
If someone wants to help it would be good for them to see your schema.
Feb 9, 2021, 9:07 AM
You need to expand the person reference.

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


body[] {
            ...,
            _type=="buildingblock"=>^->,
            
            inlinePeople[] {
                ...,
                peopleRef[] {
                  ...,
                  person->
                }
              }
        }
    }
Feb 9, 2021, 9:52 AM
is body an array of blockContent ?
Feb 9, 2021, 9:53 AM
edit: no sorry, I’m confusing you.it
is the blockcontent
name: 'body', type: 'blockContent'
Feb 9, 2021, 9:54 AM
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'=>^->
}
Feb 9, 2021, 12:02 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.

Was this answer helpful?