
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeBased on your description, you're dealing with nested references in GROQ, and you're on the right track! The -> operator does indeed dereference references, and you can chain multiple dereferences to follow nested references as deep as you need.
For multi-layer dereferencing, you can simply chain the -> operators. Here's how it works:
*[_type == "typeA" && name == "yourName"]{
'typeB': typeBDocuments[].typeBDocument->{
// Fields from type B document
title,
someField,
// Now dereference nested references inside type B
'nestedRef': nestedReference->
},
'typeC': typeCDocuments[].typeCDocument->{
// Fields from type C document
name,
// Dereference nested references inside type C
'nestedRef': anotherReference->{
// You can even go deeper
field1,
field2,
'deeperRef': yetAnotherReference->
}
}
}The key points:
-> multiple times to follow references as deep as neededarray[]-> automatically maps over the array and dereferences each item{}, explicitly list which fields you want from the dereferenced documentIf you want to be more selective about which fields come back at each level, you can be explicit:
*[_type == "typeA" && name == "yourName"]{
name,
'typeBData': typeBDocuments[].typeBDocument->{
_id,
title,
description,
'relatedItem': relatedItemRef->{
_id,
name,
imageUrl
}
}
}This approach is covered in the projections documentation, which explains how projections let you follow references and build custom data structures.
The alternative approach you mentioned—querying type B & C documents separately—would be less efficient since you'd need multiple queries and then manually match them up in your application code. The nested dereferencing approach keeps everything in a single query and returns the exact shape you need.
If you're still having trouble with the syntax, feel free to share your actual query and schema structure, and the community can help you format it more specifically!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store