GraphQL error format shows problems clearly when something goes wrong in a query. It helps developers fix issues fast.
GraphQL error format
Introduction
Syntax
GraphQL
{
"errors": [
{
"message": "Error message here",
"locations": [
{ "line": 2, "column": 3 }
],
"path": ["fieldName"],
"extensions": {
"code": "ERROR_CODE",
"details": "Additional info"
}
}
]
}The errors field is an array because there can be many errors.
message explains what went wrong in simple words.
Examples
GraphQL
{
"errors": [
{
"message": "Cannot query field 'age' on type 'User'.",
"locations": [{ "line": 3, "column": 5 }],
"path": ["user", "age"],
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED"
}
}
]
}GraphQL
{
"errors": [
{
"message": "Field 'email' is required.",
"locations": [{ "line": 2, "column": 3 }],
"path": ["createUser"],
"extensions": {
"code": "BAD_USER_INPUT"
}
}
]
}Sample Program
This query asks for 'name' and 'age' of a user. The server replies with an error because 'age' is not a valid field.
GraphQL
query {
user(id: "123") {
name
age
}
}
/*
Server response:
{
"errors": [
{
"message": "Cannot query field 'age' on type 'User'.",
"locations": [{ "line": 4, "column": 5 }],
"path": ["user", "age"],
"extensions": {
"code": "GRAPHQL_VALIDATION_FAILED"
}
}
]
}
*/Important Notes
The locations help find the exact spot in the query where the error happened.
The path shows which part of the query caused the error.
Extensions provide extra info like error codes to help handle errors programmatically.
Summary
GraphQL errors are returned in a clear, structured format.
Errors include message, location, path, and optional extensions.
This format helps developers quickly find and fix problems in queries.
Practice
1. What is the main purpose of the
message field in a GraphQL error response?easy
Solution
Step 1: Understand the role of the message field
Themessagefield clearly states the error description to help identify the problem.Step 2: Compare with other fields
Other fields likelocationshow where the error happened, butmessageexplains what the error is.Final Answer:
To describe what went wrong in the query -> Option AQuick Check:
message = error description [OK]
Hint: Message always explains the error clearly [OK]
Common Mistakes:
- Confusing message with location
- Thinking message holds query data
- Assuming message is optional
2. Which of the following is the correct structure of a GraphQL error object?
easy
Solution
Step 1: Recall GraphQL error object format
GraphQL errors includemessage,locationas an array of line and column objects, andpathas an array of strings.Step 2: Check each option's structure
Only { message: string, location: [{ line: int, column: int }], path: [string] } matches the correct structure with proper types and nesting.Final Answer:
{ message: string, location: [{ line: int, column: int }], path: [string] } -> Option DQuick Check:
Correct error object format = { message: string, location: [{ line: int, column: int }], path: [string] } [OK]
Hint: Look for location as array of line/column objects [OK]
Common Mistakes:
- Using location as a string
- Confusing path type with string instead of array
- Missing array brackets for location
3. Given this GraphQL error response snippet:
What does the
{"errors": [{"message": "Cannot query field 'age' on type 'User'", "locations": [{"line": 3, "column": 5}], "path": ["user", "age"]}]}What does the
path field indicate?medium
Solution
Step 1: Understand the path field meaning
Thepathshows the chain of fields in the response data that led to the error.Step 2: Analyze the example path
Here, the error is in the 'age' field inside the 'user' object, so path is ["user", "age"].Final Answer:
The sequence of fields leading to the error in the response data -> Option BQuick Check:
path = error location in response data [OK]
Hint: Path shows field chain causing error [OK]
Common Mistakes:
- Thinking path is query text location
- Confusing path with server file path
- Ignoring path as array of fields
4. You receive this GraphQL error response:
What is the most likely cause of this error?
{"errors": [{"message": "Syntax Error: Expected Name, found }", "locations": [{"line": 5, "column": 10}]}]}What is the most likely cause of this error?
medium
Solution
Step 1: Interpret the error message
The message says "Syntax Error: Expected Name, found }", indicating a syntax mistake in the query.Step 2: Understand syntax errors in GraphQL
Such errors happen when brackets, names, or punctuation are missing or misplaced in the query text.Final Answer:
A missing or extra character in the query causing invalid syntax -> Option AQuick Check:
Syntax error = query typo [OK]
Hint: Syntax errors mean query text is malformed [OK]
Common Mistakes:
- Assuming error is due to auth issues
- Confusing syntax error with server crash
- Ignoring location info in error
5. You want to add extra details to your GraphQL error responses for better debugging. Which field should you use to include custom error codes or metadata?
hard
Solution
Step 1: Identify the field for extra error info
Theextensionsfield is designed to hold custom metadata like error codes or debugging info.Step 2: Differentiate from other fields
messageis for error text,pathshows error location in data, andlocationsshows query text position.Final Answer:
extensions -> Option CQuick Check:
extensions = custom error metadata [OK]
Hint: Use extensions for custom error details [OK]
Common Mistakes:
- Putting custom info in message
- Confusing path with metadata
- Ignoring extensions field existence
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
