GraphQl: Most efficient way to get selling plans from variant ids?

I need to get the selling plans of each variant from a variants ids array.

I noticed that the productVariants query doesn’t support querying by multiple ids (it only supports ranges).

Does this mean that I’ll need to do hundreds of single productVariant queries? Is there a more efficient way to achieve this?

Any insight is appreciated, thanks!

1 Like

Hey @Pridentt,

You can use a products query with multiple variant id’s as query filters.

Here’s an example; let me know if something like this would work for you:

query {
  products(first: 50, query: "variant_id:43828186972182 OR variant_id:39259275526166") {
    edges {
      node {
        id
        title
        sellingPlanGroups(first: 10) {
          edges {
            node {
              id
              name
              sellingPlans(first: 10) {
                edges {
                  node {
                    id
                    name
                    description
                  }
                }
              }
            }
          }
        }
        variants(first: 10) {
          edges {
            node {
              id
              title
            }
          }
        }
      }
    }
  }
}

Thank you @KyleG-Shopify, but unfortunately for some reason the variant_id query doesn’t seem to filter anything for me. It returns products completely unrelated to the variant ids.

Example (try with a variant id that exists in your store):

query {
  products(first: 50, query: "variant_id:49967849832724") {
    nodes {
      id
      title
      sellingPlanGroups(first: 10) {
        edges {
          node {
            id
            name
            sellingPlans(first: 10) {
              edges {
                node {
                  id
                  name
                }
              }
            }
          }
        }
      }
      variants(first: 10) {
        nodes {
          id
        }
      }
    }
  }
}

Maybe I’m doing something wrong?

Interesting. It’s working as expected when I test. To simplify, I run this query:

   
  query {
  products(first: 50, query: "variant_id:45126221365434 ") {
    nodes {
      id
      title      
      variants(first: 10) {
        nodes {
          id
          title
        }
      }
    }
  }
}

which returns:

{
  "data": {
    "products": {
      "nodes": [
        {
          "id": "gid://shopify/Product/8196122050746",
          "title": "Unicorn",
          "variants": {
            "nodes": [
              {
                "id": "gid://shopify/ProductVariant/45126221365434",
                "title": "Default Title"
              }
            ]
          }
        }
      ]
    }
  },

You’re right @KyleG-Shopify, the problem was I was using an older API version, now it’s working. I’ll investigate if the products query can be used for my use case to detect selling plans of each variant. Thank you!

1 Like