
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeWhen working with nested references in a Shopify + Sanity schema, the key is using GROQ's in operator combined with the reference access operator (->) to traverse through multiple levels of references.
Based on your schema structure where:
product documents have a store object (or similar)variants array with references to productVariant documentsproductVariant has an array field (like attributes) containing references to your custom attribute documentsThe solution is to use the in operator to check if a specific value exists anywhere in the dereferenced chain:
*[_type == 'product' && 'your-attribute-id' in store.variants[]->attributes[]->_id]Or if you're looking for a specific field value within the attribute:
*[_type == 'product' && 'apparel_color' in store.variants[]->attributes[].attribute->key]How this works:
store.variants[]-> dereferences all variant references in the arrayattributes[] accesses the attributes array in each dereferenced variant.attribute-> (if attributes contain references) or just -> dereferences each attribute referencekey, _id, etc.)in operator checks if your target value exists anywhere in the resulting flattened arrayGROQ automatically flattens nested arrays when you chain array accessors ([]), which makes this pattern work elegantly for deeply nested references.
You can learn more about the references() function and the reference access operator (->) in the GROQ documentation. While references() is great for direct references, the in operator with dereferencing is more flexible for these nested relationship queries.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store