Load Testing GraphQL APIs

A guide on how to load test a GraphQL based API, featuring the SpaceX API as an illustrative example.

LoadForge can record your browser, graphically build tests, scan your site with a wizard and more. Sign up now to run your first test.

Sign up now

Overview

LoadForge makes testing GraphQL APIs straightforward by utilizing standard HTTP POST requests to send GraphQL queries.

This guide demonstrates how to create a load test to query the missions from the publicly available SpaceX GraphQL API. The examples provided here can be adapted to test any GraphQL API, including authentication scenarios.


Basic Example

Here's a fundamental example showing a standard GraphQL query that requests the mission IDs and names from the SpaceX API:

{
  missions {
    id
    name
  }
}

The following example shows how to implement this query for use in a LoadForge test, demonstrating the straightforward integration of GraphQL queries.

import time
from locust import HttpUser, task, between

class QuickstartUser(HttpUser):
    wait_time = between(3, 5)

    @task(1)
    def index_page(self):
        query = '''
            {
              missions {
                id
                name
              }
            }
            '''

        response = self.client.post(
            "/graphql",
            name="GraphQL",
            json={"query": query }
        )

Authenticating GraphQL Requests

Many GraphQL APIs require authentication. The following example shows how to set custom headers for authentication:

    response = self.client.post(
        "/graphql",
        name="GraphQL",
        headers={
            "Accept": "application/graphql",
            "Authorization": "<Authorization-Token>"
        },
        json={"query": query}
    )

For dynamic authorization token retrieval, the on_start method can automate this process, ensuring all requests include the required authentication header:

    def on_start(self):
        # Send a login request
        response = requests.post("http://mysite.com/login", {"username": "user", "password": "pass"})

        # Extract the "token" from the response header
        self.client.headers.update({'Authorization': response.headers.get('token')})

Locust Test Script (locust.py)

# locust.py
import time
from locust import HttpUser, between, task

class GraphQLUser(HttpUser):
    wait_time = between(1, 3)
    host = "https://api.spacex.land/graphql"

    def on_start(self):
        # Optionally disable SSL verification
        self.client.verify = False

    @task
    def fetch_missions(self):
        query = """
        {
          missions {
            id
            name
          }
        }
        """
        self.client.post(
            "/",
            name="GraphQL Query",
            json={"query": query}
        )

Locust Test Example

LoadForge is built upon the robust foundation of Locust, which means that if you're already a user of the open-source Locust tool, you can directly use this script in your tests. If you're considering upgrading your testing capabilities, think about importing your Locust script to LoadForge and make the most out of your load testing efforts!

Ready to run your test?
Launch your locust test at scale.