Skip to main content

GraphQL API Testing

While Rumour handles standard REST APIs effortlessly, it also offers first-class, native support for GraphQL.

If you've ever tried testing a GraphQL API with standard HTTP tools, you know the pain: formatting massive multi-line JSON strings, escaping quotes manually, and struggling to inject dynamic variables cleanly into a stringified payload.

Rumour solves this by providing a dedicated [graphql] block that natively parses and compiles your queries and variables.


The Scenario​

We are going to construct a two-step integration test:

  1. The Mutation: We will execute a mutation to create a new user and extract the returned ID.
  2. The Query: We will use that extracted ID to fetch the user and assert that the GraphQL engine didn't return any hidden application errors.

1. Actionable Setup​

Let's build this from scratch. First, we need a live environment to test against. We will use the official rumour-mock-server repository.

Start the Mock Server​

In a new terminal window, boot up the multi-protocol mock server (which spins up a GraphQL endpoint on port 4000):

cd rumour-mock-server/mock-server
npm install
npm start

Scaffold the Workspace​

In your main terminal, initialize a fresh testing workspace and scaffold a new collection:

mkdir graphql_tutorial
cd graphql_tutorial
rumour init
rumour new user_flows

Open the newly generated workspace.env.toml and define the target endpoint:

[variables]
graphql_url = "http://localhost:4000/api/v2/graphql"

Step 1: Executing a Mutation​

Let's create our first request file: 01_create_user.toml.

Notice how we use the protocol = "graphql" directive. This unlocks the [graphql] and [graphql.variables] blocks, allowing us to write raw, multi-line GraphQL queries without any messy JSON escaping.

name = "graphql_create_user"
protocol = "graphql"

[request]
method = "POST"
url = "{{graphql_url}}"

[graphql]
operation_name = "CreateUser"
query = '''
mutation CreateUser($name: String!, $email: String!) {
createUser(name: $name, email: $email) {
id
name
}
}
'''

# Variables are mapped directly to the GQL query context
[graphql.variables]
name = "Jane Doe"
email = "jane@example.com"

[assert]
status = 200

[extract]
# We extract the newly created ID to use in the next test
new_user_id = "json.data.createUser.id"

The Output Trace​

When you run this file (rumour run user_flows/01_create_user.toml -v), Rumour automatically compiles the TOML block into a perfectly formatted JSON payload behind the scenes:

POST http://localhost:4000/api/v2/graphql
URL: http://localhost:4000/api/v2/graphql
Body (graphql): query CreateUser { ... } variables: {"name":"Jane Doe"}
✓ SUCCESS: 01_create_user.toml (32ms)

Step 2: Querying and Asserting Data​

Now, let's create our second request file: 02_verify_user.toml.

Here, we will explicitly depend on the previous file and inject the {{new_user_id}} variable.

Built-in GraphQL Error Protection​

GraphQL endpoints famously return a 200 OK HTTP status code even when the query completely fails. The actual failure is embedded inside an errors array in the JSON response.

Rumour automatically protects against this: when protocol = "graphql", Rumour automatically fails the test if an errors array is returned in the response, preventing silent failures disguised as HTTP 200s.

name = "graphql_verify_user"
protocol = "graphql"

[dependencies]
"01_create_user.toml" = "new_user_id"

[request]
method = "POST"
url = "{{graphql_url}}"

[graphql]
query = '''
query GetUser($id: ID!) {
user(id: $id) {
name
}
}
'''

[graphql.variables]
id = "{{new_user_id}}"

[assert]
status = 200

[assert.json]
"data.user.name" = { equal = "Jane Doe" }

What happens if the query returns an error?​

Let's say we accidentally passed an invalid ID type. Even though the server returns HTTP 200 OK, Rumour catches the internal GraphQL error automatically and marks the request as failed:

POST http://localhost:4000/api/v2/graphql
URL: http://localhost:4000/api/v2/graphql
✗ FAILED: 02_verify_user.toml (15ms) - GraphQL errors: Variable "$id" got invalid value; Expected type ID.

Failure Intelligence:
Reason: GraphQL errors: "Variable '$id' got invalid value; Expected type ID."
Testing Error Scenarios

If you are specifically testing error cases and expect the endpoint to return errors without failing the test node, you can set graphql_errors = false inside [assert] to disable the automatic failure check.