I am using the node.js module "shopify-api-node" to call Shopifys graphql API. I can see in the documentation you can access the throttling information via callGraphqlLimits, but how are you supposed to use it to throttle the requests?

If I e.g. want to delay the next call 5s I tried this, but the delay seems to be executed asynchronously (while testing I force immediate throttling by checking when limit is less than 1000):

shopify.on('callGraphqlLimits', async (limits) => {
    console.log(limits)
    if (limits.remaining < 1000){
      console.log('Delaying 5s')
      await delay(5000)
    }
  })

Thanks, -Louise

1

There are 1 answers

0
ozzyonfire On

Here is how I manage throttling using that library. Conceptually the process is as follows;

  • Do a query
  • Determine your remaining cost
  • Wait for the required number of seconds
  • Repeat

So if I had an expensive query, the process could look like this (pagination stuff omitted for brevity)

async function expensiveQuery() {
  let currentCost = 0;
  let hasNextPage;
  
  shopify.on('callGraphqlLimits', (limits) => {
    currentCost = limits.current;
  });

  do {
    const waitTime = Math.ceil(currentCost / 50); // 50 is the default restore rate
    await timeout(waitTime * 1000);
    const data = await shopify.graphql(GET_PRODUCTS);
    const { products: { edges, pageInfo } } = data;
    // do something with data;
    hasNextPage = pageInfo.hasNextPage;
  } while (hasNextPage)
}

function timeout(ms) { // helper function
  return new Promise(resolve => setTimeout(resolve, ms));
}

This will always clear out your current cost, so you will always have a "full bucket". However, if we knew the cost for the query you could do something more sophisticated. Ideally, you don't want to wait until your bucket is full, but just wait long enough so you have enough cost available to perform your query.