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?
AI Update

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:

  1. Simpler queries: You can directly query any document type by SKU without needing to traverse references
  2. Better performance: Direct field queries are faster than reference lookups
  3. Easier maintenance: No need to keep separate SKU documents in sync
  4. 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.

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.
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?
Each product has it's own SKU, so a catalog is just a union of multiple document types.
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.
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.

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.

Was this answer helpful?