How to dereference all fields in GROQ without listing them explicitly?

9 replies
Last updated: Dec 2, 2025
Is there a way to get everything from a reference without typing everything out? For example I have a "content" that is a reference to a "service" and a "post" document that includes a title, description, etc etc. However, in order to get that in my groq query I can just do
export const categoryBySlug = groq`*[_type == "category" && slug.current == $slug]{
  title,
  "slug": slug.current,
  description,
  "excerpt": array::join(string::split((pt::text(description)), "")[0..160], "") + "...",
  content
}[0]`
I have to do

export const categoryBySlug = groq`*[_type == "category" && slug.current == $slug]{
  title,
  "slug": slug.current,
  description,
  "excerpt": array::join(string::split((pt::text(description)), "")[0..160], "") + "...",
  "content": content[]->{
    title,
    description,
    audience,
    website,
    phone,
    address
  }
}[0]`
Or at least I
think I do? However, the issue arises where if a new field is added to the reference I now have to update my query. I guess one could argue that I would have to update the code to show the new field... but it would be nice to not have to write out every single field. This would as both documents it references don't have the same fields.
AI Update

Good news! You don't need to list every field when dereferencing references. The -> operator by itself automatically fetches all fields from the referenced document.

Your simplified query can just be:

export const categoryBySlug = groq`*[_type == "category" && slug.current == $slug]{
  title,
  "slug": slug.current,
  description,
  "excerpt": array::join(string::split((pt::text(description)), "")[0..160], "") + "...",
  "content": content[]->
}[0]`

The reference access operator (->) automatically resolves the reference and returns all fields from the referenced documents. When you add new fields to your "service" or "post" documents, they'll automatically appear in your query results without any changes needed.

What if I want to be selective?

If you later decide you do want to limit which fields are returned (for performance or clarity), you can add a projection:

"content": content[]->{
  title,
  description,
  website
}

But unless you have a specific reason to limit fields, just using content[]-> gives you everything and is the most maintainable approach.

A note about the ellipsis operator (...)

You might see the ellipsis operator (...) used in GROQ projections, but it serves a different purpose. It's used to include all fields from the current document being iterated, not for dereferencing. For example, *[_type == 'movie']{...} returns all fields from movie documents. It's not needed when dereferencing references—the -> operator already handles that for you.

Show original thread
9 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?