Discussion on how to access grandparent object in Sanity.io using GROQ
23 replies
Last updated: Oct 29, 2021
S
Hi for some reasons I'm trying to get access to grandparent object but
Except the
^.^is not working 😞
*[_type == 'post' && slug.current == $slug]{ children[]->{ 'post': ^.^ } }
@and
^operators, I can't find anything that could do the job 😕
Oct 29, 2021, 4:54 PM
A
First, which API version are you using? You’ll need
Secondly, with that newer version,
v2021-03-25or later to get the right behaviour.
Secondly, with that newer version,
^.along should give you what you want, i.e. the parent scope.
Oct 29, 2021, 4:57 PM
S
I'm using the
2021-10-04API version.
Oct 29, 2021, 4:58 PM
A
First, which API version are you using? You’ll need
Secondly, with that newer version,
v2021-03-25or later to get the right behaviour.
Secondly, with that newer version,
^.along should give you what you want, i.e. the parent scope.
Oct 29, 2021, 4:57 PM
S
I'm using the
2021-10-04API version.
Oct 29, 2021, 4:58 PM
A
Ok, good. Have you triedÂ
^instead of
^.^?
Oct 29, 2021, 5:05 PM
S
Yep, I tried a lot of things. In every scenario, it's not working. Even something like
^.^.^.^
Oct 29, 2021, 5:06 PM
S
Testing on Vision Playground and my code it's not working.But strangely on
https://groq.dev , it is
https://groq.dev , it is
Oct 29, 2021, 5:09 PM
A
That’s because groq.dev uses a different JS implementation of GROQ
Oct 29, 2021, 5:09 PM
S
// JSON [ { "title": "parent", "_type": "post", "children": [ { "_key": "5678", "_type": "reference", "_ref": "1234" } ] }, { "_id": "1234", "_type": "child", "title": "Child" } ] // request *[_type == 'post']{ ..., children[]->{ 'post': ^ } } // result [ { "title": "parent", "_type": "post", "children": [ { "post": { "title": "parent", "_type": "post", "children": [ { "_key": "5678", "_type": "reference", "_ref": "1234" } ] } } ] } ]
Oct 29, 2021, 5:09 PM
S
Yeah I was expecting something like that 😕
Oct 29, 2021, 5:10 PM
A
This works fine for me (with just
^). Are you sure you’re using
v2021-10-04?
Oct 29, 2021, 5:12 PM
S
Ok sorry about this, this is totally working in my code but not on Vision Playground (I don't know how to check the version of the playground).
Oct 29, 2021, 5:12 PM
S
Sorry for the stupid question, long day for me
Oct 29, 2021, 5:12 PM
A
Do you mean the Vision plugin inside the Studio?
Oct 29, 2021, 5:12 PM
S
Yep
Oct 29, 2021, 5:12 PM
A
If so, it might have a dropdown. It’s been a while since I used it.
Oct 29, 2021, 5:13 PM
A
Studio projects still default to v1, AFAIK. You can specify the version in
sanity.json.
Oct 29, 2021, 5:13 PM
A
(I’m not a Studio dev, so I’m not 100% sure here)
Oct 29, 2021, 5:13 PM
S
Oh damn !
Oct 29, 2021, 5:13 PM
S
Thanks for all.Yep you can choose the API version in the Studio, I wasn't looking at the right place ^^'
Oct 29, 2021, 5:14 PM
A
🙂
Oct 29, 2021, 5:15 PM
A
Glad you got it working!
Oct 29, 2021, 5:18 PM
S
Haha me too, thanks again
Oct 29, 2021, 5:18 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.