Solving live preview in Gatsby without $99/mo Gatsby Cloud
Unfortunately, GROQ does not support automatically resolving all references recursively without explicitly specifying the fields you want. This is a fundamental difference between GROQ and the GraphQL implementation in gatsby-source-sanity.
Why GROQ Requires Explicit Field Specification
When you use the reference operator (->) in GROQ, you must explicitly project which fields you want from the referenced document. Even when using the ellipsis operator (...) to include all fields, it only spreads fields at the current level—it doesn't automatically dereference nested references.
For example, this query resolves one level of references:
*[_id == $id][0]{
...,
author->,
categories[]->
}But if author itself contains references, those won't be automatically resolved. You'd need to explicitly nest them:
*[_id == $id][0]{
...,
author->{
...,
company->
},
categories[]->
}How Gatsby's GraphQL Differs
The gatsby-source-sanity plugin provides special _raw fields that can automatically resolve references with the resolveReferences argument:
{
_rawBody(resolveReferences: {maxDepth: 5})
}This functionality is specific to the Gatsby integration and not available in Sanity's native GraphQL API or GROQ. These _raw fields are generated by the Gatsby plugin and won't appear in Sanity's standard GraphQL playground.
Workarounds for Your Use Case
For Gatsby preview functionality that mirrors the GraphQL behavior, you have a few options:
Build dynamic GROQ queries: Introspect your schema to programmatically construct GROQ queries with all necessary reference resolutions. This is complex but possible if you know your content model structure.
Use GROQ with known depth: If you know the maximum reference depth in your content model, write GROQ queries that explicitly resolve to that depth with nested projections.
Fetch and resolve client-side: Query the document with
*[_id == $id][0], then recursively fetch referenced documents by examining_reffields and making additional queries.Use the GraphQL API for previews: Continue using Sanity's GraphQL API for preview functionality, which supports the same
_rawfield patterns as Gatsby (though you'll need to deploy your schema withsanity graphql deploy).
The fundamental limitation is that GROQ is designed to be explicit about data fetching for performance and predictability, while the Gatsby GraphQL integration adds automatic reference resolution as a convenience layer on top. There's no way to tell GROQ "resolve all references at all depths automatically" without specifying what those references are.
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.