Testing GraphQL Error Handling and Validation

GraphQL error handling differs from typical REST patterns because errors are returned in a structured errors array and may coexist with partial data. Testers need to verify not only that errors occur when they should, but also that they are reported clearly and safely without leaking internal details.

GraphQL Errors, Extensions, and Partial Data

When something goes wrong, GraphQL servers populate the errors array with objects that include messages, locations, and sometimes extensions with additional metadata. In many cases, data may still contain successfully resolved fields alongside errors. Tests should assert on the presence, shape, and content of these error objects.

{
  "data": {
    "customer": null
  },
  "errors": [
    {
      "message": "Customer not found",
      "path": ["customer"],
      "extensions": { "code": "NOT_FOUND" }
    }
  ]
}
Note: Business validation failures, permission issues, and server errors can all appear in the errors array. Tests should distinguish between expected and unexpected error types.
Tip: Define standard error codes or structures in extensions and assert on them in tests, rather than brittle free-text messages.
Warning: Error messages that expose stack traces, SQL queries, or internal identifiers can create security risks. Include checks that sensitive internals are not leaked.

Validation errors often occur when required arguments are missing, types are wrong, or values violate business rules. GraphQL tooling can catch some of these at parse time, but applications also enforce domain-specific constraints that tests must cover.

Designing Error and Validation Tests

Create tests for invalid input types, missing required variables, constraint violations (such as invalid email formats), and permission errors. Verify both that the correct error codes are returned and that data is either omitted or present only where appropriate in partial responses.

Common Mistakes

Mistake 1 โ€” Ignoring partial success scenarios

Assuming all-or-nothing behaviour is incorrect in many GraphQL APIs.

โŒ Wrong: Only testing cases where everything succeeds or fails entirely.

โœ… Correct: Include tests where some fields resolve while others produce errors.

Mistake 2 โ€” Asserting only on HTTP status codes

This misses GraphQL-specific error information.

โŒ Wrong: Treating HTTP 200 as success without inspecting the errors array.

โœ… Correct: Assert on both data and errors, including error shapes and codes.

🧠 Test Yourself

What should GraphQL error handling tests focus on?