shopify-api-js icon indicating copy to clipboard operation
shopify-api-js copied to clipboard

GraphQL API "currentlyAvailable" rate limit dropped unexpectedly

Open joseph423 opened this issue 4 years ago • 2 comments

Issue summary

Little background I was developing my app and I need to invoke Shopify GraphQL API. I noticed there is a rate limit (1000) so I studied the Shopify blog https://www.shopify.com/partners/blog/optimize-rate-limit to have some ideas about the handling.

Issue here I made very simple program to make 3 async(rapid, burst API call) Shopify GraphQL API calls to get some data and observe the changes of remaining rate limit (By looking at the "actualQueryCost" and "currentlyAvailable" fields in ).

But the "currentlyAvailable" dropped unexpectedly. (Cost more than it should)

This makes the API cost completely unpredictable.

Expected behavior

3 async query, each cost 3. That means 3 * 3 = 9 query cost in total.

The "currentlyAvailable" should be at most = 1000 - 9 = 991. (Assumed my leaky bucket was 1000(full) and all queries are done within a second(not being re-filled 50))

Actual behavior

One of the "currentlyAvailable" from responses shows 985 (Sometimes it drops to 974) (Please check "Test Result" section for test code output)

As discussed previously, the total query cost is just 9, the "currentlyAvailable" should never be lower than 991.

Steps to reproduce the problem

  1. Make sure you have Node.JS installed and have a empty folder for testing
  2. Install the '@shopify/shopify-api' npm package (my version is "^5.0.0").
  3. Copy and paste the "Test Code" into a new .js file. (let's say index.js, same level as node_modules folder)
  4. Please replace the proper 'YOUR_SHOP_DOMAIN' and 'YOUR_ACCESS_TOKEN'
  5. Run command "node index.js" and see output

Test Code (Please replace 'YOUR_SHOP_DOMAIN' and 'YOUR_ACCESS_TOKEN')

import { Shopify } from '@shopify/shopify-api';

const YOUR_SHOP_DOMAIN = 'YOUR_SHOP_DOMAIN'; const ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN';

(async () => { Shopify.Context.API_VERSION = '2022-07';

const client = new Shopify.Clients.Graphql(YOUR_SHOP_DOMAIN, ACCESS_TOKEN);

let res1 = client.query({ data: { query: #graphql query Customers($first: Int, $query: String) { customers(first: $first, query: $query) { nodes { id lastName firstName email phone } } } , variables: { first: 10, query: '(email:[email protected])', }, }, extraHeaders: { 'X-GraphQL-Cost-Include-Fields': true, }, tries: 0, });

let res2 = client.query({ data: { query: #graphql query Customers($first: Int, $query: String) { customers(first: $first, query: $query) { nodes { id lastName firstName email phone } } } , variables: { first: 10, query: '(email:[email protected])', }, }, extraHeaders: { 'X-GraphQL-Cost-Include-Fields': true, }, tries: 0, });

let res3 = client.query({ data: { query: #graphql query Customers($first: Int, $query: String) { customers(first: $first, query: $query) { nodes { id lastName firstName email phone } } } , variables: { first: 10, query: '(email:[email protected]) OR (email:[email protected])', }, }, extraHeaders: { 'X-GraphQL-Cost-Include-Fields': true, }, tries: 0, });

console.log(JSON.stringify(await Promise.all([res1, res2, res3])), null, 2); })();

Test Result

[ { "body": { "data": { "customers": { "nodes": [] } }, "extensions": { "cost": { "requestedQueryCost": 12, "actualQueryCost": 3, "throttleStatus": { "maximumAvailable": 1000, "currentlyAvailable": 985, "restoreRate": 50 }, "fields": [ { "path": [ "customers", "nodes", "id" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "lastName" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "firstName" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "email" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "phone" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes" ], "definedCost": 1, "requestedTotalCost": 1, "requestedChildrenCost": 0 }, { "path": [ "customers" ], "definedCost": null, "requestedTotalCost": 12, "requestedChildrenCost": 1 } ] } } }, "headers": {} }, { "body": { "data": { "customers": { "nodes": [] } }, "extensions": { "cost": { "requestedQueryCost": 12, "actualQueryCost": 3, "throttleStatus": { "maximumAvailable": 1000, "currentlyAvailable": 996, "restoreRate": 50 }, "fields": [ { "path": [ "customers", "nodes", "id" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "lastName" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "firstName" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "email" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "phone" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes" ], "definedCost": 1, "requestedTotalCost": 1, "requestedChildrenCost": 0 }, { "path": [ "customers" ], "definedCost": null, "requestedTotalCost": 12, "requestedChildrenCost": 1 } ] } } }, "headers": {} }, { "body": { "data": { "customers": { "nodes": [] } }, "extensions": { "cost": { "requestedQueryCost": 12, "actualQueryCost": 3, "throttleStatus": { "maximumAvailable": 1000, "currentlyAvailable": 994, "restoreRate": 50 }, "fields": [ { "path": [ "customers", "nodes", "id" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "lastName" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "firstName" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "email" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes", "phone" ], "definedCost": 0, "requestedTotalCost": 0, "requestedChildrenCost": null }, { "path": [ "customers", "nodes" ], "definedCost": 1, "requestedTotalCost": 1, "requestedChildrenCost": 0 }, { "path": [ "customers" ], "definedCost": null, "requestedTotalCost": 12, "requestedChildrenCost": 1 } ] } } }, "headers": {} } ]

joseph423 avatar Nov 17 '22 09:11 joseph423

This issue is stale because it has been open for 90 days with no activity. It will be closed if no further action occurs in 14 days.

github-actions[bot] avatar Jan 31 '23 02:01 github-actions[bot]

We are closing this issue because it has been inactive for a few months. This probably means that it is not reproducible or it has been fixed in a newer version. If it’s an enhancement and hasn’t been taken on since it was submitted, then it seems other issues have taken priority.

If you still encounter this issue with the latest stable version, please reopen using the issue template. You can also contribute directly by submitting a pull request– see the CONTRIBUTING.md file for guidelines

Thank you!

github-actions[bot] avatar Feb 14 '23 02:02 github-actions[bot]