Solved

How to get all product variants metafields

tien-luckyfor
Tourist
6 0 0

Hi everyone, I'm new here..

So I've just research how to get all product metafields via grapql. And my new task is get all product variants metafields.

I trying to replace "product" in these commands to "variant" but still doesn't work anymore.

Can you help me to give great solution, Thanks for read my question.

// get all product metafields

query allProductsMetafields($cursor: String) {
  products(first: 30, after: $cursor) {
    edges {
      cursor
      node {
        legacyResourceId
        metafields(first: 30) {
          edges {
            node {
              namespace
              value
              key
            }
          }
        }
      }
      cursor
    }
    pageInfo {
      hasNextPage
      hasPreviousPage
    }
  }
}

 

Accepted Solutions (2)

srdjan
Shopify Staff (Retired)
7 1 2

This is an accepted solution.

An alternative to @Gregarican's solution, which is perfectly fine, is to query product variants from the root level.

{
  productVariants(first: 10) {
    edges {
      node {
        metafields(first: 10) {
          edges {
            node {
              namespace
              key
              value
            }
          }
        }
      }
    }
  }
}

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Gregarican
Shopify Partner
1033 86 285

This is an accepted solution.

If you need to retrieve a large amount of records using GraphQL, it's usually best to try bulk operations --> https://shopify.dev/tutorials/perform-bulk-operations-with-admin-api. I did a test of this, and pulled over 49K products using this mechanism. And the results were available in 11 minutes. Using the REST API it would've taken awhile to iterate through each paginated query response.

View solution in original post

Replies 12 (12)