GraphQL¶
Scope: designing, securing and running GraphQL APIs, whatever the server language. The general rules in API design also apply, in particular backward compatibility (API-2) and designing the contract first (API-4), as do Security (SEC-1 on authorization) and Authentication. For TypeScript servers, see also TypeScript and Node.js.
Entries are sorted by score from 1 to 10 (criteria): 9–10 start here or strongly recommended, 6–8 recommended, 1–5 further reading or conditional.
Adopted references¶
- 10 Learn GraphQL (GraphQL Foundation): the baseline: the core concepts, then the Best Practices lessons on thinking in graphs, serving over HTTP, authorization, pagination, schema design, error handling, performance, security, federation and schema governance.
- 9 GraphQL Specification (September 2025 edition): the language, type system, validation and execution rules that every GraphQL server implements.
- 8 GraphQL Cheat Sheet (OWASP): the baseline for security: input validation, denial of service, access control, batching attacks and insecure default configurations.
- 7 GraphQL over HTTP (GraphQL Foundation, working draft): how to serve and call GraphQL over HTTP: URLs,
GETandPOST, media types, status codes, and security notes. - 7 GraphQL Cursor Connections Specification (Relay): the standard shape for cursor-paginated lists: connections, edges,
pageInfoand thefirst,after,lastandbeforearguments.
Key rules¶
Schema design¶
- 8
GQL-1Evolve the schema without versions: add fields and types, mark what you plan to remove with@deprecated(reason: …)naming the replacement, and remove it only after usage tracking shows that clients have migrated, or the announced deadline has passed. Removing or renaming fields, changing their types, making arguments required, or making a non-null field nullable breaks clients (API-2). (GraphQL, Schema change management, Schema design: Versioning, GraphQL spec, @deprecated) - 8
GQL-2Change data only in mutations: keep query fields free of side effects, and don't execute a mutation sent withGET. The specification requires every field except top-level mutation fields to be free of side effects, and GraphQL over HTTP forbids mutations overGET. (GraphQL spec, Normal and serial execution, GraphQL over HTTP, GET) - 7
GQL-3Design the schema around the business domain and the way clients use the data, not around the database tables behind it. Agree on review criteria, such as naming, nullability and compatibility, and review every schema change against them, asAPI-4asks for any API contract. (GraphQL, Thinking in graphs, Review and validate schema changes) - 7
GQL-4Paginate every list field that can grow, with cursor-based connections that follow the Relay connection specification, and cap the page size. Opaque cursors let you change how the backend paginates later, and unbounded lists let one request fetch huge amounts of data. (GraphQL, Pagination, Relay, Cursor Connections Specification, OWASP, Query limiting) - 6
GQL-5Choose nullability field by field: keep a field nullable if it can fail or be denied on its own, make it non-null only where clients need the guarantee, and return lists as[Item!]!. When a non-null field fails, the null spreads to its nearest nullable parent, and making a non-null field nullable later is a breaking change. (GraphQL, Schema design: Nullability, Naming conventions: nullability patterns, Schema change management: breaking changes) - 6
GQL-6Return expected business errors, such as failed validation or a broken business rule, as typed results in the mutation's payload type, and keep the top-levelerrorsarray for unexpected failures. Errors in the schema are typed and discoverable, and a payload type can gain fields without breaking clients. (GraphQL, Error handling, Naming conventions: payload types)
Security¶
- 9
GQL-7Check authorization in the business logic layer that resolvers call, for every object and field a request reaches, including lookups by ID such asnode(id:)and connection edges. Checks written in individual resolvers drift apart between entry points, and knowing an object's ID doesn't mean the caller may see it. (GraphQL, Authorization, OWASP, Access control) - 8
GQL-8Limit what one request can cost: reject operations above a maximum depth, number of aliases and root fields, and number of operations in a batch, and stop execution after a timeout. For public APIs, also consider query cost analysis and rate limits based on cost. (GraphQL, Security: demand control, OWASP, DoS prevention, GraphQL Yoga, Preparing for production) - 7
GQL-9If only your own clients call the API, execute only trusted documents in production: operations registered at build time and sent by ID. Automatic persisted queries don't give this protection, because the server registers any operation a client sends. (GraphQL, Security: trusted documents, GraphQL Yoga, Persisted operations, Apollo, Differences from automatic persisted queries) - 7
GQL-10Mask unexpected errors in production, and turn off stack traces and debug output. Detailed errors and field suggestions reveal the schema and the systems behind it, even when introspection is off. (GraphQL, Security: error messages, OWASP, Secure configurations, GraphQL Yoga, Error masking) - 6
GQL-11Refuse requests that a browser can send cross-site without a CORS preflight, such as form-encoded or multipart bodies andGETrequests without a custom header, to prevent cross-site request forgery. Apollo Server does this by default, and GraphQL Yoga has a plugin for it. (GraphQL over HTTP, Exercise caution, Apollo Server, Preventing CSRF, GraphQL Yoga, CSRF prevention) - 5
GQL-12Turn off introspection and GraphiQL in production for APIs that only your own clients use, and don't count on it as protection. Public APIs usually keep introspection for their consumers' tools, perhaps only for authenticated users. Trusted documents and authorization give stronger protection than hiding the schema. (GraphQL, Introspection in production, Security: introspection, OWASP, Introspection and GraphiQL)
Performance and architecture¶
- 7
GQL-13Load data in resolvers through DataLoader or an equivalent, which batches and caches the loads within each request. Create the loaders per request, so that cached data from one user's request never appears in another's. This is how GraphQL servers, where every field has its own resolver, applyDATA-10. (GraphQL, Performance: the N+1 problem, DataLoader, Caching per request, OWASP, Server-side batching and caching) - 5
GQL-14Start with one GraphQL service, and adopt federation only when several teams need to own and deploy parts of the graph independently. Federation needs a gateway, a schema registry and a team to run them. (GraphQL, Is GraphQL federation right for you?)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 6 GraphQL Inspector
diff: compares the schema in a pull request with the current one, sorts the changes into breaking, dangerous and safe, and fails when any change is breaking. Removing a deprecated field counts as breaking unless you turn onsuppressRemovalOfDeprecatedField, andconsiderUsagechecks breaking changes against real usage. ChecksGQL-1. - 6 GraphQL ESLint: lints schema and operation files in CI and the editor.
relay-connection-types,relay-argumentsandrelay-page-infocheck that connections follow the Relay specification.require-deprecation-reason(inschema-recommended) andrequire-deprecation-datecheck deprecations, andno-deprecated(inoperations-recommended) flags client operations that still use deprecated fields. Partly checksGQL-1andGQL-4.
Tutorials and further reading¶
- 6 Tutorial: Designing a GraphQL API (Shopify): designs a schema step by step from a real domain, covering IDs, pagination, business logic and mutations.
- 5 Production Ready GraphQL (Marc-André Giroux, 2020): a book on schema design, performance, security, tooling and architecture for GraphQL APIs at scale.