How to retrive all the products of shop using GraphQL

abhinavp00719
Shopify Partner
28 2 21

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 7 (7)
Josh
Shopify Staff
Shopify Staff
1134 84 232

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 15

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
23 3 2

@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
Shopify Staff
1134 84 232

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
Shopify Partner
3 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?

sivasankar
Shopify Partner
7 1 2

Hi,

 

Use bulkOperationRunQuery mutation to fetch all records.

AllenChen
New Member
1 0 0

I'm also new to GraphQL and Python, just completed the Python code. It works but I just want to find a better way, like bulkOperationRunQuery? Will Someone be able to copy and paste a complete function code here? Thanks so much. And also, I'll share the Python code once I get a better way.

# get inventory of all records, including sku and inventory
def getShopifyInventory(cursor=None, numResult=0, shopifyInventory=None):
# record quantity of every query, it's too slow if too small, probably exceeds cost limit 1000 of shopify query if too big
numPer = 40
# 1st the shopifyInventory array is empty
if shopifyInventory is None:
shopifyInventory = []
if cursor is None or numPer == numResult:
preQuery = """
{}
{{
edges {{
cursor
node {{
sku
inventoryQuantity
}}
}}
}}
}}
"""
if cursor is None:
query = preQuery.format("query ($numPer: Int!) { productVariants(first:$numPer)")
variables = {"numPer": numPer}
else:
query = preQuery.format(
"query ($cursor: String!, $numPer: Int!) { productVariants(first:$numPer, after:$cursor)")
variables = {"numPer": numPer, "cursor": cursor}
result = json.loads(graphQL.execute(query, variables))
# check it to find the right qty per query
# print(result["extensions"]["cost"]["throttleStatus"]["currentlyAvailable"])
variants = result["data"]["productVariants"]["edges"]
# print(variants)
for variant in variants:
shopifyInventory.append(variant["node"])
numResult = len(variants)
cursor = variants[numResult - 1]["cursor"]
getShopifyInventory(cursor, numResult, shopifyInventory)
return shopifyInventory