DEV Community

Cover image for Deep-dive into GraphQL in JMeter
NaveenKumar Namachivayam ⚡
NaveenKumar Namachivayam ⚡

Posted on • Originally published at qainsights.com

Deep-dive into GraphQL in JMeter

With the release of the latest version of JMeter 5.4, you can do GraphQL performance testing using GraphQL HTTP Sampler. This blog deep-dive into GraphQL in JMeter 5.4. You can check out my earlier post about the top features of JMeter.

What is GraphQL?

Here is the text which is copied from graphql.org

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

GraphQL was developed by Facebook back in 2012. It was publicly announced in 2015. Couple years ago, the project moved to GraphQL Foundation, hosted by Linux Foundation.

You might be familiar with ReST APIs which is an architectural pattern which often over fetch or under fetch the data. GraphQL fetches at right amount. There are a lot of differences between ReST and GraphQL, quick internet search will help you to understand the differences.

Using ReST, you might end up using the multiple requests to fetch the required data. For e.g, if you are developing Ice Cream Online Store, to fetch the New Arrivals, Dairy-Free, and All-Flavors, you need to develop multiple HTTP requests.

Deep-dive into GraphQL in JMeter
Deep-dive into GraphQL in JMeter

If you want to fetch all the new arrivals which is dairy-free and all the flavors, you need to send multiple HTTP GET requests which is not the optimal way.

Enter GraphQL.

By sending only one query, you will be able to fetch the right amount of data without going back and forth.

Here is the fictional GraphQL query which fetches the new arrival's dairy free, all falvor's name and id.

query {
  newarrivals {
    dairyfree {
      allflavors {
        name
        id
      }
    }
  }
}

Deep-dive into GraphQL in JMeter - GraphQL
Deep-dive into GraphQL in JMeter - GraphQL

Here is the response you will get:

{
    "data": {
        "allflavors": {
            "name": "lemonbar",
            "id": "p101"
        }
    }
}

Before JMeter 5.4

As you aware, in JMeter 5.4, we get a new GraphQL HTTP Sampler to performance test the GraphQL queries. But without this GraphQL HTTP Sampler, you can use the HTTP Sampler to send the GraphQL request.

GraphQL Examples in JMeter Repo

Let us see Star Wars GraphQL demo project.

To retrieve a Character info using the HTTP Request, below is the settings you should use.

HTTP Method: POST

POST payload:

{
    "query": "{\n  hero {\n    name\n  }\n}\n"
}

Sample GraphQL Request
Sample GraphQL Request

There are three operation types in GraphQL: QueryMutation, and Subscription.


Deep-dive into GraphQL in JMeter 5.4

The equivalent query using the GraphQL in JMeter 5.4 would be:

HTTP Method: GET

Query: query=query+%7B+hero+%7B+name+appearsIn+%7D+%7D

Sample HTTP Request
Sample HTTP Request

Output in both the cases is:

{
    "data": {
        "hero": {
            "name": "R2-D2"
        }
    }
}

In GraphQL HTTP Request, you have the option to enter typical parameters such as Protocol, Server Name or IP, Port etc. It inherits the properties from HTTP Request Sampler. You will also get the GraphQL related fields such as Operation Name, Query, and Variables.

GraphQL Basic Settings
GraphQL Basic Settings

In GraphQL HTTP Request, there are two HTTP Methods: GET and POST. Other methods are irrelevant.

GraphQL Basic Settings HTTP Method
GraphQL Basic Settings HTTP Method

Also, you don't get Parameters and Body Data in GraphQL, instead you can leverage Query and Variables. File Upload option also irrelevant in here.

In Advanced tab, you don't see Embedded Resources from HTML Files.

GraphQL Advanced Settings
GraphQL Advanced Settings

In GraphQL HTTP Request, Query is Mandatory, Variables and Operation Name are Optional.

GraphQL Query Example

Now let us see few examples in JMeter 5.4 using Star Wars GraphQL. You can set up the project by following the instructions mentioned over here.

To start the server, you can follow the below commands.

git clone https://github.com/apollographql/starwars-server
cd starwars-server
npm install

If everything goes fine, you will be able to launch the server the Playground - http://localhost:8080/graphql

Star Wars GraphQL Demo
Star Wars GraphQL Demo

You will get the DOCS and SCHEMA for the Star Wars. It is important to understand the schema to write the queries.

To smoke test, let us retrieve a character. Copy and paste the below query and hit CTRL + ENTER key in the browser.

{
  hero {
    name
  }
}

In the right side, you will get the output as shown below.

{
  "data": {
    "hero": {
      "name": "R2-D2"
    }
  }
}

Back to JMeter.

In JMeter, let us add a GraphQL HTTP Request by right clicking on the Thread Group > Add > Sampler > GraphQL HTTP Request as shown below.

Add GraphQL Request in JMeter
Add GraphQL Request in JMeter

Fill the details in the GraphQL HTTP Request as shown below. Make sure you enter the port number as 8080.

Star Wars Query
Star Wars Query

Add a View Results Tree listener, and hit Run. You will get the output as shown below.

Star Wars Query Output
Star Wars Query Output

Congratulations! You have completed GraphQL test plan design in JMeter.

Let us see a little complex query which retrieves a driod information of ID 2000. Here is the query.

query {
  droid(id: 2000){
    id
    name
    friends{
      name
    }
    appearsIn
    primaryFunction
  }
}

Output

Star Wars Query Output

Now, let us make use of Variables in GraphQL. By using the Variables, you can reuse it across multiple clients.

For example, if you want to declare some dynamic string for the Search query, you can use the below query.

query search($keyword: String) {
  search(text: $keyword) {
    __typename
  }
}

In Variables, paste the below code.

{
  "keyword": "an"
}

Output

Star Wars Search Query Output
Star Wars Search Query Output

Now, let us see an example for Operation Name. If you have multiple queries, you can instruct JMeter to tell which query you would like to send.

In the below query we are trying to fetch a character and search output, where we have labelled them as heroSearch and textSearch respectively.

query textSearch {
  search(text: "an") {
    __typename
  }
}
query heroSearch {
  hero {
    name
  }
}

If you send this query as-is without specifying the Operation Name, JMeter will throw the below error.

Operation Name Error
Operation Name Error

When you have multiple queries, you must specify the Operation Name.

GraphQL Multiple Queries
GraphQL Multiple Queries

Output

GraphQL Multiple Queries Output
GraphQL Multiple Queries Output

Till now, we have seen examples for Query. Now, let us see about Mutations.

If you want to update or change the object, you should send Mutations request. In this Star Wars example, we are going to mutate the reviews.

Query

mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
  createReview(episode: $ep, review: $review) {
    stars
    commentary
  }
}

Variable

{
  "ep": "JEDI",
  "review": {
    "stars": 5,
    "commentary": "This is a great movie!"
  }
}

Output

GraphQL Variable Output
GraphQL Variable Output

IMPORTANT: JMeter doesn't support Subscription operation yet.

Even more examples are available in my GitHub repo. Please check and award a STAR.

GraphQL Examples in JMeter Repo

Pros

  • GraphQL HTTP Request is a part of JMeter Core. No need to install it from JMeter Plugins Manager
  • Supports Query and Mutations

Con

  • Doesn't support Subscriptions yet

Final Words

GraphQL in JMeter enables you to test the performance of GraphQL APIs without worrying about formatting the JSON and sniffing the HTTP traffic. But if you are comfortable in using HTTP Sampler, you can use. GraphQL provides a comfort and easy way to maintain the test plan.

Top comments (0)