GROQ: Access root document title from custom object field
Based on your question, it sounds like you're trying to dereference a reference field within a custom object, but [@].title isn't working. The issue is that @ by itself doesn't dereference - you need to use the reference access operator -> instead.
The correct syntax would be:
[@->].titleOr more commonly written as:
@->.titleThe @ symbol in GROQ represents the current element (useful in array mappings or projections), but to actually resolve a reference to get the referenced document's data, you need the -> operator.
Here's how it works: when you have a reference field, it's stored as {"_ref": "document-id"}. The reference access operator (->) performs an internal subquery to fetch the actual document with that _id and replace the reference with the full document data.
Example scenarios:
If you have a single reference field:
*[_type == "myDocument"] {
myReference->.title
}If you're already in a projection context using @:
*[_type == "myDocument"] {
"refTitle": @.myReference->.title
}If you're working with an array of references:
*[_type == "myDocument"] {
myReferences[]->title
}If you're dealing with mixed content (some references, some inline objects), you can use coalesce:
{
"title": coalesce(@->.title, @.title)
}If @->.title is still returning null, double-check that:
- The field actually contains a reference (has a
_refproperty) - The referenced document exists and has a
titlefield - You have the correct field path to the reference
Hope this helps!
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.