How to access parent field from reference context using @ in GROQ?

4 replies
Last updated: Nov 9, 2023
Hi!In groq, while resolving a reference, I want to read a field outside of the reference context. From the 'Query Cheat Sheet - GROQ' I understand the '@' to let me do this, but I am not able to make it work. Does the '@' not work as documented, or am I misunderstanding something?

In the example below I am trying to read the 'isSignLanguage'-field from inside 'rightsList'. See image from Vision:
AI Update

Based on your question, it sounds like you're looking for the parent operator (^) rather than the @ operator. This is a common point of confusion in GROQ!

The @ operator represents the current scope - whatever object you're currently working with. It doesn't let you access fields from outside the current context. The ^ operator is what you need to access parent scope values.

Here's how to read the isSignLanguage field from outside your rightsList reference context:

{
  ...,
  "rightsList": rightsList[]-> {
    ...,
    "parentIsSignLanguage": ^.^.isSignLanguage
  }
}

Let me break down what's happening:

  • First ^ - moves up from the dereferenced document back to the array element context
  • Second ^ - moves up from the array element to your parent document where isSignLanguage lives

The number of ^ operators you need depends on how many scope levels deep you are. In your case with rightsList[]->, you're two levels deep (array projection and reference dereferencing), so you need ^.^.

Important note: There was a bug with the parent operator that was fixed in API version 2021-03-25. If you're using an older API version, the parent operator might not work correctly. Make sure you're using a recent API version.

The confusion between @ and ^ is understandable since both use special symbols, but remember:

  • @ = current scope (what you're looking at right now)
  • ^ = parent scope (go up one level)

You can chain multiple ^ operators like ^.^.^.fieldName to traverse multiple levels up in your query hierarchy.

Show original thread
4 replies
What do you get if you do

rightsList[]->{
...
}
All the content of the document referred to in rightsList:
@
returns the current scope. I think you may want
^
to access the parent scope (you may need to chain two in your case).
Thanks!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?