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

Groq query in Sanity returning null values for some array items

7 replies
Last updated: Jun 30, 2022
in groq v1 the following query:

*[_id == '90ce29b2-cab4-4790-bd97-57e1a97adaf5'][0] {
  'blocks': blocks[]->,
  ...}
returns all the blocks that it can dereference as dereferenced objects, and
everything else as null:


// v1
result: {
  //...
  "blocks": [
  null, // <----------------- missing ref
  {
    "_createdAt": "2022-06-15T17:25:28Z",
    "_id": "f66bfdca-f03a-4bfe-a4fa-cda7537a448a",
    "_rev": "kwp71lZJIGHA037Eneiyj2",
    "_type": "prose",
    "_updatedAt": "2022-06-29T16:37:37Z",
    "content": [ /*...*/]
  }
]
  //...
}
Newer versions don't seem to do anything to the array in question returning
them as references instead.


// v2021-03-25
result: {
  //...
  "blocks": [
    {
      "_key": "293cc22154b1",
      "_ref": "968147d3-a988-4b05-8faa-21a1d3182650",
      "_type": "linkMenuRef",
      "_weak": true
    },
    {
      "_key": "f280c4e697e1",
      "_ref": "f66bfdca-f03a-4bfe-a4fa-cda7537a448a",
      "_type": "proseRef",
      "_weak": true
    }
  ],
  //...
}
Is there still a way to do this with relatively simple syntax?
Jun 29, 2022, 5:04 PM
it's perhaps important to explain that blocks is an array of references, each with a name and each to a specific document type. This allows us to select the kind of thing we want to insert first, then which of those things to insert second, rather than seeing all possible choices in the list at once
Jun 29, 2022, 5:11 PM
This is a cut-down version of the schema:
```
{
"name": "page",
"type": "document",
"title": "Page",
"fields": [
{
"name": "blocks",
"type": "array",
"title": "PageBuilder Sections",
"of": [
{
"type": "reference",
"title": "Link Menu",
"name": "linkMenuRef",
"to": {
"type": "linkMenu"
},
"weak": true
},
{
"type": "reference",
"title": "Rich Text Content",
"name": "proseRef",
"to": {
"type": "prose"
},
"weak": true
}
]
}
]
}
Jun 29, 2022, 5:15 PM
I’ve had issues with having the object expansion ‘…’ AFTER the specific property-projection. Try moving it above maybe!
Also you can extract the types and group them like so:


*[_id == '90ce29b2-cab4-4790-bd97-57e1a97adaf5'][0] {
  ...,
  'linkMenus': blocks[@->_type match 'linkMenu']->{...},
  'prose': blocks[@->_type match 'prose']->{...}
}
Not sure if this helps answer your questipn but thought I’d give it a whirl since it sounded like something I struggled with recently :)
Jun 30, 2022, 6:43 AM
Sorry if the formatting is off, wrote on my phone😅
Jun 30, 2022, 6:44 AM
I know about the grouping you mentioned second, but am trying to avoid that explicit list in this case.
But your suggestion of using the the
...
operator first worked, which is crazy!
Thanks so much for zeroing right in on what seems to be a parsing bug, that unsticks me on a spike that was running over.

Sanity folks, is this a known issue? I couldn't find it
Jun 30, 2022, 3:42 PM
also the issue still happens when using the explicit form `...@`:

*[_id == '90ce29b2-cab4-4790-bd97-57e1a97adaf5'][0] {
  'blocks': blocks[]->,
  ...@
}
The above returns all refs, unexpanded.
Jun 30, 2022, 3:42 PM
Fantastic! Glad I could help! Me and my team were ripping our hair out on this issue exactly 😅
Jun 30, 2022, 4:22 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?