For Zoho Services only:


I'm actually part of something bigger at Ascent Business Solutions recognized as the top Zoho Premium Solutions Partner in the United Kingdom.

Ascent Business Solutions offer support for smaller technical fixes and projects for larger developments, such as migrating to a ZohoCRM.  A team rather than a one-man-band is always available to ensure seamless progress and address any concerns. You'll find our competitive support rates with flexible, no-expiration bundles at https://ascentbusiness.co.uk/zoho-services/uk-zoho-support.  For larger projects, talk to our experts and receive dedicated support from our hands-on project consultants at https://ascentbusiness.co.uk/zoho-services/zoho-crm-implementation.

The team I manage specializes in coding API integrations between Zoho and third-party finance/commerce suites such as Xero, Shopify, WooCommerce, and eBay; to name but a few.  Our passion lies in creating innovative solutions where others have fallen short as well as working with new businesses, new sectors, and new ideas.  Our success is measured by the growth and ROI we deliver for clients, such as transforming a garden shed hobby into a 250k monthly turnover operation or generating a +60% return in just three days after launch through online payments and a streamlined e-commerce solution, replacing a paper-based system.

If you're looking for a partner who can help you drive growth and success, we'd love to work with you.  You can reach out to us on 0121 392 8140 (UK) or info@ascentbusiness.co.uk.  You can also visit our website at https://ascentbusiness.co.uk.

Zoho Deluge: Shopify API: Get all active products with GraphQL and Pagination

What?
An article to resolve my frustration in being able to ONLY retrieve the first 100 products using GraphQL, so page 1 of Shopify products.

Why?
Our use-case is that we retrieve the 100 most recently modified products at the end of each day and run some Deluge code against it to ensure that the data in Shopify is the same as in Zoho. In this case, Shopify is considered the source of truth and Zoho is the data to be overwritten, ensuring that Product IDs, Variant IDs, Inventory IDs, Current Selling Price, Inventory Level, and Barcode all match.

Our problem is that sometimes more than 100 products are modified in a day... We need to be able to do more than 100 or at least resume from a record... We need pagination.

How?
To resolve this, I'm building on top of my initial GraphQL query which will now retrieve 10 products per page and loop through 3 pages for use in Zoho Deluge. If this works, we can increase the number of pages as well as the number of products per page:

The GraphQL
The key value to retrieve is "cursor"; we're also going to use "hasNextPage" to determine if we need to keep looping:
copyraw
{
  productVariants(first: 10, query: "updated_at:>'2023-02-20T00:00:00+0000'") {
    edges {
      node {
        id
        price
        sku
        barcode
        inventoryQuantity
        inventoryItem {
          id
        }
        product {
          id
          title
          handle
          status
          createdAt
          publishedAt
          updatedAt
        }
        updatedAt
      }
      cursor
    }
    pageInfo {
      hasNextPage
    }
  }
}
  1.  { 
  2.    productVariants(first: 10, query: "updated_at:>'2023-02-20T00:00:00+0000'") { 
  3.      edges { 
  4.        node { 
  5.          id 
  6.          price 
  7.          sku 
  8.          barcode 
  9.          inventoryQuantity 
  10.          inventoryItem { 
  11.            id 
  12.          } 
  13.          product { 
  14.            id 
  15.            title 
  16.            handle 
  17.            status 
  18.            createdAt 
  19.            publishedAt 
  20.            updatedAt 
  21.          } 
  22.          updatedAt 
  23.        } 
  24.        cursor 
  25.      } 
  26.      pageInfo { 
  27.        hasNextPage 
  28.      } 
  29.    } 
  30.  } 

The Deluge Code
So this is a Zoho Creator function that I'm going to call API.fn_ShopifyQuery_UpdateAllProducts() and it will be added to a nightly schedule. It's aim is to recover all the modified records of a day and ensure that the data in Zoho is the same as on the Shopify website. Included in this is the feature to export all the data as a CSV and to email it to myself:
copyraw
void API.fn_ShopifyQuery_UpdateAllProducts()
{
	// init
	v_CountTotal = 0;
	v_CountUpdated = 0;
	v_PerPage = 10;
	b_HasNextPage = true;
	v_Cursor = "";
	l_CsvFileRows = List();
	//
	// start preparing CSV file to email for debugging purposes
	v_ReportCSV = "Ref,Shopify SKU,Shopify Title,Shopify Product ID,Shopify Variant ID,Shopify Inventory ID,Shopify Price,Shopify Quantity,Shopify Status,Shopify Barcode,Created on Shopify,Modified on Shopify,Published on Shopify";
	l_CsvFileRows = List();
	l_CsvFileRows.add(v_ReportCSV);
	// 
	// app specific 
	v_ShopID ="joels-awesome-shop";
	v_ShopifyApiVersion = "2022-01";
	v_AccessToken = "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2222";
	v_Endpoint = "https://" + v_ShopID + "/admin/api/" + v_ShopifyApiVersion.toString() + "/graphql.json";
	// 
	// set header parameters 
	m_Header = Map();
	m_Header.put("Content-Type","application/json");
	m_Header.put("X-Shopify-Access-Token",v_AccessToken);
	//
	// create a list of pages (specifies the number of pages to try)
	l_Pages = {1,2,3};
	for each  v_Page in l_Pages
	{
		if(b_HasNextPage)
		{
			//
			// query using GraphQL
			v_UpdatedSince = zoho.currentdate.subDay(1).toString("yyyy-MM-dd'T'00:00:00+0000");
			if(v_Page == 1)
			{
				v_GraphQl = "{ productVariants(first: " + v_PerPage + ", query: \"updated_at:>'" + v_UpdatedSince + "'\") { edges { node { id price sku barcode inventoryQuantity inventoryItem { id } product { id title handle status createdAt publishedAt updatedAt } updatedAt } cursor} pageInfo { hasNextPage } } }";
			}
			else
			{
				v_GraphQl = "{ productVariants(first: " + v_PerPage + ", after: \"" + v_Cursor + "\", query: \"updated_at:>'" + v_UpdatedSince + "'\") { edges { node { id price sku barcode inventoryQuantity inventoryItem { id } product { id title handle status createdAt publishedAt updatedAt } updatedAt } cursor} pageInfo { hasNextPage } } }";
			}
			//info v_GraphQl;
			//
			// query using GraphQL
			m_GraphQl = Map();
			m_GraphQl.put("query",v_GraphQl);
			// 
			// Let's test with this GraphQL query 
			r_GetProductVariants = invokeurl
			[
				url :v_Endpoint
				type :POST
				parameters:m_GraphQl.toString()
				headers:m_Header
			];
			//info r_GetProductVariants;
			// 
			// parse the response 
			if(r_GetProductVariants.get("data") != null)
			{
				if(r_GetProductVariants.get("data").get("productVariants") != null)
				{
					if(r_GetProductVariants.get("data").get("productVariants").get("edges") != null)
					{
						l_Edges = r_GetProductVariants.get("data").get("productVariants").get("edges");
						for each  r_Node in l_Edges
						{
							v_CountTotal = v_CountTotal + 1;
							//
							// parse node
							m_Node = r_Node.get("node");
							//
							// evaluate
							v_VariantID = m_Node.get("id").getSuffix("ProductVariant/");
							v_VariantSKU = m_Node.get("sku");
							v_VariantBarcode = m_Node.get("barcode");
							v_VariantPrice = m_Node.get("price");
							v_VariantQty = m_Node.get("inventoryQuantity");
							v_InventoryID = m_Node.get("inventoryItem").get("id").getSuffix("InventoryItem/");
							v_ProductID = m_Node.get("product").get("id").getSuffix("Product/");
							v_ProductTitle = m_Node.get("product").get("title");
							v_ProductHandle = m_Node.get("product").get("handle");
							v_ProductStatus = m_Node.get("product").get("status");
							v_ProductCreated = m_Node.get("product").get("createdAt");
							v_ProductPublished = m_Node.get("product").get("publishedAt");
							v_ProductModified = m_Node.get("product").get("updatedAt");
							v_VariantModified = m_Node.get("updatedAt");
							//
							// transforms
							if(v_ProductTitle.contains(","))
							{
								v_ProductTitle = "\"" + v_ProductTitle + "\"";
							}
							if(v_ProductHandle.contains(","))
							{
								v_ProductHandle = "\"" + v_ProductHandle + "\"";
							}
							v_Shopify_Created = v_ProductCreated.getPrefix("Z").replaceAll("T"," ");
							v_Shopify_PublishedStr = if(!isNull(v_ProductPublished),v_ProductPublished,v_Shopify_Created);
							v_Shopify_Published = v_Shopify_PublishedStr.getPrefix("Z").replaceAll("T"," ");
							v_Shopify_UpdatedStr = if(!isNull(v_ProductModified),v_ProductModified,v_Shopify_Created);
							v_Shopify_Updated = v_Shopify_UpdatedStr.getPrefix("Z").replaceAll("T"," ");
							// adding an apostrophe/single-quote to ID values so MS Excel doesn't round them up
							v_ProductIDStr = "\"'" + v_ProductID + "\"";
							v_VariantIDStr = "\"'" + v_VariantID + "\"";
							v_InventoryIDStr = "\"'" + v_InventoryID + "\"";
							//
							// generate CSV row for monitoring purposes
							l_CsvFileRow = List();
							l_CsvFileRow.add(v_CountTotal);
							l_CsvFileRow.add(v_VariantSKU);
							l_CsvFileRow.add(v_ProductTitle);
							l_CsvFileRow.add(v_ProductIDStr);
							l_CsvFileRow.add(v_VariantIDStr);
							l_CsvFileRow.add(v_InventoryIDStr);
							l_CsvFileRow.add(v_VariantPrice);
							l_CsvFileRow.add(v_VariantQty);
							l_CsvFileRow.add(v_ProductStatus);
							l_CsvFileRow.add(v_VariantBarcode);
							l_CsvFileRow.add(v_Shopify_Created);
							l_CsvFileRow.add(v_Shopify_Updated);
							l_CsvFileRow.add(v_Shopify_Published);
							v_CsvRow = l_CsvFileRow.toString();
							l_CsvFileRows.add(v_CsvRow);
							//
							// note the cursor so that we resume from the last cursor
							v_Cursor = r_Node.get("cursor");
							//
							// output to console for debugging purposes
							info "Page #" + v_Page + "::" + v_CountTotal + "::" + v_VariantSKU + "::" + v_Cursor;
						}
						//
						// capture whether to continue the loop or not
						b_HasNextPage = r_GetProductVariants.get("data").get("productVariants").get("pageInfo").get("hasNextPage");
					}
				}
			}
		}
	}
	// 
	// generate a CSV list for monitoring purposes
	v_CSVFilename = "replay-golf-export-active-page-" + zoho.currenttime.toString("yyyy-MM-dd-HH-mm-ss") + ".csv";
	l_CsvFileRows.add("-----------------------------" + v_CSVFilename);
	v_CSVFile = l_CsvFileRows.toString("\n").toFile(v_CSVFilename);
	// 
	// send via Email 
	v_CountRows = l_CsvFileRows.size() - 2;
	v_Subject = "Shopify Products Export & Comparison";
	v_Message = "Please find attached a log of <b>" + v_CountRows + "</b> Product(s) from Shopify that had been updated on " + zoho.currenttime.subDay(1).toString("EEEE, dd-MMM-yyyy") + "<br /><br />";
	v_Message = v_Message + "There were " + v_CountUpdated + " record(s) updated.<br /><br />Kind Regards,<br /><br />The Team";
	sendmail
	[
		from :zoho.loginuserid
		to:"Joel Lipman <This email address is being protected from spambots. You need JavaScript enabled to view it.>"
		subject :v_Subject
		message :v_Message
		Attachments :file:v_CSVFile
	]	
}
  1.  void API.fn_ShopifyQuery_UpdateAllProducts() 
  2.  { 
  3.      // init 
  4.      v_CountTotal = 0
  5.      v_CountUpdated = 0
  6.      v_PerPage = 10
  7.      b_HasNextPage = true
  8.      v_Cursor = ""
  9.      l_CsvFileRows = List()
  10.      // 
  11.      // start preparing CSV file to email for debugging purposes 
  12.      v_ReportCSV = "Ref,Shopify SKU,Shopify Title,Shopify Product ID,Shopify Variant ID,Shopify Inventory ID,Shopify Price,Shopify Quantity,Shopify Status,Shopify Barcode,Created on Shopify,Modified on Shopify,Published on Shopify"
  13.      l_CsvFileRows = List()
  14.      l_CsvFileRows.add(v_ReportCSV)
  15.      // 
  16.      // app specific 
  17.      v_ShopID ="joels-awesome-shop"
  18.      v_ShopifyApiVersion = "2022-01"
  19.      v_AccessToken = "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2222"
  20.      v_Endpoint = "https://" + v_ShopID + "/admin/api/" + v_ShopifyApiVersion.toString() + "/graphql.json"
  21.      // 
  22.      // set header parameters 
  23.      m_Header = Map()
  24.      m_Header.put("Content-Type","application/json")
  25.      m_Header.put("X-Shopify-Access-Token",v_AccessToken)
  26.      // 
  27.      // create a list of pages (specifies the number of pages to try) 
  28.      l_Pages = {1,2,3}
  29.      for each  v_Page in l_Pages 
  30.      { 
  31.          if(b_HasNextPage) 
  32.          { 
  33.              // 
  34.              // query using GraphQL 
  35.              v_UpdatedSince = zoho.currentdate.subDay(1).toString("yyyy-MM-dd'T'00:00:00+0000")
  36.              if(v_Page == 1) 
  37.              { 
  38.                  v_GraphQl = "{ productVariants(first: " + v_PerPage + ", query: \"updated_at:>'" + v_UpdatedSince + "'\") { edges { node { id price sku barcode inventoryQuantity inventoryItem { id } product { id title handle status createdAt publishedAt updatedAt } updatedAt } cursor} pageInfo { hasNextPage } } }"
  39.              } 
  40.              else 
  41.              { 
  42.                  v_GraphQl = "{ productVariants(first: " + v_PerPage + ", after: \"" + v_Cursor + "\", query: \"updated_at:>'" + v_UpdatedSince + "'\") { edges { node { id price sku barcode inventoryQuantity inventoryItem { id } product { id title handle status createdAt publishedAt updatedAt } updatedAt } cursor} pageInfo { hasNextPage } } }"
  43.              } 
  44.              //info v_GraphQl; 
  45.              // 
  46.              // query using GraphQL 
  47.              m_GraphQl = Map()
  48.              m_GraphQl.put("query",v_GraphQl)
  49.              // 
  50.              // Let's test with this GraphQL query 
  51.              r_GetProductVariants = invokeUrl 
  52.              [ 
  53.                  url :v_Endpoint 
  54.                  type :POST 
  55.                  parameters:m_GraphQl.toString() 
  56.                  headers:m_Header 
  57.              ]
  58.              //info r_GetProductVariants; 
  59.              // 
  60.              // parse the response 
  61.              if(r_GetProductVariants.get("data") != null) 
  62.              { 
  63.                  if(r_GetProductVariants.get("data").get("productVariants") != null) 
  64.                  { 
  65.                      if(r_GetProductVariants.get("data").get("productVariants").get("edges") != null) 
  66.                      { 
  67.                          l_Edges = r_GetProductVariants.get("data").get("productVariants").get("edges")
  68.                          for each  r_Node in l_Edges 
  69.                          { 
  70.                              v_CountTotal = v_CountTotal + 1
  71.                              // 
  72.                              // parse node 
  73.                              m_Node = r_Node.get("node")
  74.                              // 
  75.                              // evaluate 
  76.                              v_VariantID = m_Node.get("id").getSuffix("ProductVariant/")
  77.                              v_VariantSKU = m_Node.get("sku")
  78.                              v_VariantBarcode = m_Node.get("barcode")
  79.                              v_VariantPrice = m_Node.get("price")
  80.                              v_VariantQty = m_Node.get("inventoryQuantity")
  81.                              v_InventoryID = m_Node.get("inventoryItem").get("id").getSuffix("InventoryItem/")
  82.                              v_ProductID = m_Node.get("product").get("id").getSuffix("Product/")
  83.                              v_ProductTitle = m_Node.get("product").get("title")
  84.                              v_ProductHandle = m_Node.get("product").get("handle")
  85.                              v_ProductStatus = m_Node.get("product").get("status")
  86.                              v_ProductCreated = m_Node.get("product").get("createdAt")
  87.                              v_ProductPublished = m_Node.get("product").get("publishedAt")
  88.                              v_ProductModified = m_Node.get("product").get("updatedAt")
  89.                              v_VariantModified = m_Node.get("updatedAt")
  90.                              // 
  91.                              // transforms 
  92.                              if(v_ProductTitle.contains(",")) 
  93.                              { 
  94.                                  v_ProductTitle = "\"" + v_ProductTitle + "\""
  95.                              } 
  96.                              if(v_ProductHandle.contains(",")) 
  97.                              { 
  98.                                  v_ProductHandle = "\"" + v_ProductHandle + "\""
  99.                              } 
  100.                              v_Shopify_Created = v_ProductCreated.getPrefix("Z").replaceAll("T"," ")
  101.                              v_Shopify_PublishedStr = if(!isNull(v_ProductPublished),v_ProductPublished,v_Shopify_Created)
  102.                              v_Shopify_Published = v_Shopify_PublishedStr.getPrefix("Z").replaceAll("T"," ")
  103.                              v_Shopify_UpdatedStr = if(!isNull(v_ProductModified),v_ProductModified,v_Shopify_Created)
  104.                              v_Shopify_Updated = v_Shopify_UpdatedStr.getPrefix("Z").replaceAll("T"," ")
  105.                              // adding an apostrophe/single-quote to ID values so MS Excel doesn't round them up 
  106.                              v_ProductIDStr = "\"'" + v_ProductID + "\""
  107.                              v_VariantIDStr = "\"'" + v_VariantID + "\""
  108.                              v_InventoryIDStr = "\"'" + v_InventoryID + "\""
  109.                              // 
  110.                              // generate CSV row for monitoring purposes 
  111.                              l_CsvFileRow = List()
  112.                              l_CsvFileRow.add(v_CountTotal)
  113.                              l_CsvFileRow.add(v_VariantSKU)
  114.                              l_CsvFileRow.add(v_ProductTitle)
  115.                              l_CsvFileRow.add(v_ProductIDStr)
  116.                              l_CsvFileRow.add(v_VariantIDStr)
  117.                              l_CsvFileRow.add(v_InventoryIDStr)
  118.                              l_CsvFileRow.add(v_VariantPrice)
  119.                              l_CsvFileRow.add(v_VariantQty)
  120.                              l_CsvFileRow.add(v_ProductStatus)
  121.                              l_CsvFileRow.add(v_VariantBarcode)
  122.                              l_CsvFileRow.add(v_Shopify_Created)
  123.                              l_CsvFileRow.add(v_Shopify_Updated)
  124.                              l_CsvFileRow.add(v_Shopify_Published)
  125.                              v_CsvRow = l_CsvFileRow.toString()
  126.                              l_CsvFileRows.add(v_CsvRow)
  127.                              // 
  128.                              // note the cursor so that we resume from the last cursor 
  129.                              v_Cursor = r_Node.get("cursor")
  130.                              // 
  131.                              // output to console for debugging purposes 
  132.                              info "Page #" + v_Page + "::+ v_CountTotal + "::+ v_VariantSKU + "::+ v_Cursor; 
  133.                          } 
  134.                          // 
  135.                          // capture whether to continue the loop or not 
  136.                          b_HasNextPage = r_GetProductVariants.get("data").get("productVariants").get("pageInfo").get("hasNextPage")
  137.                      } 
  138.                  } 
  139.              } 
  140.          } 
  141.      } 
  142.      // 
  143.      // generate a CSV list for monitoring purposes 
  144.      v_CSVFilename = "replay-golf-export-active-page-" + zoho.currenttime.toString("yyyy-MM-dd-HH-mm-ss") + ".csv"
  145.      l_CsvFileRows.add("-----------------------------" + v_CSVFilename)
  146.      v_CSVFile = l_CsvFileRows.toString("\n").toFile(v_CSVFilename)
  147.      // 
  148.      // send via Email 
  149.      v_CountRows = l_CsvFileRows.size() - 2
  150.      v_Subject = "Shopify Products Export & Comparison"
  151.      v_Message = "Please find attached a log of <b>" + v_CountRows + "</b> Product(s) from Shopify that had been updated on " + zoho.currenttime.subDay(1).toString("EEEE, dd-MMM-yyyy") + "<br /><br />"
  152.      v_Message = v_Message + "There were " + v_CountUpdated + record(s) updated.<br /><br />Kind Regards,<br /><br />The Team"
  153.      sendmail 
  154.      [ 
  155.          from :zoho.loginuserid 
  156.          to:"Joel Lipman <This email address is being protected from spambots. You need JavaScript enabled to view it.>" 
  157.          subject :v_Subject 
  158.          message :v_Message 
  159.          Attachments :file:v_CSVFile 
  160.      ] 
  161.  } 

Error(s):
  • {"errors":[{"message":"Field 'endCursor' doesn't exist on type 'PageInfo'","locations":[{"line":1,"column":254}],"path":["query","productVariants","pageInfo","endCursor"],"extensions":{"code":"undefinedField","typeName":"PageInfo","fieldName":"endCursor"}}]}
    I only resolved this by not using "endCursor" in my GraphQL and instead looped through the nodes to get the last cursor. Not sure if this meant any data was missing. Perhaps by forcing the sort criteria to be by order of cursor alphabetically; but we were happy with the results the above code did anyway so I haven't tried this.

Source(s):

Category: Zoho :: Article: 833

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.