0
0
GraphQLquery~10 mins

GraphQL error format - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - GraphQL error format
Client sends GraphQL query
Server processes query
Error occurs?
NoReturn data response
Yes
Create error object
Include error fields: message, locations, path, extensions
Return error response to client
The server processes a GraphQL query and if an error occurs, it creates a structured error object with specific fields and returns it to the client.
Execution Sample
GraphQL
{
  user(id: "123") {
    name
    age
  }
}

// Error response example
A GraphQL query requesting user data, with an example error response returned if user not found.
Execution Table
StepActionEvaluationResult
1Receive query from clientQuery with user(id: "123")Start processing query
2Resolve user fieldCheck if user with id 123 existsUser not found
3Create error objectBuild error with message and location{"message": "User not found", "locations": [{"line": 2, "column": 3}], "path": ["user"]}
4Return responseInclude errors array in response{"errors": [{"message": "User not found", "locations": [{"line": 2, "column": 3}], "path": ["user"]}], "data": {"user": null}}
5EndResponse sent to clientExecution complete
💡 Error detected during field resolution, error object created and returned in response
Variable Tracker
VariableStartAfter Step 2After Step 3Final
querynull{ user(id: "123") { name age } }{ user(id: "123") { name age } }{ user(id: "123") { name age } }
userDatanullnull (user not found)null (user not found)null (user not found)
errorObjectnullnull{"message": "User not found", "locations": [{"line": 2, "column": 3}], "path": ["user"]}{"message": "User not found", "locations": [{"line": 2, "column": 3}], "path": ["user"]}
responsenullnullnull{"errors": [{"message": "User not found", "locations": [{"line": 2, "column": 3}], "path": ["user"]}], "data": {"user": null}}
Key Moments - 3 Insights
Why does the error response include both 'errors' and 'data' fields?
The 'errors' field contains details about what went wrong, while the 'data' field shows partial or null data. This allows clients to receive as much valid data as possible even if some parts failed, as seen in step 4 of the execution_table.
What is the purpose of the 'locations' field in the error object?
The 'locations' field shows where in the query the error happened (line and column). This helps developers quickly find and fix the problem, as shown in step 3 of the execution_table.
Why is the 'path' field included in the error object?
The 'path' field indicates which part of the query caused the error, helping clients understand which field failed. This is demonstrated in step 3 where the path is ["user"].
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'userData' after step 2?
AError object
BUser object with id 123
Cnull (user not found)
DQuery string
💡 Hint
Check the variable_tracker row for 'userData' after step 2
At which step is the error object created?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the execution_table action 'Create error object'
If the user was found successfully, how would the response differ at step 4?
AErrors array would contain error message
BErrors array would be empty and data would contain user info
CData field would be null
DResponse would be empty
💡 Hint
Consider the purpose of 'errors' and 'data' fields in the response
Concept Snapshot
GraphQL error format:
- Errors returned in 'errors' array
- Each error has 'message', 'locations', 'path'
- 'locations' shows query line/column
- 'path' shows field causing error
- 'data' may contain partial results
- Enables clients to handle errors gracefully
Full Transcript
When a GraphQL query is sent, the server processes it. If an error occurs, such as a missing user, the server creates an error object with a message, location in the query, and the path to the field that caused the error. This error object is included in an 'errors' array in the response. The 'data' field may still contain partial data or null for the failed field. This structured error format helps clients understand what went wrong and where, allowing better debugging and user experience.