GROQ references() not working with parent document ID in projection

6 replies
Last updated: Jan 11, 2023
Hello all, does anyone know why
*[_type == 'hos_chapter' && references(@->_id)][0].slug.current
wouldn't work? I've tried
references(^._id)
too but no joy. Full code...
AI Update

I can see you're having trouble with GROQ reference syntax! The issue is likely with how you're using the scope operators. Let me break down what's happening:

The problem with references(@->_id) is that @ refers to the current element in the context where it's used, and you're trying to dereference it with -> before passing it to references(). The references() function expects a document ID string, not a dereferenced object.

Here's what you should use instead:

If you're in a projection or subquery:

*[_type == 'hos_chapter' && references(^._id)][0].slug.current

The ^ operator refers to the parent scope - the document from the outer query context. This is the correct syntax when you're inside a nested query or projection and need to reference the "current" document from the parent level.

If you're at the root level:

*[_type == 'hos_chapter' && references("some-document-id")][0].slug.current

You'd pass the actual document ID as a string.

Common gotcha with drafts: If you're working with documents that might have draft versions, you may need to handle both the published and draft IDs:

*[_type == 'hos_chapter' && (references(^._id) || references("drafts." + ^._id))][0].slug.current

Understanding the symbols:

  • @ = current element (useful in array filters like array[@.active == true])
  • ^ = parent scope (the document from one level up in nested queries)
  • -> = dereference operator (follows a reference to fetch the full document)

Without seeing your full query context, references(^._id) is most likely what you need. If that's still not working, make sure you're actually inside a nested context where ^ has meaning - if you're at the root query level, you'd need to pass a specific ID string instead.

Show original thread
6 replies
"relHos": relHos[]{
      "id": @->_id,
      "title": @->title,
      "status": @->meta.status,
      "slug": @->slug.current,
      @->_type == 'hos' => {
        "chapter": *[_type == 'hos_chapter' && references(@->_id)][0].slug.current
      },
    },
chapter
is returning
null
Also, just to confirm, hardcoding a relevant id does return the chapter, so its something to do with the
@->
or
^.
that's causing the issue.
It’ll probably be more performant if you expand the reference on the object level:
relHos[]->{
  _id,
  title,
  "status": meta.status,
  _type == "hos" => {
    "chapter":  *[_type == "hos_chapter" && references(^._id)]
    
  }
}
I’d think in this case that
^._id
should work for you, however you may have to go up another level (
^.^._id
) because of the conditional.
Thanks
user M
- I did actually implement that technique afterwards, not sure why I had it like that initially. And thanks, all sorted and working now 😊
Maybe changed things around when you were debugging? I’ve done that before! 🙂

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?