
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeI can see the issue you're encountering. When you try to use references(subjectTag->_id) inside a projection, the dereferencing operation (->) happens in a different context, and you can't directly use it within the references() function like that.
The problem is that subjectTag->_id tries to dereference in the context of the outer document, but inside the nested query *[_type=='subject' && references(...)], you need the actual ID value, not a path expression.
Here are two solutions:
_type == 'contentSubjectTag' => {
_type,
"title": subjectTag->title,
"text": subjectTag->text,
"subjectTagId": subjectTag->_id,
"subjects": *[_type=='subject' && references(^.subjectTagId)]{
title,
externalLink,
},
},By first storing subjectTag->_id as "subjectTagId", you create a field in the projected object. Then you can reference it using the parent operator (^) in your nested query.
If your subjectTag field is a reference (contains {_ref: "..."} structure), you can use the reference value directly:
_type == 'contentSubjectTag' => {
_type,
"title": subjectTag->title,
"text": subjectTag->text,
"subjects": *[_type=='subject' && references(^.subjectTag._ref)]{
title,
externalLink,
},
},Here, ^.subjectTag._ref accesses the _ref property of the reference object in the parent document.
The key insight is that the parent operator ^ lets you access fields from the outer document context when you're inside a nested query. You need to either:
^ (Solution 1)_ref value from the reference object using ^ (Solution 2)Both approaches work around the limitation that you can't perform dereferencing operations directly inside the references() function parameter. The parent operator ^ is essential here because it allows your nested query to reach back up to the parent document's context and grab the value you need.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store