How to retrive all the products of shop using GraphQL

abhinavp00719
Shopify Partner
28 2 13

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.

Founder Descrii Description Generator
Replies 5 (5)
Josh
Shopify Staff
1131 82 203

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! 

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

emilbryggare
Shopify Partner
21 0 8

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.
Codesto1
Shopify Partner
15 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?

Josh
Shopify Staff
1131 82 203

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!  

Josh | Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

OluendEN
New Member
2 0 0

Thank You for Your answer, @Josh! It helped me a lot!

However, with Josh's query I am able to return only the first 1000 products.I have to pull more than 20 000 records. How to return more than 1000?