👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Best approach for querying by SKU in a product catalog using Sanity.io and GraphQL.

5 replies
Last updated: Aug 20, 2021
Hi all. I have multiple document types that together form a product catalog. I will need an easy way to query by SKU, and I am not sure what the best way to proceed. Should I add SKU to individual documents and then use some aggregate query for it? Or should I create a new document type that will hold the unique global SKU and reference the product document types?
Aug 19, 2021, 12:12 PM
Hey Ismayil! To clarify, do each of these product documents have their own SKU or do you want multiple products under one SKU?If they are individual SKUs, I suggest you add an SKU field to each document, then you can use GROQ to query and stitch together the information that you need.
Aug 19, 2021, 5:33 PM
I am not entirely sold on GROQ, as it's some custom technology that no IDE supports, and it doesn't provide any type safety. If we were to use GraphQL what would the better approach be?
Aug 20, 2021, 5:53 AM
Each product has it's own SKU, so a catalog is just a union of multiple document types.
Aug 20, 2021, 5:54 AM
If you’re not sold on GROQ, you can start learning about how to use our GraphQL API here .
As for your structured content approach, I would say that each individual product should be its own document with its own SKU field. Then, to group products into various categories, create a category document that contains an array of references to the relevant product documents. It may be helpful to take a look at these
e-commerce starters to get some ideas on best practices.
Aug 20, 2021, 3:03 PM
If you’re not sold on GROQ, you can start learning about how to use our GraphQL API here .
As for your structured content approach, I would say that each individual product should be its own document with its own SKU field. Then, to group products into various categories, create a category document that contains an array of references to the relevant product documents. It may be helpful to take a look at these
e-commerce starters to get some ideas on best practices.
Aug 20, 2021, 3:03 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?