Privacy and Security

GraphQL Query Cost and Depth: Protecting Your API From Heavy Queries

How nested lists make GraphQL queries expensive, how depth and cost limits work, and how to audit a schema for unpaginated lists and circular types.

Saizul Amin
Saizul Amin3 Nov 2026 · 3 min read
GraphQL Query Cost and Depth: Protecting Your API From Heavy Queries

GraphQL's flexibility is its best feature and its biggest operational risk. A client can ask for exactly the fields it needs, which also means it can ask for a nested tree that costs the server millions of database calls. This guide explains where that cost comes from, how depth and cost limits contain it, and how to measure queries and review schemas with the GraphQL Query Complexity and Schema Security Auditor.

Where the cost comes from

Every list field multiplies everything below it. Ask for 20 reviews, each with its author, each author with their reviews, each with its product, and the short query becomes hundreds of product lookups. Add one more level and it reaches thousands. Circular relationships make this unlimited: if Product has reviews and Review has a product, a client can nest them as deep as it likes.

Depth versus cost

A depth limit caps how many levels a query may nest. It is simple and blocks the most obvious abuse, but it treats a deep query over single objects the same as a shallow query over huge lists.

A cost limit estimates the work. A common model gives every object field a cost of 1 and multiplies by the requested list size. The auditor follows that model: list sizes come from arguments such as first or limit, and when the argument is missing or a variable, an assumed size you choose is used. You can also give scalar fields a cost if your resolvers make them expensive.

Most production servers use both: a depth limit of roughly 7 to 10 and a cost limit tuned to real traffic.

Measuring a query

Paste your schema, as SDL or an introspection JSON result, and a query with any fragments. The auditor checks every field against the schema and reports unknown fields, missing selection sets and broken fragments with line numbers. It then lists every field with its depth, multiplier and cost, and totals them against your limits. In the sample product page query, a single reviews(first: 20) with a nested reviews list produces 200 product lookups, and the total cost is 253 at depth 6.

Use this on the queries your apps actually send. It shows which queries would be rejected by the limits you plan, and which fields are worth adding pagination to.

Auditing the schema

The schema tab looks for designs that make expensive queries possible:

  • Unpaginated lists, fields that return lists of objects without a first or limit argument. On the root query type these are high risk because one request can ask for every row.
  • Pagination without defaults, which means a missing argument might return everything unless the resolver enforces a maximum.
  • Circular relationships between types, and types that reference themselves.
  • Secret-looking fields such as passwordHash or apiKey that should never be in a public graph.
  • Free-text query arguments on mutations that need strict server-side validation.
  • No cost or rate-limit directives, a reminder that limits must then exist in middleware.

The findings become a score so you can track improvements.

Introspection

Introspection lets anyone ask a GraphQL server for its full schema. It is invaluable during development and for public APIs, but for private APIs it hands an attacker a complete map. If you obtained your schema by sending an introspection query to production, introspection is enabled there. Many teams switch it off in production and publish the schema through documentation instead.

Enforcing limits

The server tab shows example validation rules for a Node server with your chosen depth and cost limits. Pair them with maximum values for pagination arguments in resolvers, request timeouts and per-client rate limits. Persisted queries, where the server only runs pre-registered operations, are the strongest control for first-party apps.

Limits

The cost model is an estimate based on the schema and query text. It does not measure your resolvers, does not evaluate include or skip directives and handles interfaces and unions by fragment type only. The tool never sends anything to a GraphQL endpoint.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages