Gadget now offers the ability to programmatically sync Shopify data to your backend, using the new Sync API. The API can be used to fetch store data, check on a sync’s status, or power a user interface. The Sync API is available on all Gadget projects with an active Shopify Connection.
Gadget’s Shopify Connection provides a robust data sync between your Gadget app and a Shopify store, configured in just a few clicks. Out of the box, your app OAuths with Shopify, reliably receives webhooks, and syncs historical data without you having to write any code.
The syncing system for the Shopify Connection works by leveraging Actions and Effects. Each time Gadget goes to sync the connected shop, it creates a new Shopify Sync record and dispatches the Run and either the Complete or Error actions based on the outcome of the sync.
In the past, Gadget offered you two ways to sync historical data:
While manual and daily syncs cover many use-cases, developers often need the ability to trigger a sync programmatically, in response to end-user behavior or other events in the system.
For maximum control, we’re introducing the Sync API, which offers you the ability to trigger and react to syncs in code. The Shopify Sync model and its Actions are now available in your application’s GraphQL API, so you can run them to fetch data, query them to check on sync statuses, or invoke them to power your application.
You can start a Shopify Sync programmatically by making a call to the <inline-code>runShopifySync<inline-code> mutation in GraphQL from any client or your Gadget backend:
This will dispatch the <inline-code>Run<inline-code> action in your Sync model, and subsequently the <inline-code>Complete<inline-code> or <inline-code>Error<inline-code> actions, depending on the result of the sync.
If your app should run a sync as soon as a merchant installs it, you can add a Success Effect to your Shop Model. The Shop model captures install/uninstall events from Shopify, and can run the sync code above:
The Sync API also gives you the ability to set a starting point for the sync. For example, if you only want to sync records from the last five days, you can do so by passing the optional parameter <inline-code>syncSince<inline-code>, as shown below:
In the event that a sync fails, you can troubleshoot the failure by looking for the sync ID in your logs to find the relevant details:
The Sync API is instantly available on every Gadget project, once a Shopify Connection is established. We’re excited to see what you build with it.