Querying an array of references and getting both the resolved reference and the _key field.
Great question! Yes, you can absolutely get both the resolved reference data and the _key field. The trick is to use GROQ's object projection syntax to explicitly select the fields you want.
Instead of just products[]->, you need to construct a new object that includes both the _key from the reference object and the dereferenced document:
products[] {
_key,
"item": @->
}Or if you want to spread all the fields from the referenced document directly alongside _key:
products[] {
_key,
...@->
}The @ symbol refers to the current element in the array, and @-> dereferences it. The spread operator ... pulls all fields from the dereferenced document into your result object.
If you only need specific fields from the referenced document, you can be more selective:
products[] {
_key,
"productName": @->name,
"productPrice": @->price,
"productSlug": @->slug.current
}This pattern is covered in the GROQ Query Cheat Sheet and there's also a helpful community answer thread about keeping the _key property when resolving references.
The key insight is that when you use products[]->, you're replacing each reference object with the dereferenced document, which is why you lose the _key. By using object projection with {}, you maintain control over exactly what fields end up in your result.
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.