Unexpectedly Low GraphQL Query Cost in Shopify Admin API: Seeking Clarification

Hello, Everyone!

I have a question regarding the calculation of query costs in the Shopify Admin GraphQL API. According to the documentation and the article, the cost of a query should be proportional to the number of objects and connections requested. However, I am seeing a significant discrepancy between my manual calculation and the actual cost returned by the API.

For example, I ran the following query:

query getVariantsInventoryItem($id: ID!) {
    product(id: $id) {
        variants(first: 250) {
            nodes {
                inventoryItem {
                    id
                    tracked
                }
            }
        }
    }
}

Based on the article Rate Limiting GraphQL APIs by Calculating Query Complexity - Shopify , I expected this query to cost 503 points, assuming each variant and inventory item would be counted individually. However, the API response provided:

with the following breakdown in the extensions.cost.fields object:

{
    "data": {
        "product": null
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 25,
            "actualQueryCost": 1,
            "throttleStatus": {
                "maximumAvailable": 2000,
                "currentlyAvailable": 1999,
                "restoreRate": 100
            },
            "fields": [
                {
                    "path": [
                        "product",
                        "variants",
                        "nodes",
                        "inventoryItem",
                        "id"
                    ],
                    "definedCost": 0,
                    "requestedTotalCost": 0,
                    "requestedChildrenCost": null
                },
                {
                    "path": [
                        "product",
                        "variants",
                        "nodes",
                        "inventoryItem",
                        "tracked"
                    ],
                    "definedCost": 0,
                    "requestedTotalCost": 0,
                    "requestedChildrenCost": null
                },
                {
                    "path": [
                        "product",
                        "variants",
                        "nodes",
                        "inventoryItem"
                    ],
                    "definedCost": 1,
                    "requestedTotalCost": 1,
                    "requestedChildrenCost": 0
                },
                {
                    "path": [
                        "product",
                        "variants",
                        "nodes"
                    ],
                    "definedCost": 1,
                    "requestedTotalCost": 2,
                    "requestedChildrenCost": 1
                },
                {
                    "path": [
                        "product",
                        "variants"
                    ],
                    "definedCost": null,
                    "requestedTotalCost": 24,
                    "requestedChildrenCost": 2
                },
                {
                    "path": [
                        "product"
                    ],
                    "definedCost": 1,
                    "requestedTotalCost": 25,
                    "requestedChildrenCost": 24
                }
            ]
        }
    }
}

Could you please clarify why the query cost is so much lower than expected? Is there a change in how costs are calculated, or is there an internal optimization that affects the returned values? Any insights would be greatly appreciated.

Thank you for your time!

Hi Ulan :waving_hand:

Thank you for pointing out that the Connections section of the blog post is outdated. I’ll review it and update it to reflect the latest changes in rate limits and complexity cost calculations.

Is there a change in how costs are calculated, or is there an internal optimization that affects the returned values?

Yes — during Winter Editions in early 2024, we improved the way connection costs are calculated to give Shopify developers more GraphQL throughput.

We switched from a linear progression to a more logarithmic shape, which significantly reduces the cost of connections that return a high number of objects, like in your example.

Thank you for your feedback, and I hope this helps!
Guilherme Vieira

Hi Guilherme,

Thank you for clarifying the recent changes to the cost calculation. Could you please share more details about the current algorithm or formula used to determine the query cost for connections? For example, is there documentation or an example that explains how the logarithmic progression is applied?

Understanding the specifics would really help us optimize our queries more effectively.

Thanks again for your support!

Best regards,
Ulan

Hi again Ulan,

I’ve dug into this a bit on my side and theres a few factors that can influence the costs. For example its not exactly 1pt per object as for connections we use a logarithmic scale. EG:

query {
  products(first: 100) {
    nodes {
     id
    }
  }
}

This query has 1 connection and an estimated costs of 11pts if it returns 100 products. The actual cost will depend on how many objects are actually returned.
If you want to test or debug how things work, you should use the GraphiQL app:

Thank you for the clarification, Liam.

I’m particularly interested in understanding the exact algorithm or formula Shopify uses to calculate the requestedQueryCost before execution - not just the actualQueryCost after execution. Could you provide more details or documentation on how the requestedQueryCost is determined, especially in light of the recent (Winter Editions 24) changes to the cost calculation model?

Is there any update on the documentation or details regarding the current algorithm for calculating requestedQueryCost in the Shopify Admin API?

Hi @Ulan_Omurkulov

I don’t believe we publish the exact formula or per-field cost table, your best option is to rely on info from here: Shopify API limits

I’m honestly quite frustrated by this situation. It doesn’t feel fair that the query cost calculation algorithm is kept hidden from developers. Has anyone in the community tried to reverse engineer how the query cost is actually calculated? If so, I’d love to hear about your experience or any insights you’ve uncovered.

The calculation can be different depending on the API - is there a reason why you can’t test using the GraphiQL app?

Hi Liam Im here again :)! Yes, I am using GraphQL. Could you please share the exact formula or details behind the logarithmic cost calculation for connections? I’d really appreciate a clearer understanding of how these costs are determined.

Thanks, Liam. I’ll wait for your response on the logarithmic cost details for connections and any clarifications on requestedQueryCost. Appreciate the help.