GraphQL API testing: permissions, errors and query complexity
Updated:
When testing a GraphQL API, check the selected data, permissions, errors, and query cost. A successful HTTP status alone does not confirm that the client received the correct result. GraphQL lets the client choose fields and related objects, so the same endpoint can serve very different requests. The test suite should cover specific paths to data and situations that the user interface does not offer at all.
Why user interface tests are not enough
A mobile application may request only an order’s name, while a partner integration also requests its items and billing details. Both use the same API but select different fields. An error in a nested object may therefore affect only one client.
There is also a risk when the server checks permission for the main object but overlooks related data. A customer could then send a custom query that follows an object relationship to someone else’s order. Basic API checks remain necessary, but extend them for GraphQL to cover field selection, nested relationships, and error handling.
Start by listing operations, clients and expectations
Prepare the current schema, named queries used by clients, and a list of roles. The schema describes available types and fields, but does not define every business rule. For each scenario, record the operation, inputs, user identity, data owner, and expected result.
Choose by risk. Prioritise orders, payments, personal data, or administrative changes over an insignificant information field. Also test supported older client versions, which may use a different selection of fields.
An example order query could look like this. Adapt the field names to your own schema:
query OrderDetail($id: ID!) {
order(id: $id) {
id
total
customer {
id
email
}
}
}
Keep the query the same while changing the id variable and the account sending it. Check the user’s own order, then someone else’s order, a nonexistent order, and an order from another organisation. Define expectations beforehand from the API’s rules so that the test does more than confirm current behaviour.
Check HTTP status, data and errors separately
GraphQL distinguishes a request error before execution from an error while resolving individual fields. A request error prevents execution from starting. Typical examples include a nonexistent field or a query with invalid syntax. The response contains errors but has no data entry.
An execution error may produce a partial result. The client receives some data together with errors. This differs from a missing response or a network timeout.
| Scenario | What the test should check |
|---|---|
| Successful query | Correct values, the requested data shape, and no errors |
| Error before execution | The expected error, no data entry, and no execution of the operation |
| Failure of one field | Expected partial data and the correct error path in errors[].path |
| Network or transport error | Client behaviour when a response is unavailable or cannot be processed |
Check business rejections returned as data separately, such as insufficient stock for an order. The API may express a rejection through its own result type or a list of issues inside data, without a top-level errors field. The response can be technically successful even though no order was created. The test must distinguish successful operation execution from a successful customer action. Your schema and business contract define field names and expected error codes, rather than a general GraphQL rule.
HTTP 200 can accompany a GraphQL error. The exact status depends on the implementation, error type, and response format. GraphQL over HTTP is an evolving draft that also distinguishes application/graphql-response+json. Check the Accept and Content-Type headers and the deployed server’s contract in your tests. Do not reuse a single expected HTTP code for every error scenario.
How an error propagates through required fields
A field may allow null or be marked Non-Null, meaning it cannot be null. When a required field fails, the error propagates to its parent until it reaches a position that allows null. If no such position exists along the chain, the result may be data: null.
In the example query, make email retrieval fail. Depending on the schema, only the email, the entire customer, or the order may become null. Check the exact scope and error path. A null value without an error may also be a valid result, for example for an optional detail the customer did not provide.
Test variables and input objects at their boundaries
Variables separate values from the operation’s text. You can therefore test the same query with different inputs. Check correct and incorrect types, an unknown enum value, an empty list, a long string, and an invalid identifier.
Distinguish omitted input from explicit null. For optional input, these may mean different things, such as retaining an existing detail or deleting it. Default values also have their own rules. Derive expectations from the schema and the operation’s contract.
The type system does not replace business validation. A value can have the correct type and still contain an unacceptable quantity or a date outside the permitted period. For custom types, such as dates or monetary values, check both the supported format and the resulting meaning. Also verify that rejected input caused no unwanted data changes.
Test permissions on objects, fields and relationships
Prepare a matrix of roles, operations, and data ownership. Include an unauthenticated user, a customer, a colleague in the same organisation, and an account from another organisation if the application distinguishes them. Authorisation should follow business rules. Possessing an object’s identifier is not enough.
For each sensitive detail, try several available paths. An order may be accessible directly by ID, through a customer, or through a result list. OWASP recommends checking objects and their relationships. Also check sensitive fields on an object that the user is otherwise allowed to read.
When access is denied, check the whole result. An access-denied message in errors is insufficient if forbidden information is still present in data. The rules may require null, an error, or hiding the result. The test must check the agreed behaviour and ensure related fields do not leak the information.
Decide whether to expose introspection, the ability to explore the schema, according to the deployment. A hidden schema does not replace authorisation. Check that error responses do not give clients internal stack traces, secret values, or details that the contract does not expose.
For mutations, check the resulting state too
A mutation changes data, for example by creating or cancelling an order. After a successful response, verify the stored state with another query or a suitable system check. For a rejected change, confirm that it was not performed.
Try a user without write permission, an invalid state transition, and repeated submission. If the contract promises that a repeated request will not create a second order, verify that promise too. GraphQL does not guarantee this property itself. Prepare separate data and cleanup for mutations so that parallel tests do not change the same records.
Query cost involves more than depth
A short query can request a large list. A deeper query may return just one record. Demand control can therefore combine depth, field count, result size, estimated work, and request limits.
Test the allowed boundary and what happens beyond it. Use broad field selections, nested lists, and aliases, which rename fields in the response and let clients request multiple variants at once. If the server supports batched requests, check their limit too. Rejection should follow the behaviour agreed for that API.
For pagination, check result count, continuation through a cursor, the final page, and unauthorised items. Cursors and arguments depend on the specific schema. Measure server work too: the N+1 problem means fetching a list triggers further separate calls for its items. Then assess performance with realistic data through API performance tests.
What to automate and how to verify coverage
For direct API tests, you can use Python with an HTTP client and pytest or Playwright. You send the request without opening a browser. Write checks for the GraphQL result according to your contract.
Add critical queries, mutations, the authorisation matrix, and expected errors to CI/CD, the automated process for checking and deploying changes. When the schema changes, validate the stored operations of supported clients. Contract testing can supplement checks of the agreement between client and server, but does not replace tests of the resulting values.
Assess coverage with a deliberate fault in a test environment. Remove a permission or change an expected value and verify that the relevant test fails. The result should identify which operation, role, and condition no longer work. Test performance limits in an agreed environment so that the regular pipeline does not load the live system.
Where to start
Choose one important operation and add the user’s own and someone else’s data, invalid input, a partial error, and a demand-control check. This shows which expectations still need agreement and which checks the suite actually performs. The result should be a view of access rules and a repeatable check that warns of changed behaviour before release.