How does GraphQL handle errors compared to REST?
Learn how GraphQL handles errors with a 200 response and errors array plus partial data, versus REST status codes — including path and extensions.code patterns.
Expected Interview Answer
GraphQL almost always returns HTTP 200 and reports problems inside a top-level errors array alongside a partial data object, whereas REST signals failure through HTTP status codes (4xx/5xx) with the error typically in the response body and no partial success.
Because a GraphQL response can resolve some fields and fail others, the spec defines a response shape with both data and errors, letting clients receive partial results. Each error entry carries a message, path, locations and an extensions object for machine-readable codes. REST instead maps each request to a single status: 404 for not found, 401 for auth, 500 for server errors, and clients branch on that status. Many GraphQL servers still use extensions.code (e.g. UNAUTHENTICATED) to give REST-like semantics without abandoning the 200-with-errors model.
- Partial data can be returned even when some fields fail
- Errors carry a precise path to the failing field
- extensions allow structured, machine-readable error codes
- One consistent response shape across all operations
- Field-level granularity instead of a single request-level status
AI Mentor Explanation
In REST, a rained-off match just gets one verdict — 'abandoned' — and you get nothing else. In GraphQL, the umpires hand you the scorecard as far as play got plus a note listing exactly which innings was interrupted and why, so you still keep the runs already scored while knowing precisely which part of the match failed to complete.
Step-by-Step Explanation
Step 1
Understand the response shape
A GraphQL response is an object with optional data and optional errors keys defined by the spec.
Step 2
Return partial data
Fields that resolve successfully stay in data; a failed field becomes null and its failure is recorded in errors.
Step 3
Read the error path
Each error's path array points to the exact field in the query tree that failed.
Step 4
Use extensions for codes
Put machine-readable info in extensions.code (e.g. UNAUTHENTICATED, BAD_USER_INPUT) for clients to branch on.
Step 5
Contrast with REST
In REST you inspect the HTTP status and body; GraphQL keeps status 200 and shifts semantics into the errors array.
What Interviewer Expects
- Knowledge that GraphQL usually returns HTTP 200 with an errors array
- Understanding of partial data responses
- Awareness of error fields: message, path, locations, extensions
- Ability to compare with REST status-code semantics
- Knowledge of extensions.code for structured error handling
Common Mistakes
- Assuming GraphQL uses HTTP status codes the way REST does
- Believing an error means data is always null
- Ignoring the extensions object for error codes
- Not distinguishing field-level errors from request-level (transport) errors
- Forgetting that partial success is possible in one response
Best Answer (HR Friendly)
“REST tells you something went wrong through the response's status code, like a 404 or 500, and usually the whole request either works or fails. GraphQL instead almost always returns a normal success status and puts any problems in a separate errors list, so you can still receive the parts of the answer that worked along with a precise note about which part failed.”
Code Example
// A single GraphQL HTTP 200 response can carry both:
{
"data": {
"user": { "id": "42", "name": "Ada" },
"orders": null
},
"errors": [
{
"message": "Not authorized to view orders",
"path": ["orders"],
"locations": [{ "line": 4, "column": 3 }],
"extensions": { "code": "UNAUTHENTICATED" }
}
]
}
// Client branches on the machine-readable code, not HTTP status
const res = await fetch('/graphql', { method: 'POST', body });
const { data, errors } = await res.json();
if (errors?.some(e => e.extensions?.code === 'UNAUTHENTICATED')) {
redirectToLogin();
}
render(data.user); // partial data still usableFollow-up Questions
- What fields does a GraphQL error object contain?
- How do you return a machine-readable error code from a resolver?
- What is the difference between transport errors and GraphQL errors?
- When would a GraphQL server actually return a non-200 status?
- How do you handle partial data on the client?
MCQ Practice
1. What HTTP status does a typical GraphQL server return when a single field's resolver throws?
GraphQL usually returns 200 and reports the failure in the errors array, keeping any successfully resolved fields in data.
2. Which field on a GraphQL error identifies the exact failing field?
The path array points to the specific field in the query tree that failed.
3. Where should a server put a machine-readable error code like UNAUTHENTICATED?
The extensions object carries structured, machine-readable metadata such as extensions.code.
Flash Cards
What status does GraphQL usually return on a resolver error? — HTTP 200, with details in the top-level errors array.
Can GraphQL return data and errors together? — Yes — partial data plus an errors array in the same response.
What are the standard GraphQL error fields? — message, locations, path and extensions.
How does REST differ? — REST signals failure via HTTP status codes (4xx/5xx) with no partial success.
Continue Learning
Related Interview Questions
What is GraphQL and how does it differ from REST?
easy
What is the difference between over-fetching and under-fetching, and how does GraphQL address them?
medium
What is a union type in GraphQL and when should you use it?
medium
What is a GraphQL schema and what is the Schema Definition Language (SDL)?
medium