GROQ pipes with tasty, pricy candy bars (ssr)

Written by Lo Etheridge, Kapehe

Missing Image!

Kap and I decided to dive into the e-commerce sample data set so that we could query delicious and luxurious candy bars! The GROQ pipe| operator allows you to grab the data from the left-hand side of the array and pass it over to the right-side pipeline component for more processing. In our case, we are passing data over to the order function order() to sort products by title and price.

From our test studio, loaded with candy bar product data (shoutout to Wasabi Kit Kats, our favorite!) we made the following query:

*[_type == "product"] | order(
	defaultProductVariant.price desc, title asc)[0...3]{
	  title,
	  "luxuryItems" : defaultProductVariant{
		  price
	}
}

This query filters on the document type product and pulls out the title and price only using a projection. Notice the defaultProductVariant.price, what's happening there? Did a cyborg named defaultProductVariant capture all of the candy bar prices?!? It sure seems like it, but really price is a field inside of the defaultProductVariant object, so it must remain attached to the price or any other field that is inside of it.

The GROQ pipe | operator grabs this data, thanks to the filter for its cooperation and hard work, and passes the data to the order function. The order function sorts the data by the parameters Kap and I set. First, the product data is sorted by price in descending order, and then it is sorted again by title in ascending order. We are also slicing [0...3] the data and using an alias luxuryItems in this query because we only want to return the 3 most expensive products as our luxury candy bars.

If you missed the stream, you can watch Kap & Lauren learn GROQ - Part 4 on YouTube. Follow us on Twitch and tune in for another episode of Kap & Lauren learn GROQ, we hope to see you there!