Format a GraphQL Response
GraphQL responses always follow a predictable envelope with a data field and an optional errors array. This example shows a response from a user query including partial data alongside an error. Formatting the response makes it easy to identify which resolvers failed and what data was still returned. Use this when debugging GraphQL queries in development or auditing production responses.
Example
{
"data": {
"user": {
"id": "usr_99",
"name": "Alex Chen",
"posts": [
{ "id": "p1", "title": "Getting Started with GraphQL" },
{ "id": "p2", "title": "Optimising Resolvers" }
]
}
},
"errors": [
{
"message": "Cannot read field 'avatar' on null",
"path": ["user", "avatar"],
"locations": [{ "line": 4, "column": 5 }]
}
]
}FAQ
- Why can a GraphQL response have both data and errors?
- GraphQL allows partial responses. If one field resolver fails, it returns null for that field and adds an entry to the errors array while still returning valid data for all other fields.
- What does the path field in a GraphQL error mean?
- path is an array of field names showing exactly which field in the query caused the error, making it easy to pinpoint the failing resolver.
- How do I format a GraphQL query string?
- For formatting the query itself (not the JSON response), use the GraphQL Formatter tool which understands GraphQL syntax and indentation.
Related Examples
Format a REST API Response
REST APIs return compact JSON that is hard to read at a glance. Paste this examp...
Format an API Error ResponseStructured error responses help API clients handle failures gracefully, but they...
Explore Deeply Nested JSONDeep nesting is common in configuration files, CMS payloads, and analytics event...