Using GROQ queries for live preview in Gatsby without knowing fields
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:
- Project specific fields from the referenced document:
*[_type == "event"] {
venue->{
name,
address
}
}- 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 SanityresolveReferences: When enabled with amaxDepth, this will automatically traverse and resolve references within the raw field datamaxDepth: 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
_rawfields are Gatsby-specific: They're generated by gatsby-source-sanity and won't work in Sanity's GraphQL playground- Reference resolution is opt-in: You must explicitly add
resolveReferences: {maxDepth: N}to each_rawfield - Performance consideration: Deep reference resolution can be expensive, so choose an appropriate
maxDepth - 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.
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.