Best way to fetch all products' images using GraphQL

AndreiAverkin
Shopify Partner
23 0 7

Hey guys,

I'm migrating my 'products' API request from REST to GraphQL.
While in REST I used to get all the products' images in the response, in GraphQL I need to specify the amount of media (first/last).

Is there a way to get all products' media in GraphQL without exceeding query cost of 1000, and without paginating on media in each product?
If not, what's the best way to paginate the images of the products?

0 Likes
hassain
Shopify Staff (Retired)
Shopify Staff (Retired)
623 103 142

Hey @AndreiAverkin ,

 

You can achieve paginating through the images of a product by making the following GraphQL query:

{
  product(id: "gid://shopify/Product/1990442876950") {
  	id
    images(first:10) {
     pageInfo {
    	 hasNextPage
       hasPreviousPage
      }
      edges {
       cursor
       node {
         originalSrc
         id
       }
      }
    }  
  }
}

In the results, if you see that "hasNextPage = true", that means that there are more than 10 images for this product and that you need to paginate to the next page to get the next set of images for that product. To get the next page, you will use the exact same query as above but instead replace the line `images(first:10)` with `images(first:10, after:"INSERT CURSOR OF LAST IMAGE")`

 

Alternatively, if you do not want to use any pagination at all and just get all images of each product, you can look into using the Bulk Operations feature of the Shopify GraphQL API: https://shopify.dev/tutorials/perform-bulk-operations-with-admin-api

 

Hassain | Developer Support Specialist @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Click Accept as Solution 

AndreiAverkin
Shopify Partner
23 0 7

Thanks @hassain!
I ended up making one GQL request for the product ids (with search term, and paginated), and then another REST request with the product ids to get all the data (not paginated).

tobychen
Tourist
3 0 0

Hi @AndreiAverkin,

I also have the same issue like your. Could you give me the GraphQL requests to do it?

0 Likes