How to Fetch All Images References in a GROQ Request
Yes! You can use the ellipsis operator (...) in GROQ to include all fields from an object without specifying each one individually. This is perfect for fetching image asset data.
Here's how you can simplify your query:
*[_id == "spring"].content {
place_banner_1 {
alt,
photo {
asset-> {
...,
"width": metadata.dimensions.width,
"height": metadata.dimensions.height,
"ratio": metadata.dimensions.aspectRatio
}
}
},
place_banner_2 {
alt,
photo {
asset-> {
...
}
}
},
place_banner_3 {
alt,
photo {
asset-> {
...
}
}
}
}The ellipsis operator (...) expands to include all fields from the dereferenced asset document, giving you the url, metadata, and all other asset properties automatically.
Important tip about operator placement: The ellipsis is always evaluated first, regardless of where it appears in the projection. This means if you want to override or add custom fields, you should place the ellipsis first:
asset-> {
..., // Place first to avoid it overriding your custom fields
"width": metadata.dimensions.width,
"height": metadata.dimensions.height,
"ratio": metadata.dimensions.aspectRatio
}If you place ... after your custom fields, it won't override them (since it's evaluated first anyway), but placing it first makes the intent clearer and is the recommended pattern.
You can also use this pattern to fetch all images used by a document by querying for references to sanity.imageAsset:
*[_id == "spring"] {
"allImages": *[_type == "sanity.imageAsset" && references(^._id)]
}This gives you all image assets that are referenced anywhere in your document, which can be useful for understanding asset usage across your content.
Show original thread11 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.