GraphQL Security Best Practices and Tooling
What you'll learn: How to harden your GraphQL API using persisted queries, allowlisting, automated scanning, and layered security strategies.
The Core Idea
By now you understand GraphQL's vulnerabilities—introspection leaks, depth attacks, authorization flaws. Now it's time to build a comprehensive defense. Think of this as assembling multiple security checkpoints: even if an attacker bypasses one, others remain. This "defense-in-depth" approach transforms your API from a target into a fortress.
Persisted Queries
Instead of accepting arbitrary queries from clients, persisted queries allow only pre-approved query strings identified by hash or ID. Clients send the ID, not the full query. This prevents attackers from crafting malicious queries entirely.
// Client sends:
{ "id": "q123", "variables": { "userId": 42 } }
// Server executes only the pre-registered query for "q123"
Benefit: Eliminates entire classes of injection and complexity attacks.
Query Allowlisting
Similar to persisted queries, but implemented at the validation layer. You maintain a list of permitted query patterns. Any query not matching the allowlist is rejected immediately.
Automated Security Scanning
Use tools to continuously test your GraphQL API:
- InQL (Burp Suite extension) for introspection analysis
- GraphQL Cop for static security checks
- Custom scripts to verify depth limits, cost analysis, and authorization
These tools simulate attacks automatically, catching vulnerabilities before production.
Defense-in-Depth Strategy
Layer multiple protections:
- Disable introspection in production
- Enforce query depth and complexity limits
- Implement field-level authorization
- Use persisted queries or allowlisting
- Apply rate limiting
- Sanitize error messages
- Monitor and log suspicious patterns
Each layer compensates for potential weaknesses in others.
Key Takeaway: Secure GraphQL APIs require multiple overlapping defenses—persisted queries prevent malicious input, allowlisting restricts query patterns, automated scanning catches vulnerabilities early, and defense-in-depth ensures no single failure compromises your system.