Go Behind the Experience to see how Tecovas brings the West to life with Sanity 🤠 July 18th

GROQ Query - Is it Possible to Filter Variants?

1 replies
Last updated: May 16, 2021
Hi, I am hoping that someone can help me with understanding this. I have a groq query that works in v1 of the API, but nothing subsequent (e.g. 2021-03-25). I have a document type "product" that has an array of references. I am trying to filter that array so that the referenced variants are in a parameter, like below:
*[_type == "product" && _id in $ids]
      {
        price,
        "variant": variants[]->|[variantId in $variantIds][0]{
        	packageSize, 
        	flavour,
          variantId,
      	},
        "image": images|[defaultImage == true][0]{
          altText,
          "url": asset->url
        }
      }
This is giving an error now at the
[variantId in $variantIds]
that an object or function is expected. I have tried various other ways of structuring this and the best I can get is returning an array of objects where only one has content... any help appreciated
May 13, 2021, 5:36 AM
Ok, after the weekend to form a new mind, I have come up with this:
"variant": variants[@->variantId in $variantIds]->{
          packageSize,
        	flavour,
        	variantId
        }
which does the trick
May 16, 2021, 10:06 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?