How to Get Image Position from GraphQL Products API

How to Get Image Position from GraphQL Products API

huangcong12
Shopify Partner
10 0 1

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","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!

ImgCraft: AI Sharp & Resize - Rescuing Low-Quality Images | LitaCat: Page Watermark - Convey Your Voice
Replies 2 (2)

lthomas1
Shopify Partner
30 0 5

Hi @huangcong12 

Did you got a solution ? 

Lily

huangcong12
Shopify Partner
10 0 1

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.

huangcong12_0-1718151672078.png

 

ImgCraft: AI Sharp & Resize - Rescuing Low-Quality Images | LitaCat: Page Watermark - Convey Your Voice