Mock GraphQL-Style JSON Response
Generate a mock response following GraphQL conventions with a data wrapper and optional errors array. Test GraphQL client integrations.
Detailed Explanation
GraphQL-Style Response
GraphQL responses follow a specific structure defined by the GraphQL specification. Even if you are building a REST API, understanding this pattern is valuable because many modern APIs adopt similar conventions.
Response Structure
{
"data": {
"user": {
"id": "uuid",
"name": "string",
"email": "email",
"posts": [
{
"id": "uuid",
"title": "string",
"comments": [
{ "id": "uuid", "body": "string" }
]
}
]
}
}
}
GraphQL Conventions
| Field | Description |
|---|---|
data |
The successful response payload; matches the shape of the query |
errors |
Optional array of error objects when the query partially or fully fails |
extensions |
Optional metadata (query complexity, execution time, etc.) |
Partial Errors
Unlike REST where a response is either success or error, GraphQL can return both data and errors simultaneously. For example, a query for user data and their posts might succeed for the user but fail for the posts:
{
"data": {
"user": { "id": "123", "name": "Alice" },
"posts": null
},
"errors": [
{ "message": "Posts service unavailable", "path": ["posts"] }
]
}
Testing GraphQL Clients
This mock is useful for testing Apollo Client, urql, or Relay integrations. You can verify that your client correctly handles the nested data structure, updates the cache, and processes partial errors.
Use Case
Developers migrating from REST to GraphQL or building a BFF (Backend for Frontend) layer can use this mock to prototype the response shape and test their GraphQL client configuration.