Projections
In GROQ, projections are how we select the data we want returned.
The .project
method
.project(sub => ProjectionMap)
The sub
parameter is a strongly-typed GroqBuilder, so it knows the names and types of the fields in the current filters.
The ProjectionMap
is an object that maps keys to queries.
Example:
q.star.filterByType("product").project(sub => ({
imageUrl: sub.field("image").deref().field("url", q.string()),
slug: sub.field("slug.current", q.string()),
price: sub.field("price", q.number()),
}))
// Result GROQ:
// *[_type == "product"]{
// "imageUrl": image->url,
// "slug": slug.current,
// price,
// }