Can one GraphQL query wear out your server? Set depth limits
In GraphQL, depth and amount can be unlimited by default, which OWASP says can lead to DoS. Set depth and amount limits and timeouts, and turn off introspection and GraphiQL in production.

With REST, each URL returns one shape that you chose. With GraphQL, whoever sends the request decides the shape and depth of the answer. That is what makes it flexible, and it is also what lets one carefully written query make your server do far more work than it should. By the end of this article you will know which limits to set, and where.
What are depth and amount limits in GraphQL?
A depth limit is the largest number of nested levels the server accepts in one query, and an amount limit is the largest number of items it agrees to return from a list. Both limits reject a heavy query before it starts to run.
Why can one query wear out the server?
One query can wear out the server because depth and amount in GraphQL can be unlimited by default, so a query can ask for nested relations, one level inside another.
The OWASP GraphQL Cheat Sheet says both can be unlimited by default, which may lead to DoS, meaning the service stops for everyone. It asks you to set limits on depth and amount.
The mistake is human. A developer made the relation work both ways so the front end stays flexible: an author has posts, and each post has an author. Nobody thought someone would loop through it:
query {
author(id: "abc") {
posts {
author {
posts {
author {
posts { title }
}
}
}
}
}
}Without a depth limit, nothing stops someone from adding as many levels to this query as they like.
How do you set the limits?
You set a depth limit and an amount limit, a timeout for the request, and if you can, a cost for each query. The same OWASP cheat sheet names a JavaScript library for each:
Limit | What it does | JavaScript library per the cheat sheet |
|---|---|---|
Depth | Rejects a query nested deeper than a limit |
|
Amount | Rejects a request for more items than a limit |
|
Cost | Works out the cost of a query before running it |
|
The cheat sheet says query cost analysis is not easy to set up and may not always be needed, but it is the most thorough way to prevent DoS. It also asks for reasonable timeouts at the application layer, the infrastructure layer, or both, because timeout needs differ by API and by how the data is fetched.
Why turn off introspection and GraphiQL?
You turn them off because introspection lets whoever uses your API learn everything about it: the schema, the mutations and the deprecated fields. And it often comes enabled by default, without needing authentication.
The OWASP cheat sheet asks you to turn off introspection queries system wide in any production or publicly accessible environment, and to turn off GraphiQL and similar schema exploration tools in the same environments. This does not stop a heavy query, but it makes it harder for a stranger to find the relations to loop through.
What is a batching attack?
A batching attack packs many operations into one request, so the attacker tries many values faster and with less noise. The cheat sheet calls it a form of brute force attack specific to GraphQL.
The examples it gives: brute forcing passwords, two-factor authentication codes (OTPs) and session tokens. Its fixes are a limit on the number of operations that can be batched and run at once, and rate limits in the business logic itself. If you already protect your login page from credential stuffing, make sure it counts the operations inside a request, not only the requests.
Where do you start now?
Open your GraphQL server settings and check three things: is there a depth limit? Is introspection off in production? Is there a request timeout? Add whichever is missing today.
And if the server reads from PostgreSQL, check the heavy queries with EXPLAIN ANALYZE too.
If you want someone to test your API the way an attacker would, there is Muhassan. How deep is the deepest query your server accepts right now?
FAQ
If I turn off introspection, is my API safe?
No. Turning it off makes the schema harder to explore, but the depth and amount limits and the timeouts are what stop a heavy query.
My API is internal. Do I need this?
Yes. OWASP says that for an internal API, the easiest approach is to turn off introspection system wide.
What is a batching attack in GraphQL?
It packs many operations into one request to try many values fast, like passwords or OTP codes. The fix is a limit on operations per request, plus rate limits.


