Best approach for querying by SKU in a product catalog using Sanity.io and GraphQL.
Based on the information about product catalog management in Sanity, I'd recommend adding SKU directly to your individual product documents rather than creating a separate document type. Here's why:
Direct SKU approach (recommended):
- Store the SKU field directly on each product document type
- Query by SKU using GROQ filters like
*[_type == "product" && sku == "ABC123"][0] - This is the standard pattern used in e-commerce implementations with Sanity
Why this works better:
- Simpler queries: You can directly query any document type by SKU without needing to traverse references
- Better performance: Direct field queries are faster than reference lookups
- Easier maintenance: No need to keep separate SKU documents in sync
- Flexibility: Each document type can have its own SKU while still being queryable
If you need to query across multiple document types:
You can use a GROQ query that searches multiple types at once:
*[_type in ["productType1", "productType2", "productType3"] && sku == $sku][0]Or if you want to find all documents with a specific SKU regardless of type:
*[defined(sku) && sku == $sku]When you might consider a separate SKU document:
The separate document approach only makes sense if:
- You need complex SKU-specific metadata that doesn't belong on product documents
- You're managing SKUs that can point to different product types over time
- You need a central registry for SKU allocation/validation
But for most product catalogs, the direct approach is cleaner and more maintainable. As mentioned in the structured content patterns for e-commerce guide, storing SKUs directly on product documents (often within variant arrays) is the standard pattern in e-commerce implementations.
Sanity – Build the way you think, not the way your CMS thinks
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.