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 –
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 -
- Select POST from the dropdown menu as we know that all graphql requests are POST in nature.
- Select Body > Graphql
- Write your query you want to test.
Image is also attached for your reference-
Sample Results
This will return results for your test case as shown-
Now, we are ready for the Load and functional tests.
Functional Test
For functional testing, select Runner from right bottom as shown -
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.
Result
After running the tests for n iterations, the results will be given as shown -
Load Performance Test
For Load testing, select Runner from right bottom as shown -
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.
Result
Once the test is successful, it will give you the result with the graph as shown -
Conclusion
Hence, load testing on graphQL API was done and we got report after the tests. Happy coding!