Groq query in Sanity returning null values for some array items
7 replies
Last updated: Jun 30, 2022
A
in groq v1 the following query:
returns all the blocks that it can dereference as dereferenced objects, and
everything else as null:
Newer versions don't seem to do anything to the array in question returning
them as references instead.
Is there still a way to do this with relatively simple syntax?
*[_id == '90ce29b2-cab4-4790-bd97-57e1a97adaf5'][0] { 'blocks': blocks[]->, ...}
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": [ /*...*/] } ] //... }
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 } ], //... }
Jun 29, 2022, 5:04 PM
A
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
A
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
}
]
}
]
}
```
{
"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
O
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:
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 :)
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']->{...} }
Jun 30, 2022, 6:43 AM
O
Sorry if the formatting is off, wrote on my phone😅
Jun 30, 2022, 6:44 AM
A
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
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
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
A
also the issue still happens when using the explicit form `...@`:
The above returns all refs, unexpanded.
*[_id == '90ce29b2-cab4-4790-bd97-57e1a97adaf5'][0] { 'blocks': blocks[]->, ...@ }
Jun 30, 2022, 3:42 PM
O
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.