GROQ: Access root document title from custom object field

6 replies
Last updated: Jan 27, 2026
What should my GROQ be to get the root document's title from a custom object's field? I feel like I should be able to just use
[@].title
but that's just returning null.
AI Update

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:

[@->].title

Or more commonly written as:

@->.title

The @ 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:

  1. The field actually contains a reference (has a _ref property)
  2. The referenced document exists and has a title field
  3. You have the correct field path to the reference

Hope this helps!

Show original thread
6 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.

Was this answer helpful?