0
0
GraphQLquery~10 mins

Why error handling differs from REST in GraphQL - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why error handling differs from REST
Client sends request
Server processes request
REST: HTTP status code sent
Client interprets status code
Client sends GraphQL query
Server processes query
GraphQL: HTTP 200 always
Errors included in response body
Client checks 'errors' field in response
REST uses HTTP status codes for errors, while GraphQL always returns HTTP 200 and includes errors inside the response body.
Execution Sample
GraphQL
query {
  user(id: "123") {
    name
    email
  }
}

Response:
{
  "data": null,
  "errors": [{"message": "User not found"}]
}
A GraphQL query requesting user data returns HTTP 200 but includes an error message inside the response body.
Execution Table
StepActionHTTP StatusResponse BodyClient Handling
1Client sends GraphQL query200{}Wait for response
2Server processes query200{"data": null, "errors": [{"message": "User not found"}]}Check 'errors' field
3Client receives response200{"data": null, "errors": [{"message": "User not found"}]}Detect error from 'errors' field, not status code
4Client displays error message200{"data": null, "errors": [{"message": "User not found"}]}Show error to user
5End200Response fully handledStop processing
💡 Execution stops after client handles error inside response body despite HTTP 200 status
Variable Tracker
VariableStartAfter Step 2After Step 3Final
HTTP StatusN/A200200200
Response Body{}{"data": null, "errors": [{"message": "User not found"}]}{"data": null, "errors": [{"message": "User not found"}]}Handled error inside response
Client Error Detectedfalsefalsetruetrue
Key Moments - 2 Insights
Why does GraphQL always return HTTP 200 even when there is an error?
GraphQL returns HTTP 200 because errors are included inside the response body under the 'errors' field, so the HTTP status does not indicate error presence. See execution_table step 2 and 3.
How does the client know there is an error in GraphQL response?
The client checks the 'errors' field inside the response body, not the HTTP status code. This is shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the HTTP status code when the error message is returned?
A404
B200
C500
D400
💡 Hint
Check the 'HTTP Status' column at step 2 and 3 in the execution_table.
At which step does the client detect the error in the GraphQL response?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Client Handling' column in execution_table where the client checks the 'errors' field.
If GraphQL used HTTP status codes for errors like REST, how would the HTTP status change in the table?
AIt would change to 404 or 500 on error
BIt would be 100
CIt would remain 200
DIt would be 302
💡 Hint
Compare GraphQL's approach with REST's use of HTTP status codes for errors.
Concept Snapshot
GraphQL error handling differs from REST:
- REST uses HTTP status codes (e.g., 404, 500) to indicate errors.
- GraphQL always returns HTTP 200.
- Errors are included inside the response body under 'errors' field.
- Clients must check 'errors' field to detect problems.
- This allows partial data with errors in one response.
Full Transcript
In REST APIs, errors are indicated by HTTP status codes like 404 or 500. The client checks these codes to know if something went wrong. In GraphQL, the server always returns HTTP 200 status even if there are errors. Instead, errors are included inside the response body in a special 'errors' field. The client must look inside the response body to detect errors. This difference means GraphQL can return partial data along with errors in one response. The execution table shows the client sending a query, receiving HTTP 200 with an error message inside the body, and then detecting the error by checking the 'errors' field. This approach requires clients to handle errors differently than in REST.