How load test apollo server with locust?

Viewed 2795

Does anyone know how to load test the Apollo server?

 class UserBehavior(TaskSet):
        def on_start(self):
            self.login()

        @task
        def login(self):
            headers = {"content-type": "application/json"}
            self.client.post("/", data=json.dumps({
            "query": "mutation { login(username:\"9849999983\", password: \"123456\") {  token User { id fullName "
                     "email phoneNumber } } } "
            },
                headers=headers))


    class ApolloSample(HttpLocust):
        host = "https://sampleurl.com/api"
        min_wait = 20000
        max_wait = 50000
        task_set = UserBehavior

The problem with this is that, there is no particular endpoint to keep in the self.client.post("/") method. Since Graphql basically consist of Queries and Mutations.

2 Answers

Following would work for GraphQL queries & mutation. Don't miss Accept headers.

Queries

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

Mutation

response = self.client.post(
            "http://localhost:5424/graphql",
            name="GraphQL",
            headers={
                "Accept": "application/graphql",
                "Authorization": "<Authorization-Token>"
            },
            json={"query": "<Your-GraphQL-Query>,"
                  "operationName": "<Operation-Name>,
                  "variables":"<Input-Variables>" }
        )

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

Related