How to retrive all the products of shop using GraphQL

Highlighted
Shopify Partner
26 2 7

Hello folks,

I am new to graphql and trying to get ids of all the products in shop.

I am looking for graphql equivalent to "/admin/products.json", as this gets list of all the products. But in graphql I need to provide first variable. So help please.

1 Like
Highlighted
Shopify Staff
Shopify Staff
1129 82 179

Hey there, 

 

GraphQL uses cursor-based pagination, so you'll need to add a line to any queries that you're making in order to get the cursors back that you need to paginate through products. You will then use a combination of 'first' and 'after' to page through products. 'After' will return the products that come 'after' the cursor value that you provide in your query.

 

As an example, here's a basic query you could use to start with : 

 

{
  products(first: 50) {
    edges {
      cursor
      node {
        id
      }
    }
  }
}

That query will return the first 50 products on a shop, and include their cursors in the response. You would then grab the cursor value of the last product provided in the response of the above query, and use that cursor value for your 'after' parameter in your next query like this : 

 

{
  products(first: 50, after:"eyJsYXN0X2lkIjoxOTMxMzU4OTYxNzIwLCJsYXN0X3ZhbHVlIjoiMTkzMTM1ODk2MTcyMCJ9") {
    edges {
      cursor
      node {
        id
      }
    }
  }
}

This will then give you the next 50 products on the shop. If you continue repeating this step with the cursor value of the last product in each response you get until your response is empty, you've then paginated through all available products. 

 

Happy GraphQL-ing! 

4 Likes
Highlighted
Shopify Partner
21 0 4

Great answer by @Josh already. If you are new to GraphQL, I would also recommend using the Shopify GraphQL Explorer to test your queries. With that tool, you will get code auto-completion and error checking of your queries on the fly.

 

Good luck!

Founder of Evermark - Smart and automated Facebook ads for your Shopify store.
1 Like
Highlighted
Shopify Partner
14 2 1

@Josh for me on an order query I get the last cursor then using that I try to do a call on the after but it gives me orders starting from the very beginning, August 2014 for me.  Any idea why that happens?

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1129 82 179

Hey @Codesto1 , 

 

Sorry for the delay, I've been away for the holidays. 

 

I wouldn't be able to say what was happening off the top of my head, but that doesn't sound like expected behaviour. 

 

Could you post the query/queries that you've been using in here so I can take a look? The shop you were performing the query on and a rough timeframe would be helpful as well if it's not too long ago to remember. 

 

Thanks!  

0 Likes