Joel Smith
Joel Smith

Reputation: 55

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

I want to fetch all variants of a product suing shopify graphql api, but it seems like we have to define the number of variants or any other thing we want by using first 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
                        
                      }
                    }
                  }
                }
            `,
        }

    });

Upvotes: 1

Views: 4435

Answers (1)

drip
drip

Reputation: 12943

A product can't have more than 100 variants, that is a set limit by Shopify.

So in order to get ALL variants you just have to say first: 100 as long as your request cost doesn't exceed the allowed bucket cost for the query you will get them.

As for what you are asking, there is no way to get all variants without using first or last, this is a requirement when you are trying to get an array of edges.

Upvotes: 1

Related Questions