GraphQL Testing: How to Test a GraphQL API When a 200 Hides Errors

GraphQL testing without code: the cases to copy for queries, variables and mutations, and how to catch the errors an answer can hold under a status 200.

By Ait Nohaila, Business Development Representative 13 min read

GraphQL testing means sending an API's queries and mutations, then checking the data, the errors and the status of each answer. An answer can report errors under a 200, so the status alone is not enough.

Here is how it goes unnoticed. The app shows an order with no price. The API test is green, because it checks that the server answered 200. It did. The price was null, and the reason was under errors, where nobody looked.

What is GraphQL testing?

Definition

GraphQL testing checks that an API answers its queries with the right data, changes what its mutations say they change, and refuses what it must refuse.

  • A query reads data.
  • A mutation changes it.

Both usually travel to one address, and the text you send says which fields you want back.

That is what makes GraphQL pleasant to use, and what surprises testers. There is no list of addresses to go through. The thing to test is each query and each mutation your app relies on.

GraphQL vs REST testing: what changes for a tester

What you testA REST APIA GraphQL API
The addressOne per kind of record, such as /ordersUsually one for everything, such as /graphql
The actionThe method: GET, POST, PUT, DELETEThe text you send: a query or a mutation
The fields in the answerChosen by the serverChosen by the query
The statusSays most of what happenedSays a part of it: read errors too
A failureUsually an answer of its ownCan stand beside good data, in the same answer

The one address is GraphQL's own convention: its documentation on HTTP says a server works on a single address, usually /graphql.For the REST side, with every status, see API test cases you can copy.

GraphQL error 200: how can an answer be a 200 and still be wrong?

One query can ask for twenty fields. If nineteen work and one fails, the server can send back the nineteen and describe the failure. HTTP has no status for a partial success. So the status says success, and the failure is inside the answer.

"If a response contains a data key and its value is not null, then the server should respond with a 2xx status code" ... "This is the case even if the response includes errors"
GraphQL.org, Serving over HTTP

In plain words: as soon as some data came back, the status should say success, such as 200.GraphQL calls an answer with both data and errors a partial response.

Everything worked

Status
200
data
Everything you asked for
errors
Not there

A check on the status passes, and it is right.

A part of it failed

Status
200
data
Some of it, the rest is null
errors
What failed, and why

A check on the status passes, and it is wrong.

Refused before it ran

Status
Typically 400, or 200 on some older servers
data
Not there
errors
The mistake in the query

What a check on the status says depends on the server.

Three answers a GraphQL API can give. The second is the one a test misses when it looks at the status alone.

Where to look: the status, data and errors

What went wrongWhat the answer holdsWhat to check
Nothingdata, and no errorsThe values you asked for
A field failed while the query randata, with null where the field failed, and errorsThe message, and which values are null
The query ran, and nothing could be returneddata is null, and errorsThe message: the status differs from one server to another
The query was refused: a typing mistake, a field that does not exist, a wrong typeerrors, and no dataThe message, and the status your server gives
No GraphQL answer came: the server is down, or something in front of it answeredA page or nothing, instead of data and errorsThe status, or that no answer came

What an answer holds is described on GraphQL's page about the response, and the statuses on its page about HTTP.Each error carries a message for the person who reads it.That message is what you want in a failed test, instead of a bare "expected 200".

Testing GraphQL variables: two kinds, one request

A query should not hold the values it looks for. It declares variables, and the values travel beside it as JSON. The same query then reads any order.

A query with one variable
query Order($id: ID!) {
  order(id: $id) {
    id
    status
    customer { name }
    items { name quantity }
  }
}

A test has variables of its own: the server's address, a token, the id an earlier step saved. The two kinds look alike and do different jobs.

  1. A variable of the test

    {{orderId}}

    Saved by the step that created the order. It fills the JSON when the request is sent.

  2. The query's variables, as JSON

    { "id": "{{orderId}}" }

    What the API receives beside the query. This is where the two kinds meet.

  3. A variable of the query

    order(id: $id)

    Declared by the query itself. The server fills it with the JSON value.

The way of one order id. Double braces belong to the test, and the dollar sign belongs to GraphQL.

One set of values per environment, and a secret that never enters the test file, are the subject of environment variables in API testing.

GraphQL test cases to copy

Start with the queries and mutations your app's main screens use. For each one, take the cases below that apply. The names are from an invented shop: put yours in their place.

Test caseWhat you sendWhat to check
A query returns the recordThe order query, with an id that existsdata.order.id equals that id, and data.order.status exists
The variables really change the answerThe same query, with another iddata.order.id equals the other id
A nested list is completeThe order query, for an order with two itemsdata.order.items.length equals 2
A mutation really changes the recordThe mutation, then a query that reads the record backThe second answer holds the new values
A field that does not existThe query, with a field the API does not haveerrors[0].message contains the field's name, and data does not exist
An argument of the wrong typeA word where the API expects a numbererrors[0].message exists, and nothing was changed
No permission on one fieldThe query, as a user who may not see the cost priceThe order is there, data.order.costPrice is empty, and errors[0].message says why
An id that does not existThe query, with an id no order hasAn empty order or an error, whichever your API promises
No tokenThe query, with no authorizationThe refusal your API promises: a status such as 401, or an error

Several rows expect an error: how to test a GraphQL error on purpose, further down, has the settings for them.

The permission case deserves a place in every list. It is natural to check that the right person sees the price, and easy to forget that the wrong person must not. Ask your team what the API promises there: an empty field with an error, or no order at all.

How to test a GraphQL query, step by step

GUI Test Studio automates end-to-end tests of any app without code.Its Call API step sends one request and checks the answer, in any test, with or without a screen.

  1. 1

    Add a Call API step

    In GUI Test Studio, drag Call API from the Test Flow group of the Events palette onto the timeline, then click Edit Request... in its Properties.

  2. 2

    Give the address

    Choose POST and type your GraphQL address, such as {{baseUrl}}/graphql, so the same test runs against any server.

  3. 3

    Paste the query and its variables

    Choose GraphQL as the body.Paste the query, and write its values as JSON under Variables.Fill in Operation only when the text holds several operations.

  4. 4

    Say who is asking

    On Authorization, choose how your API recognizes a caller, such as a Bearer token kept in a variable.When the API has its own sign-in, see how to test an API with OAuth 2.0.

  5. 5

    Send it and click what must be true

    Press Send.Click a value of the answer, then Check This Value, or Save as Variable to use it in a later step.Nobody types a path.

  6. 6

    Apply, then run the test

    Every check says at once whether it holds.Click Apply, run the test, and read the step in the Confidence Report.

If you are new to API tests, start with API testing without code.It follows one order from its first request to its clean-up.

Testing GraphQL mutations: change it, then read it back

A mutation answers with the fields you ask for. It can answer with the values you sent while the record was never saved. So never let the mutation be its own witness.

Three Call API steps, one test

  1. 1Create the order

    mutation createOrder

    The server accepted the change.

    Saves orderId

  2. 2Read it back

    query order

    The order is really there, with the right customer and items.

    Uses orderId

  3. 3Cancel it

    mutation cancelOrder

    The test leaves the server as it found it.

    Uses orderId

The first answer says what the server meant to do. The second one says what it did.
The variables of the first step
{
  "input": {
    "customer": "Atlas Textiles",
    "reference": "{{$uuid}}",
    "items": [{ "name": "Office chair", "quantity": 2 }]
  }
}

Three habits keep this test reliable.

  • Give each order a reference made new at every use, so two runs never collide.
  • Compare the second answer with what you sent, not with the first answer.
  • If the server saves a moment later, turn on "Keep trying until it passes" on the step that reads the order back.

When your app also has a screen, add one more witness.Create the order with a mutation, then check that the screen shows it.API testing vs UI testing works through such a test, and end-to-end testing explains why the whole path matters.

How to test a GraphQL error on purpose

Some tests must see an error: the wrong argument, the forbidden field. The error is then the result you expect, and the step has to pass because of it.

  • 1Untick "Fail the step when the answer reports an error".
  • 2Press Send and click errors[0].message in the answer, then "Check This Value".
  • 3If your server answers the refusal with another status, such as 400, click "Expect 400".
  • 4Add a check on data too: it does not exist for a refused query, and it holds the fields that worked for a partial answer.
ExampleA support agent must not see the cost price

The step uses a support agent's test token and asks for an order's status and its cost price.Four checks: data.order.status exists, data.order.costPrice is empty, errors[0].message contains "not allowed", and errors.length equals 1.If a release opens that price to everyone, the second check fails on that build, with the price in the report.

The last check keeps the test honest. With the box unticked, a second error, one you did not expect, would otherwise pass unseen.

How to test GraphQL endpoints at every build

Adding a field to a GraphQL API rarely breaks an app. Renaming or removing one breaks every app that still asks for it. Run the cases above at every build, and you learn it before your users do.

One run against staging
gtr-cli run tests --variables staging.env --junit results\junit.xml

When a step fails, the console and the JUnit file say which check did not hold and the beginning of what the server answered.Run tests in CI/CD has the set-up for each pipeline tool.

WhoWhat they do in a GraphQL test
Your teamDecides what each query must promise, and who may see which field.
An AI assistant, if you use oneSends a query to read the answer, then adds the step with its checks. It can be Claude, ChatGPT with Codex, or a local AI model, one we provide or one you bring.
The GTS EngineRuns the saved test and checks every answer, the same way at every run. It needs no AI model to do it.

What an assistant adds stands on the same timeline as your own steps, and you read it there.See Create a test from a prompt.

What to know before you start

Good to knowWhat it means
No subscriptionsThe step sends one request and reads one answer: queries and mutations. Check a live update on your app's screen instead.
Servers differThe status of a refused query, and what happens to a forbidden field, depend on your server. Press Send, then decide what to expect.
It checks what you ask forA field your query does not name is not in the answer, so it is not tested. Copy the queries your app really sends.
A functional testIt checks what the API answers and whom it refuses. It is not a load test or a security audit.

Key takeaways

  • 1A 200 is not a pass: a GraphQL answer can hold data and errors at the same time.
  • 2Servers differ: a refused query is typically a 400, and some older servers answer 200.Find out what yours does.
  • 3Read three things: the status, data and errors.
  • 4Read a mutation back: its own answer does not prove that the record was saved.
  • 5Let the step catch the error: a Call API step fails when the answer reports one.Untick that box only when the error is the point of the test.

Questions

How do I test a GraphQL API without code?

Add a Call API step in GUI Test Studio, choose GraphQL as its body, paste the query and its variables, and press Send. Click a value of the real answer to check it. The step also fails by itself when the answer reports an error.

Why does GraphQL return 200 with errors?

Because part of the query worked. GraphQL's own documentation says an answer whose data is not null should be a 2xx, even when it includes errors, because HTTP has no status for a partial success. The failed part is described under errors.

What does GraphQL error code 200 mean?

It means the server handled the HTTP request, not that every field of the query worked. Read the answer itself: errors says what failed, and data holds what worked. A check on the status alone misses it.

Does a failed GraphQL query always return 200?

No. A query that is refused before it runs, for example because a field does not exist, is typically answered with a 400, although some older servers answer 200. Send the request to your own server once and see which one it gives.

How is GraphQL testing different from REST testing?

A GraphQL API usually has one address, and the query says what you want. So you test queries and mutations instead of a list of addresses, you read errors as well as the status, and one answer can hold both data and errors.

How do I test a GraphQL mutation?

Send the mutation, save the id it returns, then send a query that reads the same record back and check its values. The mutation's own answer says what the server meant to do. The second call says what was really saved.

How do I pass variables to a GraphQL query in a test?

Write them as JSON beside the query, such as an id for the query's $id. In GUI Test Studio, that JSON can hold a variable of the test between double braces, so the id an earlier step saved goes into the query.

Can GUI Test Studio test GraphQL subscriptions?

No. A Call API step sends one request and reads one answer, so it covers queries and mutations. When a subscription updates your app's screen, a check on the screen can verify that update.

All articles