Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

Discussion on how to access grandparent object in Sanity.io using GROQ

23 replies
Last updated: Oct 29, 2021
Hi for some reasons I'm trying to get access to grandparent object but
^.^
is not working 😞
*[_type == 'post' && slug.current == $slug]{
  children[]->{
    'post': ^.^
  }
}
Except the
@
and
^
operators, I can't find anything that could do the job 😕
Oct 29, 2021, 4:54 PM
First, which API version are you using? You’ll need
v2021-03-25
or 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
I'm using the
2021-10-04
API version.
Oct 29, 2021, 4:58 PM
First, which API version are you using? You’ll need
v2021-03-25
or 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
I'm using the
2021-10-04
API version.
Oct 29, 2021, 4:58 PM
Ok, good. Have you tried 
^
instead of
^.^
?
Oct 29, 2021, 5:05 PM
Yep, I tried a lot of things. In every scenario, it's not working. Even something like
^.^.^.^
Oct 29, 2021, 5:06 PM
Testing on Vision Playground and my code it's not working.But strangely on
https://groq.dev , it is
Oct 29, 2021, 5:09 PM
That’s because groq.dev uses a different JS implementation of GROQ
Oct 29, 2021, 5:09 PM
// 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
Yeah I was expecting something like that 😕
Oct 29, 2021, 5:10 PM
This works fine for me (with just
^
). Are you sure you’re using
v2021-10-04
?
Oct 29, 2021, 5:12 PM
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
Sorry for the stupid question, long day for me
Oct 29, 2021, 5:12 PM
Do you mean the Vision plugin inside the Studio?
Oct 29, 2021, 5:12 PM
Yep
Oct 29, 2021, 5:12 PM
If so, it might have a dropdown. It’s been a while since I used it.
Oct 29, 2021, 5:13 PM
Studio projects still default to v1, AFAIK. You can specify the version in
sanity.json
.
Oct 29, 2021, 5:13 PM
(I’m not a Studio dev, so I’m not 100% sure here)
Oct 29, 2021, 5:13 PM
Oh damn !
Oct 29, 2021, 5:13 PM
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
🙂
Oct 29, 2021, 5:15 PM
Glad you got it working!
Oct 29, 2021, 5:18 PM
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.

Was this answer helpful?