GraphQL: How do I get all product variants by product ID for each product?

RaviAmbasana
New Member
1 0 0

Hi, I am trying to get product variants for a product using the product ID. I am querying the `productVariants` and my query looks like this:

{
  productVariants(first:10 ) {
    edges {
      node {
        id
        product {
          id
        }
      }
    }
  }
}

I want to get only the product variants that match a particular product id. How can I do that?

Replies 3 (3)
VivekH
Excursionist
12 1 12

Hey @RaviAmbasana ,

You can query all product variants of a product using this -

 

{
  product(id: "gid://shopify/Product/1234") {
    variants(first: 15) {
      edges {
        node {
          id
          sku
        }
      }
    }
  }
}

  Hope this helps.

Veesy
Shopify Partner
7 0 1

It's only first 15, is there a way to remove the "first" filter ?

DenisSabolotni
Shopify Partner
12 1 14

Unfortunately not. Shopify did that on purpose to reduce the "cost" of queries.

You could implement "paging", to get all results through multiple small requests, instead of a big one, that gets everything for you:

 

https://shopify.dev/api/usage/pagination-graphql