Joint session with Vercel: How to build intelligent storefronts (May 15th)

GROQ: Resolve all references without declaring fields explicitly?

18 repliesLast updated: Nov 29, 2025

Asking again, is it not possible for GROQ queries to resolve references without declaring the fields you want to return?
I need to query for a document by its ID, and return
all its fields – and resolve all its references – without knowing what they could be.
(The use case is creating a preview in Gatsby, so I need to mirror the GraphQL data, which does exactly this, but only at build time)
AI Update

Unfortunately, no, GROQ does not support automatically resolving all references without specifying fields. The dereference operator (->) requires you to explicitly indicate which fields you want from referenced documents.

Why This Limitation Exists

GROQ is designed to be explicit about data fetching. When you use the -> operator, you need to either:

*[_type == "event"] {
  venue->{
    name,
    address
  }
}
*[_type == "event"] {
  venue->{
    ...
  }
}

However, even with spread operators, you'd need to manually traverse nested references and specify fields at each level. There's no "resolve everything recursively" option in GROQ itself.

Your Gatsby Preview Use Case

For your specific Gatsby preview scenario, you're trying to mirror what GraphQL does automatically. The good news is that gatsby-source-sanity has a solution: the _raw fields with resolveReferences.

According to the gatsby-source-sanity documentation, the plugin generates special _raw fields that can automatically resolve references:

{
  allSanityProject {
    edges {
      node {
        _rawTasks(resolveReferences: {maxDepth: 5})
      }
    }
  }
}

The key options here:

Practical Solution for Your Preview

For your document-by-ID query with full reference resolution:

query PreviewQuery($id: String!) {
  sanityYourType(_id: {eq: $id}) {
    _id
    _type
    _rawBody(resolveReferences: {maxDepth: 10})
    _rawExcerpt(resolveReferences: {maxDepth: 10})
    # Add _raw versions of any other fields that might contain references
  }
}

This approach:

Important Caveats

Alternative: Multiple Queries

If _raw fields don't meet your needs, you'd need to construct multiple GROQ queries or use GROQ's projection syntax with explicit field listing—there's simply no automatic "give me everything with all references resolved" option in GROQ itself. The GROQ reference access operator always requires you to specify what you want from the referenced document.

Show original thread
18 replies

Was this answer helpful?

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.

Related contributions