How to fill arrays of references in a single query
How to fill arrays of references in a single query
I have a schema type Page which has an array of blocks:
title: 'Page',
name: 'page',
type: 'document',
fields: [
...
title: 'Blocks',
name: 'blocks',
type: 'array',
of: [
type: 'tileGrid',
type: 'otherType'
],
options:
editModal: 'fullscreen'
]
The type tileGrid has the following fields:
tileGrid
title: 'Tiles',
name: 'tiles',
type: 'array',
of: [
type: 'reference',
to: [
type: 'tile'
]
]
So the tile type is deeply nested page.blocks.tiles.tile.
How can I query page and fill in the tile references in the same query?
tile
page.blocks.tiles.tile
page
tile
1 Answer
1
Since tile is a reference, you need the dereferencing operator, rather than the dot operator. This should work: page.blocks.tiles->.
page.blocks.tiles->
*[_type == "page"]_id, name, route, blocks, blocks.tiles->tile}
Whenever you build complex chains or arbitrary queries like that, you probably have to name that field. Something like this:
*[_type == "page"]_id, name, route, "blocks": blocks.tiles->} (Remove the tile after the dereferencing operator, this would extract the field "tile" from whatever is referred to, while just adding the arrow expands whatever is pointed to by the reference)– svale
Jan 16 at 14:38
*[_type == "page"]_id, name, route, "blocks": blocks.tiles->}
tile
Thanks! That worked!
– Slem
Jan 18 at 12:17
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How would you structure the query? This is what I've tried:
*[_type == "page"]_id, name, route, blocks, blocks.tiles->tile}– Slem
Jan 16 at 12:51