How to retrive all the products of shop using GraphQL

Tourist
12 1 1

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.

0 Likes
Shopify Staff
Shopify Staff
1102 75 141

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! 

2 Likes
Highlighted
Shopify Partner
21 0 3

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