Re: How to fetch all variants of a product from shopify graphql api?

How to fetch all variants of a product from shopify graphql api?

Leith
Shopify Partner
12 1 3

I want to fetch all variants of a product using shopify graphql api, but it seems like we have to define the number of variants or any other thing we want by using first an argument after the query. I want to get all variants of the product instead of defining a number of variants, how can this be possible?

 

const myproducts = await axios({
        url: `https://${req.session.shop}/admin/api/2022-01/graphql.json`,
        method: 'post',
        headers: {
          "X-Shopify-Access-Token": req.session.token,
        },
        data: {
            query: `
                {
                  product(id: "gid://shopify/Product/6829796196490") {
                    title
                    variants(first: 5) {
                      edges {
                        cursor
                        node {
                          selectedOptions {
                            name
                            value
                          }
                          media(first: 5) {
                            edges {
                              node {
                                alt
                                mediaContentType
                                status
                                __typename
                                ... on MediaImage {
                                  id
                                  preview {
                                    image {
                                      originalSrc
                                    }
                                  }
                                  __typename
                                }
                              }
                            }
                          }
                        }
                      }

                      pageInfo {
                        hasNextPage
                        
                      }
                    }
                  }
                }
            `,
        }

    });
Replies 2 (2)

JohnHa
Shopify Partner
108 1 20

Hi @Leith 

I think you can use REST API to get all variants for a product, it's easier than graphql API.

In Postman, you create a new get with this endpoint.

https://your-development-store.myshopify.com/admin/api/2021-10/products/{Product_ID}/variants.json

Then click send

The API will return all variants information for a product.

You can read our blog: Shopify API - Retrieve all variants for a product using Postman 

Or watch the video for more overview: Shopify API | Lesson #23: Retrieve all variants for a product.

I hope this helps. Feel free to contact us if you need further assistance.

get all variants for a product.png

 

banned
Leith
Shopify Partner
12 1 3

Thank you, I am now using rest API, but what about in the future I want to retrieve all products using graphql