How to Get Image Position from GraphQL Products API

Hello everyone,

I’m trying to query image information using the GraphQL products API. One important field I need is the position of the image on the product detail page. Here is my current query:

query {
    products(first:10 reverse:true) {
      edges {
        node {
          id
          title
          status
          vendor
          featuredImage {
            id
          }
          category {
            name
          }
          handle
          images(first: 24) {
            edges {
              node {
                id
                url
                height
                width
                altText
              }
            }
          }
          variants(first:24) {
            edges {
              node {
                sku,
                image {
                  id
                  altText
                  url
                  height
                  width
                }
              }
            }
          }
        }
      }
      pageInfo {
        hasNextPage
        endCursor
        hasPreviousPage
        startCursor
      }
    }
  }

However, I am unsure how to retrieve the position of each image on the product detail page using this query. This field is available in the REST API, but calling the REST API returns the following deprecation warning:

[shopify-api/WARNING] API Deprecation Notice 2024/5/16 10:03:01 : {"message":"[https://shopify.dev/api/admin-rest/latest/resources/product](https://shopify.dev/api/admin-rest/latest/resources/product)","path":"products.json"} - Stack Trace: Error

Since the REST API is being deprecated, I am trying to obtain all the necessary fields from the GraphQL products API.

Does anyone have any suggestions or solutions for this issue? Thank you very much!

Hi @huangcong12

Did you got a solution ?

Lily

Yes, I found the solution. I discovered that the default sorting for media is based on position, with the retrieved images arranged from the first to the last. If you want to know the position, you just need to use a for loop to calculate it: position = i + 1.

From the GraphQL Product Object, the field media has a default sort key of POSITION. The position is also, as huangcong12 pointed out the same as they appear in the store. I verified this with 5 products. I’m writing a script which handles product images so re-uploading them in the same order is essential.