🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Querying for products that have a variant referencing a specific attribute in a Shopify + Sanity schema.

9 replies
Last updated: Jul 15, 2022
Hello 👋 . I'm using the sanity + shopify reference schema from here . At least close enough to it w.r.t. the objects and references for "sanity" products and variants vs. objects for the shopify products and variants. I've added an array of references to
that point to some other new document type. I want to use the
function to find all `product`s that have a variant that reference a particular document of that type. I cannot figure out how to drill down in GROQ from a
*[_type == 'product']
thru the
object which references a
which ultimately has the reference to the document I'm considering. Thanks for any help.
Jul 13, 2022, 9:26 PM
*[_type == 'product' && _id == 'shopifyProduct-7429472551073']{
  "title": shop.title,
  "attributes": *[_type == 'attribute_color'],
  "attributes_real": attributes
Jul 13, 2022, 9:27 PM
"result":[1 item
0:{4 items
"attributes":[2 items
0:{8 items
"name":"Apparel Color"
1:{7 items
"name":"Lure Color"
"attributes_real":[1 item
0:{3 items
"attribute":{2 items
but I'm getting all
in attributes. I just want the
here. Ultimately I want to remove the top level id filter and get all the documents that have a variant that reference
attribute. Or generally any other attribute we've defined.
Jul 13, 2022, 9:30 PM
Hi Phil! 👋 I’m hoping you can help me clarify if this is the core of your question: “I want to … find all `product`s that have a variant that reference a particular document of that type.”
Jul 14, 2022, 4:04 PM
Hi Geoff. I'm starting from this example in the GROQ cheat sheet
// Movies which reference ancient people
*[_type=="movie" && references(*[_type=="person" && age > 99]._id)]{title}
However my
movie (product) does not reference a person (attribute) directly. I'm my schema, product owns an object shopifyProduct that has an array of references to productVariant which has an array of references to attribute. I want to find all the product documents that, thru this round about model, ultimately have a productVariant which has a reference to a particular attribute in its array of attribute references.
Jul 14, 2022, 8:22 PM
Great, thank you. When you say “_product_ owns an object shopifyProduct,” can I confirm that means
is an object in the schema (it’s just not showing because it wasn’t included in the projection)?
Jul 14, 2022, 8:29 PM
yes, its defined as an object in the schema. Full disclosure, we started from your reference schema for the Sanity connect app w/ Shopify. Then added a new document type, attribute. The productVariant document has an array of references to type of attribute.
Jul 14, 2022, 8:31 PM
Perfect, thank you. I’m not sure if this’ll work first try, as there are places where we might have structured things differently, but if you want to get a
with an array of references that has an array of references containing a title of ‘Apparel Color’, it might look something like this (bear in mind that arrays need names too, so I used

*[_type == 'product' && 'Apparel Color' in shopifyProduct.myArray[]->myArray[]->title]
Jul 15, 2022, 12:12 AM
🔥 🙏 I didn't think to kind of invert the lookup. Many thanks. This was the working query BTW:
*[_type == 'product' && 'apparel_color' in store.variants[]->attributes[].attribute->key]
Jul 15, 2022, 12:20 PM
Excellent! Glad you got it working!
Jul 15, 2022, 3:34 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?