GROQ references() not working with parent document ID in projection
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.currentThe ^ 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.currentYou'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.currentUnderstanding the symbols:
@= current element (useful in array filters likearray[@.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 thread6 replies
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.