Skip to main content
GraphQL Security Best Practices

GraphQL Security Best Practices

KU
· Founder & Chief Editor
• 8 min read•Updated •

GraphQL's single flexible endpoint creates attack surfaces REST checklists miss, from introspection exposure to query depth and batching abuse.

Key takeaways

  • Secure a GraphQL API by disabling introspection in production, rejecting queries over a depth and cost limit before execution, and rate-limiting by query cost instead of request count.
  • Limit batched operations and aliases, authorize every sensitive field in its resolver, mask error details, and use persisted queries for first-party clients.

Secure GraphQL by disabling introspection in production, rejecting queries over a depth and cost limit before execution, and rate-limiting by query cost rather than request count. Cap batched operations and aliases, authorize every sensitive field in its resolver, mask error details, and accept only persisted queries from first-party clients.

Verified against

Apollo Server 5.5.1, graphql-js 17.0.2 (npm), against the Apollo Server API reference, the graphql.org security guide and the OWASP GraphQL Cheat Sheet. Checked October 2026.

GraphQL security differs from REST because one endpoint replaces dozens. A REST API spreads risk across many URLs, each with its own rate limit and access check. GraphQL collapses that into a single /graphql route where the client builds the query, so authorization, resource limits, and input validation all move from the transport layer into the query itself.

That shift is not academic. OWASP maintains a dedicated GraphQL Cheat Sheet, because the flaws that hit GraphQL deployments don't map onto REST's endpoint-by-endpoint model. Teams that apply only REST-era controls end up with a schema that documents itself to attackers and a query engine that executes whatever shape of request it receives. This post covers the risk classes that matter most, as a companion to application security best practices and API security best practices.

Why is introspection dangerous in production?

Introspection is a built-in GraphQL feature that lets a client ask the schema to describe itself: every type, field, argument, and mutation. It's genuinely useful in development, powering tools like GraphiQL and client codegen. Left enabled in production, it hands an attacker your full API surface without a single guess. They don't need to fuzz endpoints or scrape documentation. One query returns the entire object graph, including internal-sounding fields and mutations nobody meant to expose.

Fix: disable introspection in production builds, or restrict it to authenticated internal tooling behind a separate gateway. Most GraphQL servers (Apollo, GraphQL Yoga, graphql-ruby) expose a single config flag for this. Apollo Server's introspection option defaults to true unless NODE_ENV is production, so a container that forgets to set NODE_ENV ships with introspection on. Set it explicitly. The graphql.org guide also warns that disabling introspection is obscurity, not access control: field-suggestion hints in validation errors can still leak the schema. If your public API genuinely needs a browsable schema, publish it as static documentation instead of leaving the live introspection query open to anyone.

How do you prevent query depth and complexity attacks?

GraphQL's flexibility is also its denial-of-service risk. A client can nest a query arbitrarily deep, or request a field that fans out into thousands of child objects, and the server will try to resolve all of it. A query like user { friends { friends { friends { friends { name } } } } } can turn a cheap request into an exponential amount of database work. Unlike REST, where each endpoint does a fixed amount of work, a GraphQL resolver's cost depends on what the caller asks for.

Fix: set hard limits on query depth and computed complexity, and reject anything over the threshold before execution starts, not after resolvers run.

JavaScript
// server.js (Apollo Server 5): depth + cost limits, explicit production defaults
import { ApolloServer } from '@apollo/server';
import depthLimit from 'graphql-depth-limit';
import { createComplexityLimitRule } from 'graphql-validation-complexity';

const server = new ApolloServer({
  typeDefs,
  resolvers,
  introspection: false,
  includeStacktraceInErrorResponses: false,
  allowBatchedHttpRequests: false,
  validationRules: [
    depthLimit(6), // reject queries nested deeper than 6 levels
    createComplexityLimitRule(1000, {
      // assign per-field cost so paginated/list fields
      // count more than scalar fields
      scalarCost: 1,
      objectCost: 2,
      listFactor: 10,
      onCost: (cost) => console.log('query cost:', cost),
    }),
  ],
});

Depth limits catch nested recursion; complexity (cost analysis) catches wide fan-out that touches a large number of records without being deep. Run both. A query requesting 5,000 items at depth 2 is just as damaging as one nested 20 levels deep. One maintenance note: graphql-validation-complexity has not shipped since 0.4.2 in 2020, so for variable-aware cost (a first: $n argument that changes the cost) look at graphql-query-complexity, which released 2.0.0 in July 2026 and computes cost per request.

What makes batching attacks a GraphQL-specific problem?

GraphQL supports sending an array of operations in a single HTTP request. That's a legitimate feature for reducing round trips, and it's also a way to route around defenses built for one-request-per-action. A rate limiter tuned for REST typically counts HTTP requests. An attacker who batches 500 login mutations into one POST slips past that counter entirely, brute-forcing credentials while showing up in logs as a single request. Apollo Server ships with allowBatchedHttpRequests set to false, but that only blocks array batching. Aliases do the same thing inside one operation: a1: login(...), a2: login(...) through a500 is a single valid query.

Fix: rate-limit by query cost, not request count. A batch of 500 mutations should register as 500 units of work against the limiter, not one. Cap the number of operations and aliases allowed per request, with tight limits on sensitive mutations like login and password reset, and keep batching disabled on public-facing schemas where it isn't a real client need.

Why does field-level authorization matter more in GraphQL?

REST authorization usually lives at the endpoint: check the token, check the role, return the resource. GraphQL breaks that model because one query can traverse many types in a single request. It's straightforward to authorize the top-level viewer or order query and then forget that a nested field, say order.customer.ssn, is reachable through a path that never passes through the check you wrote. The gap isn't in the query root, it's three levels deep in a field nobody thought to guard.

Fix: push authorization down to the resolver or field level, not just the query root. Every resolver returning sensitive data should independently verify the caller is allowed to see it, regardless of which parent query reached it. Directive-based authorization (@auth(requires: ADMIN) on the field itself) keeps the check next to the data it protects, instead of scattered across query handlers where it's easy to miss on new fields.

How much can verbose GraphQL errors leak?

GraphQL's error format is more expressive than a typical REST error body, and that's part of the problem. A misconfigured server will return stack traces, resolver file paths, or SQL fragments in the errors array when a query fails, and because errors describe the schema shape, they can leak field and type names an attacker hasn't discovered yet.

Fix: strip stack traces and internal details from error responses, and log the full detail server-side instead. Most GraphQL servers support a formatError (or equivalent) hook for this; in Apollo Server, includeStacktraceInErrorResponses defaults to true unless NODE_ENV is production or test, which is another reason to set it explicitly. For anything customer-facing, return a generic message and an error code, and keep diagnostic detail out of the response body entirely.

Locking the schema down: persisted queries

The strongest defense for a public-facing GraphQL API is to stop accepting arbitrary queries at all. Persisted queries: the client sends a hash of a pre-approved query instead of the query text, and the server only executes queries on its allowlist. Combined with disabled introspection, this turns GraphQL's biggest attack surface, the ability to ask for anything, into a fixed, auditable set of known operations. The graphql.org guide calls these trusted documents and notes they cannot serve a public API, because third-party operations aren't known in advance. Use them for first-party clients, and keep cost limits and field-level authorization for everything else. If you are still deciding whether GraphQL is worth this surface at all, our GraphQL vs REST comparison covers the trade, and API rate limiting covers the budget mechanics behind cost-based limits.

The call we'd make

Treat GraphQL like what it is: a query language sitting on top of your data graph, not a REST API with extra syntax. Disable introspection outside development, enforce depth and complexity limits before execution, rate-limit by cost rather than request count, and move authorization checks down to the field level. If the API serves your own clients, adopt persisted queries and stop accepting arbitrary schema traversal from the open internet. Each is a small, bounded change. Skipping any one turns your schema into documentation handed to the attacker for free.

Sources

Explore topics:SecurityDevOps
React
Share this post
Cite this article
Plain text

Kiril Urbonas (2026). GraphQL Security Best Practices. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/graphql-security-best-practices

Markdown

[GraphQL Security Best Practices](https://www.devopsness.com/blog/graphql-security-best-practices) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)

KU

Kiril Urbonas

Founder & Chief Editor

607 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.