GROQ: Resolve all references without declaring fields explicitly?

18 replies
Last updated: Jan 28, 2026
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:

  1. Project specific fields from the referenced document:
*[_type == "event"] {
  venue->{
    name,
    address
  }
}
  1. Use spread operators to get all fields at that level:
*[_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:

  • _raw<FieldName>: These fields contain the raw JSON data from Sanity
  • resolveReferences: When enabled with a maxDepth, this will automatically traverse and resolve references within the raw field data
  • maxDepth: Controls how many levels deep to resolve references (prevents infinite loops)

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:

  • Returns all data in its raw Sanity format
  • Automatically resolves references up to the specified depth
  • Doesn't require you to know the structure ahead of time
  • Mirrors what GraphQL's normal field resolution does

Important Caveats

  1. _raw fields are Gatsby-specific: They're generated by gatsby-source-sanity and won't work in Sanity's GraphQL playground
  2. Reference resolution is opt-in: You must explicitly add resolveReferences: {maxDepth: N} to each _raw field
  3. Performance consideration: Deep reference resolution can be expensive, so choose an appropriate maxDepth
  4. Top-level only: The documentation notes this is only done for "top-level nodes (documents)"

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

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?