
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes! The select() function is exactly what you're looking for. Your current approach of assigning the same "title" key twice works because GROQ overwrites the first value with the second, but you're right that it's not ideal and could lead to unexpected behavior.
Here's the cleaner way using select():
*[_type == "story" && !(_id in path('drafts.**'))] | order(publicationDate desc) {
"references": references[] {
"title": select(
reference->_type == "artist" => reference->name,
reference->_type == "otherType" => reference->title
)
}
}The select() function works like a switch statement - it evaluates conditions in order and returns the value for the first condition that matches. You can add as many conditions as you need for different document types, and optionally include a fallback value at the end:
"title": select(
reference->_type == "artist" => reference->name,
reference->_type == "venue" => reference->title,
reference->_type == "event" => reference->eventName,
"Untitled" // fallback if none match
)This approach is more explicit, easier to maintain, and won't have any unexpected behavior when you scale to more complex queries. The select() function also short-circuits (stops evaluating once it finds a match), making it more efficient than your current approach.
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