πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Projection of a reference item in a deeplinked optional array.

By Surjith S M

It will expand the reference (projection) which is deeply linked in an array and optional using select funtion.

Groq Query to expand `plan` reference which optional only in _type == "pricing" inside `content` array

// Using (select) 
// https://www.sanity.io/docs/groq-functions#298e89c3c8d9

*[_type == "page" && slug.current == $slug][0] {
	..., 
	content[] {
  	...,
     		select( _type == "pricing" ) => {
     				plan[]->  
				},
		}
}  


// Short Form
// Also works without select()

*[_type == "page" && slug.current == $slug][0] {
    ..., 
    content[] {
  		...,
     		_type == "pricing" => {
     			plan[]->  
				},
		}
}  


// Params
{
 "slug": "slugname"
}

Imagine you have a document with deeply linked arrays. Inside that, you have a reference that might or might not appear. You have to expand that reference using projection -> but the problem is it doesn't appear on every array. it's completely optional.

This snippet will expand the reference which is deeply linked in an array and optional using the select function.

Contributor

Other schemas by author