How to test GraphQL using Postman ?
When we call an API, it internally executes some code on the server. That code will do any tasks like computation, fetch the data, or call some other API.
GraphQL is an open-source data query and manipulation language for APIs. It is created by Facebook in 2012 and made open sourced in 2015. It helps to fetch the data from the server to the client and to get the response what the user exactly requests, nothing more or less. It provides a better data-fetching approach.
GraphQL is currently used by a large number of companies like PayPal, GitHub, The New York Times, Glassdoor, Twitter, etc.
Advantages of GraphQL
1. It solves overfetching (getting more data than we need) and underfetching (not getting all the data we need) problems
2. It interacts with many databases.
3. It keeps documentation in sync with API changes automatically.
4. It allows the user to fetch data with a single API call, no need to go many versions.
5. It can be used for query batching and caching.
6. It allows the user to make requests based on needs.
7. It encodes on the client side, not the server side.
8. It is suitable for microservices and also complex systems
Tools support GraphQL
Following tools support GraphQL in API Testing.
Test GraphQL Java
Components in GraphQL
Schema : It describes the available functionalities to the clients which are connecting to it. Our request needs a GraphQL schema in order to enable autocompletion. using GraphQL introspection, Postman will automatically fetch a schema based on the request URL. A message is displayed next to the schema selection list in green color when Postman is able to auto-fetch a schema.
Query: It is used to read or fetch values.
Mutations: It modifies data in the data store and returns a value. Mutations help to insert, update, or delete data. It is similar to REST API's Create, Update or Delete methods.
Resolver: Resolvers provide the directions for converting GraphQL operation into data. They also resolve the query to data by defining resolver functions. They are also known Query Handlers.
Subscriptions: It allows the server to send the data to the client when a specific event occurs. It is similar to Trigger in database concepts.
HTTP Methods: It has 2 methods, POST and GET.
Difference between GraphQL and REST
Consisteny across multiple platforms
Not consistent across multiple platforms
Message format should be a String for GraphQL
Message format can be anything.
It uses metadata for
It does not have any machine readable metadata
It is strongly typed
It is weakly typed
GraphQL is organized based on schema
REST is organized based on endpoint
It has a growing user community
It has a large user community
It has single endpoint
It can have multiple endpoints
It follows client-driven architecture
It follows server-driven architecture
Things needed to start GraphQL
3. Query format
4. Test resolvers
5. HTTP Methods
How to test GraphQL endpoints using Postman ?
Postman tests many kinds of HTTP endpoints, GraphQL is one among them.
Query1: Fetch the names of 2 repositories in my GitHub
Step 1. Create a Collection in Postman.
Step 2. Name the collection as “GraphQL using Postman”.
Step 3. Add a POST request and name as Get total no. of repositories in my GitHub. Enter the endpoint as https://api.github.com/graphql.
Step 4. Postman allows to send the body in GraphQL. Next click the body tab and select GraphQL and enter the script in body.
Step 5. Next click Authorization tab and select Bearer Token and enter your Token.
Step 6. We can edit the GRAPHQL variables based on our requirement. Here I want to fetch just 2 records from my GitHub.
A GraphQL request consists of two sections. One for query and another for variables. GraphQL Variables are declared after the query and passed as arguments to a function. GraphQL takes the dynamic values out of the query section and keeps in variables section.
Steps involved with variables
1. Replace the static value in the query with $variableName
2. Declare $variableName as one of the variables accepted by the query
3. Pass variableName
Step 7. Click on Send.
Step 8. We can see just 2 Repository names DSAlgoProject and JenkinsDemoProject from my GitHub.
Query2: Fetch the specified fields, such as url, createdAt and updated At for a repository in my GitHub
Step 1. Add another request.
Step 2: Select a POST request and name it as Get specific fields for a repository. Enter the endpoint as https://api.github.com/graphql.
Step 3. Click Body tab and select GraphQL and enter the script. Here we are going to fetch the following fields createdAt, updatedAt and owner for my repository DSAlgoproject.
Step 4. Click Authorization tab, select Bearer token and paste Personal access token generated from GitHub.
Step 5. Click on Send.
Step 6. We can see the response with createdAt, updatedAt, and url for a particular Repository DSAlgoProject with success code 200.
How to generate Personal Access Token in your GitHub?
Personal access token is similar to your passwords used for authentication to your GitHub. There are 2 types of Personal access tokens in GitHub.
· 1.Fine-grained personal access token – It has more security features
· 2.Personal access token (classic) – It has less security features
Step 1. Signin to your GitHub account
Step 2. Click on your Profile picture on the right hand side and click on Settings
Step 3. Click on Developer Settings under Public profile on left hand side.
Step 4: Then click on Tokens (Classic).
Step 5: Click on Generate New Token and copy, paste it in a Word document for future reference.
In this blog, we covered how to test GraphQL using Postman. GraphQL is used to fetch data with a single API call and build better APIs.