Load test on GraphQL APIs using postman
Tags: graphql, postman, loadtest, testing
Published: 2024-02-29 08:50:26

## **What is graphql?**

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. (definition from https://graphql.org/)

## **Why graphql?**

GraphQL resolves the issues of 
•	Over-fetching and under-fetching by allowing clients to ask what exactly data they need 
•	Fetching the exact data, no more, no less just the required data.

## **Difference between graphql and REST APIS**

**Client-side request**

Here’s what a REST request uses to work: 
•	HTTP verbs that determine the action
•	A URL that identifies the resource on which to action the HTTP verb
•	Parameters and values to parse, if you want to create or modify an object within an existing server-side resource
For example, you use GET to get read-only data from a resource, POST to add a new resource entry, or PUT to update a resource.

In contrast, here’s what GraphQL requests use:
•	Query for getting read-only data
•	Mutation for modifying data
•	Subscription to receive event-based or streaming data updates

A data format describes how you would like the server to return the data, including objects and fields that match the server-side schema. You can also input new data. Internally, GraphQL sends every client request as a POST HTTP request.

**Graphql queries**

Due to the ability of graphql of solving the under fetching and over fetching problem, it’s very easy to write a graphql query and ask for only what we need.
Example – 

![Graphql Query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qmfm5klyndt9bnal1ubf.png)


## **Why POSTMAN?**

Postman is an API platform. It is a stand-alone tool for testing apps that creates, tests, designs, modifies, and documents APIs. For sending and viewing HTTP requests and responses, it is a primary GUI.
(More at https://www.codingninjas.com/studio/library/why-is-postman-so-popular-among-developers)

**How to load test api using postman?**

Write test to check if the response from the server is less than 1 sec. The code is shown as - 

Code -

```
pm.test("Response time is less than 1sec", function () {
    pm.expect(pm.response.responseTime).to.be.below(1000);
});
```
Now, create new request by clicking on the + icon.
Following steps are to be followed - 

1. Select POST from the dropdown menu as we know that all graphql requests are POST in nature.
2. Select Body > Graphql
3. Write your query you want to test.

Image is also attached for your reference-

![Postman settings](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hpxudv5u2s65jequ2vld.png)


## **Sample Results**
 
This will return results for your test case as shown-

![Tests result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s4856vi7mt258iabgs8t.png)

Now, we are ready for the Load and functional tests.

## **Functional Test**

For functional testing, select Runner from right bottom as shown - 


![Runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsqkord1lrtphholrsh2.png)


**Setup**

Input the number of iterations and delay if needed between two consecutive api tests.
Drag and drop or select the API test file in the playground. You can also order your API as per your preferences.
Press Run to start the tests.

![Tests setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7bf6tctj27wh9uq49zvw.png)

**Result**

After running the tests for n iterations, the results will be given as shown - 

![Tests result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gareo6cvh1t3w7e27397.png)

## **Load Performance Test**

For Load testing, select Runner from right bottom as shown - 

![Runner](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jsqkord1lrtphholrsh2.png)

**Setup**

On load performance testing, select the number of virtual user you want to include and the duration for which u want to keep your APIs under load.
Press Run to start the load test.

![Test setup](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0v2qh13rwuph77aq6bjm.png)

**Result**

Once the test is successful, it will give you the result with the graph as shown -


![Test Result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4h63eemeyivfx7keyfl.png)

## **Conclusion**

Hence, load testing on graphQL API was done and we got report after the tests. Happy coding!