Lesson 6 of 9 · 24m
Errors, Query Cost, and Resolver Performance
How to design user-facing errors, protect the graph with cost analysis, and measure resolver performance without drowning in noise.
A GraphQL API can return HTTP 200 while completely failing. That is not a bug in the spec; it is a consequence of partial success. The error contract therefore matters more than in a simple REST endpoint. Clients need to know which part of the graph is usable, operators need enough detail to debug, and attackers must not receive a free architecture diagram.
Separate transport errors, graph errors, and domain errors
Transport failures are HTTP-level: malformed JSON, missing auth token, rate limit. GraphQL execution errors appear in the top-level errors array and often null part of data. Domain failures are expected business outcomes and often belong in mutation payloads.
{
"data": { "workspace": { "name": "Acme", "invoices": null } },
"errors": [
{
"message": "Invoices are temporarily unavailable",
"path": ["workspace", "invoices"],
"extensions": { "code": "DEPENDENCY_UNAVAILABLE" }
}
]
}The path tells the client exactly what failed. Internal details — SQL text, stack traces, hostnames — belong in logs, not responses.
Mutation errors should be typed
A mutation payload union or structured error list lets clients render correct recovery behavior:
type UserError {
code: ErrorCode!
message: String!
field: String
}
enum ErrorCode {
VALIDATION_FAILED
NOT_FOUND
FORBIDDEN
CONFLICT
DEPENDENCY_UNAVAILABLE
}Do not make clients parse human-readable strings. Messages are for people; codes are for programs.
Cost analysis before execution
Depth limiting alone is insufficient. A depth-three query can request thousands of expensive nodes. Assign costs to fields and multipliers to list arguments:
const cost = analyzeQuery(document, schema, {
scalarCost: 1,
objectCost: 2,
listMultiplier: true,
maxCost: 5000,
maxDepth: 12,
});A connection with first: 100 and nested connections multiplies quickly:
query Expensive {
workspaces(first: 100) {
edges {
node {
members(first: 100) {
edges { node { invoices(first: 100) { edges { node { id } } } } }
}
}
}
}
}That is a potential million-node response. Reject or throttle it before execution, not after the database has done the work.
Persisted operations reduce ambiguity
In trusted first-party clients, persisted queries replace arbitrary operation text with known hashes. Benefits: smaller requests, stable metrics, easier cost accounting, and a strong security boundary when arbitrary queries are disabled in production. They are not a substitute for authorization, but they do eliminate whole classes of query-shape abuse.
Measure resolver work, not just request latency
Useful per-request metrics include operation name/hash, total cost, field error count, loader batch sizes, database query count, cache hit ratio, and timings by resolver path. High-cardinality paths can be noisy, so aggregate carefully. A p95 of 200 ms with occasional 9-second dependency stalls means the mean dashboards are comforting lies.
Field notes
- Never expose internal exception messages directly; map them to safe public categories.
- A validation failure in an input object is a client error and should be cheap to understand.
- Rate limiting by IP ignores authenticated abuse; rate limit by actor, token, tenant, and cost.
- Complexity budgets need escape valves for internal jobs, or teams will route around them.
- A resolver that is always fast in tests can still batch poorly under real fan-out; test nested queries, not scalar fields only.
Was this lesson helpful?