Protect GraphQL APIs from deep and expensive queries using depth and complexity limits
npm install graphql-safe-guards!CI
!npm
!downloads
!license
!typescript
Protect your GraphQL API from deep, expensive, and abusive queries
by enforcing query depth and query complexity limits before execution.
A tiny, framework-agnostic utility that combines depth limiting and
query complexity validation using native GraphQL validation rules.
> Designed for production GraphQL APIs, public endpoints, and high-traffic systems.
---
GraphQL gives clients a lot of power β sometimes too much.
Without safeguards, a single query can:
- Exhaust database connections
- Cause CPU spikes
- Trigger accidental or malicious DoS attacks
- Degrade performance for all users
This is especially dangerous in public APIs or high-traffic applications.
---
graphql-safe-guards acts as a logical firewall for GraphQL queries.
Before any resolver executes, it:
- Validates query depth
- Calculates query complexity
- Rejects unsafe operations early
No resolvers are executed.
No database calls are made.
---
- β
Limit query depth
- β
Limit query complexity
- β
Blocks abusive or accidental DoS-style queries
- β
Uses native GraphQL validation (AST-based)
- β
No schema directives
- β
No runtime execution cost
- β
Framework-agnostic
- β
Production-ready presets
---
``bash`
npm install graphql-safe-guardsor
yarn add graphql-safe-guards
---
`ts
import { ApolloServer } from "@apollo/server";
import { createSafeGuards } from "graphql-safe-guards";
const server = new ApolloServer({
schema,
validationRules: createSafeGuards({
depth: 3,
complexity: 10,
}),
});
`
Any query exceeding the limits will be rejected before execution.
graphql-safe-guards includes opinionated, production-ready presets for common use cases.
`ts`
createSafeGuards({ preset: "strict" });
createSafeGuards({ preset: "balanced" });
createSafeGuards({ preset: "relaxed" });
| Preset | Depth | Complexity | Use case |
| -------- | ----- | ---------- | --------------------------- |
| strict | 3 | 50 | Public APIs, read-only APIs |
| balanced | 4 | 100 | Private frontends |
| relaxed | 6 | 200 | Admin / internal tools |
Environment-based setup
`ts`
createSafeGuards({
preset: process.env.NODE_ENV === "production" ? "strict" : "balanced",
});
`ts
createSafeGuards({
depth?: number; // default: 3 (strict preset)
complexity?: number; // default: 50 (strict preset)
/**
* If true, GraphQL introspection queries are ignored
* by depth and complexity validation.
*
* Useful for GraphQL Playground / Apollo Sandbox.
*/
ignoreIntrospection?: boolean; // default: false
});
`
---
- This library does not enable or disable GraphQL introspection
- Introspection is controlled by your GraphQL server (e.g. Apollo Server)
For private APIs with documentation enabled, the recommended setup is:
`ts`
const server = new ApolloServer({
schema,
introspection: true,
validationRules: createSafeGuards({
depth: 3,
complexity: 10,
ignoreIntrospection: true,
}),
});
---
Deeply nested queries
`ts`
a {
b {
c {
d {
e {
f {
g
}
}
}
}
}
}
High-cost queries
`ts`
posts(limit: 100) {
comments(limit: 100) {
author {
posts(limit: 100)
}
}
}
---
- Parses the GraphQL AST
- Computes depth and complexity
- Runs during the validation phase
- Blocks unsafe queries before execution
Fast, predictable, and scalable.
---
This package composes and unifies:
- graphql-safe-depthgraphql-complexity-validation
-
Validated through integration tests using native graphql-js validation.
---
- Apollo Server
- GraphQL Yoga
- Envelop
- NestJS GraphQL
- Plain graphql-js
---
- For best results, combine this library with:
- DataLoader (avoid N+1 queries)
- Pagination on list fields
- HTTP caching / CDN
- Persisted queries (hash-based)
---
- β
Combine depth + complexity validation
- β
Presets support (strict, balanced, relaxed)graphql-js`
- β
Backward-compatible API
- β
Integration tests with
- π Additional presets based on community feedback
> Roadmap items may change based on feedback and real-world usage.
If this library helped you secure your GraphQL API,
please consider giving it a β on GitHub.
MIT Β© Mateo Diaz