Raviv Lior
Raviv Lior

Reputation: 61

Performance test for graphQL API

Today I'm doing my API automation testing and performance testing with Jmeter when the server is a REST API.

Now the development changed to graphQL API, and I have two questions about it:

  1. What is the best way to perform the automation API and performance testing?
  2. Does Jmeter support graphQL API?

Upvotes: 6

Views: 16012

Answers (7)

Kalz
Kalz

Reputation: 1

The easiest way will be to use the GraphQL queries directly in JMeter without the need to convert them to JSON.

All you need to do is to pass "Content-Type" as "application/graphql" in the header.

Image Link for: HTTP Request with GraphQL Query as input

Image Link for: Header details

Upvotes: 0

Rutvij07
Rutvij07

Reputation: 375

I have recently tried API testing with GraphQl with both GET and POST request in Jmeter

Make sure its POST request for both Query and Mutation

Example Your Graph Ql query

{
  storeConfig{
    default_title
    copyright
  }
}

For Jmeter it would be like this

{
    "query":"{ storeConfig { default_title copyright } }"
}

Step up HTTP Request

enter image description here

In place of the localhost, your domain name will come. Make sure you don't add https

Example:- https://mydomainname.com

In Jmeter :- mydomainname.com

Setup HTTP Header Manager

enter image description here

For requesting Mutation in Jmeter

Example mutation in Graphql

 mutation {
      generateCustomerToken(
          email: "[email protected]"
          password: "1234567"
      ) {
          token
   }
}

In Jemeter mutation will be like this

{
    "query":"mutation { generateCustomerToken( email: \"[email protected]\" password: \"1234567\" ) { token } }"
}

Replace double quotes with (\") as shown in above query

enter image description here

Upvotes: 1

Yuci
Yuci

Reputation: 30189

I use Apollo to build the GraphQL server, and use JMeter to query the GraphQL API as below.

1. Set up HTTP Request

enter image description here

2. Set up HTTP Headers

enter image description here

Depending on your application, you might also need to set up HTTP header Authorization for JWT web tokens, such as:

Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxx

3. Set up HTTP Cookie if needed for your app

4. Run the test

enter image description here

Upvotes: 6

Germán Segura
Germán Segura

Reputation: 19

I am testing our GraphQL Implementation, you will need:

Upvotes: 1

ppcano
ppcano

Reputation: 2861

Disclaimer: I work for LoadImpact; the company behind k6.

If you are willing to consider an alternative, I've recently written a blog post about this topic: Load testing GraphQL with k6.

This is how a k6 example looks like:

let accessToken = "YOUR_GITHUB_ACCESS_TOKEN";

let query = `
 query FindFirstIssue {
   repository(owner:"loadimpact", name:"k6") {
     issues(first:1) {
       edges {
         node {
           id
           number
           title
         }
       }
     }
   }
 }`;

let headers = {
 'Authorization': `Bearer ${accessToken}`,
 "Content-Type": "application/json"
};

let res = http.post("https://api.github.com/graphql",
 JSON.stringify({ query: query }),
 {headers: headers}
);

Upvotes: 5

alejandro estrada
alejandro estrada

Reputation: 504

You can try using easygraphql-load-tester

How it works:

easygraphql-load-tester is a node library created to make load testing on GraphQL based on the schema; it'll create a bunch of queries, that are going to be the ones used to test your server.

Examples:

Result:

Using this package, it was possible to me, to identify a bad implementation using dataloaders on the server.

Results without dataloaders

All virtual users finished
Summary report @ 10:07:55(-0500) 2018-11-23
  Scenarios launched:  5
  Scenarios completed: 5
  Requests completed:  295
  RPS sent: 36.88
  Request latency:
    min: 1.6
    max: 470.9
    median: 32.9
    p95: 233.2
    p99: 410.8
  Scenario counts:
    GraphQL Query load test: 5 (100%)
  Codes:
    200: 295

Results with dataloaders

All virtual users finished
Summary report @ 10:09:09(-0500) 2018-11-23
  Scenarios launched:  5
  Scenarios completed: 5
  Requests completed:  295
  RPS sent: 65.85
  Request latency:
    min: 1.5
    max: 71.9
    median: 3.3
    p95: 19.4
    p99: 36.2
  Scenario counts:
    GraphQL Query load test: 5 (100%)
  Codes:
    200: 295

Upvotes: 2

Dmitri T
Dmitri T

Reputation: 168147

Looking into Serving over HTTP section of the GraphQL documentation

When receiving an HTTP GET request, the GraphQL query should be specified in the "query" query string.

So you can just append your GraphQL query to your request URL.

JMeter Query GraphQL

With regards to "best practices" - you should follow "normal" recommendations for web applications and HTTP APIs testing, for example check out REST API Testing - How to Do it Right article.

Upvotes: 2

Related Questions