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]
        "variant": variants[]->|[variantId in $variantIds][0]{
        "image": images|[defaultImage == true][0]{
          "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]->{
which does the trick
May 16, 2021, 10:06 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the 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?

Categorized in