Querying nested attributes in Sanity.io model and upgrading to the latest API version.
15 replies
Last updated: May 31, 2021
J
Hi! I have the following model:• A
filter to work. I've tried all combinations (
Is it not possible to filter by a sub-sub-attribute, i.e.
See the attached screenshot for my current workaround using `slugString`:
companywith an array of
teamobjects (no references, only nested objects) called
companyTeams[]• Each
companyand
teamhas their own unique
slugI'm trying to write a query where I filter by both
company.slug.currentand
team.slug.current, but I'm not able to get the
$teamSlug in companyTeams[].slug[].current[]
slug[].current,
slug.current[],
slug[].current[]), and if I create a new string attribute for the
teamobjects called
slugString, I get the following filter to work as intended:
$teamSlug in companyTeams[].slugString[]
Is it not possible to filter by a sub-sub-attribute, i.e.
slug.current, with the
inoperator?
See the attached screenshot for my current workaround using `slugString`:
May 31, 2021, 9:35 AM
A
slugis an object like
{"current": "foo"}? If so, you should be able to use:
$teamSlug in companyTeams[].slug.current
May 31, 2021, 9:42 AM
A
We realize that the semantics of the
[]operator can be confusing! Think of
[]as being “map over”. So
a[].bis equivalent to JavaScript
a.map(val => val.b).
May 31, 2021, 9:43 AM
A
The
[]operator becomes a flattening operator if added to the end of an array, which means that something like
parents[].childrenreturns an array of arrays (i.e. each parent’s children as a nested array), but
parents[].children[]returns a flat array of all the children.
May 31, 2021, 9:44 AM
J
Thanks for the clarification! However,
$teamSlug in companyTeams[].slug.currentstill returns
{}
May 31, 2021, 9:46 AM
J
I'm using the v1 API version btw
May 31, 2021, 9:47 AM
A
You should be using
v2021-03-25. The array handling is very buggy in v1.
May 31, 2021, 9:48 AM
A
The new version also has much better performance.
May 31, 2021, 9:48 AM
J
Ahh, I wasn't aware of that. Will try the new version now. Any breaking changes from v1 to
v2021-03-25?
May 31, 2021, 9:52 AM
A
May 31, 2021, 9:59 AM
A
Scroll down to “Breaking changes from v1 to v2021-03-25”
May 31, 2021, 9:59 AM
A
The breaking changes are mostly fixes 🙂
May 31, 2021, 10:00 AM
J
Perfect, thanks for the help!
May 31, 2021, 10:16 AM
A
👍
May 31, 2021, 10:27 AM
J
Everything worked as expected now – and super-easy upgrade –
nullvs.
{}return value was the only breaking change for us
May 31, 2021, 10:31 AM
A
Great!
May 31, 2021, 10:35 AM
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.