How to test your GraphQL API | Beginner’s Guide
Start testing your GraphQL API easily with Postman
GraphQL is a query language for APIs that started at Facebook before being open-sourced in 2015. The simplification of queries and responses provided by GraphQL originated from usage within mobile applications, but it has general usefulness for simplifying the use of complex APIs.
The technology was originally used by Facebook’s iOS mobile team and then spread to the rest of the mobile world. In 2015, Facebook announced the implementation, the world saw the advantages of using the technology, and is now available in many environments and used by teams of all sizes.
GraphQL enables users to specify exactly which data they get back in their response, and it allows querying for multiple fields in a single request. GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint. It uses types to ensure Apps only ask for what’s possible and provide clear and helpful errors.
After a GraphQL service is running (typically at a URL on a web service), it can receive GraphQL queries to validate and execute. We, as a software QA engineers, will be mainly focused on this part — to write queries to validate response.
GraphQL vs. REST
It is not going to be analyzed from the performance, security, or usage perspective to define which one is better, or which one to choose in our project since both of them have pros and cons, instead, the differences from the testing perspective will be analyzed.
GraphQL APIs are becoming more popular with developers because of some of their benefits over RESTful APIs, and has consistently increased in popularity since its release. However, since 2020 the trend in popularity is relatively neutral, even back to the level of 2018–2019, which also needs to be further analyzed.
One of the biggest benefits is that GraphQL allows for more precise querying, which is especially useful when working with large APIs that return a lot of data. It can provide some significant querying efficiencies as compared to REST. RESTful APIs often return more data than what the client needs, alternatively, the client has to make multiple API calls to get all the data it needs.
If you wanted to use the Spotify REST API to get a list of all the titles of all the tracks of all the albums by a particular artist (e.g. Red Hot Chili Peppers), you would need to make three queries. You would need one query to find the artist ID, another to find all the albums, and another to find all the tracks on each album. If you were using the Spotify GraphQL API, you could access all of that data with a single query.
Creating a GraphQL API Schema for Query Auto-completion
First, go to the APIs tab in Postman and import if the file is downloaded, or create an API. Following steps can be followed if the file is downloaded, however similar steps can be followed for creating an API, and adding your schema and save. Just make sure that Schema type is GraphQL, and Schema Format is GraphQL SDL if you are creating an API.
* Select GraphQL and click on upload files
You will see similar to below, then click on import. You can also import collection as well by checking to generate a collection option.
Now, select your schema as Postman Collection after refreshing the page.
You will see autocompletion, which is very useful when it comes to writing Query / Mutation.
How to test GraphQL API using Postman
As all we know, we are testing the API based on CRUD, which consists of Create, Read, Update, and Delete. The Query type is used to read the data from the server. Other Crud operations such as create, update, or delete will be handled by Mutation type.
To send a GraphQL query through Postman, select the request body type as GraphQL and pass the query in the query section. The method would be POST.
There are 2 prerequisites before starting testing.
- We need to pass bearer token value.
- Set Content/Type value application/json
Query Type
Now we are ready to write our query type. The open-source site to practice is as follows:
https://spotify-graphql-server.herokuapp.com/graphql
In Query type, we are allowed to use variables as well. We just need to replace the static value in the query with $variableName
as in jQuery or PHP. On the right-hand side, there is a section, where you can define your GraphQL variables. You can even combine it with Postman environment variables.
As we have a response from the server, we can validate them. Since there is no difference regarding validation, I will not go into details about how to execute tests in Postman. However, one simple execution of the test can be seen below.
Mutation Type
The Mutation type is similar in structure and purpose to the Query type. The Mutation type defines entry points for write operations such as create, update, and delete operations.
The queries can also be created in GraphiQL tool with some basic steps, then this query can be copied into Postman.
As reference, the open-source site to practice is as follows:
https://hasura.io/learn/graphql/graphiql
Below you can see basic examples of Mutation type such as creating and deleting a new data in Postman.
As we see in the above example, we want to delete todos object, where ID is equal to the one that we have created above(48440). Affected rows 1 shows us that how many rows were affected from this delete action. Additionally, we could observe returning values to see which object actually we have deleted.
Directives
There is a feature in GraphQL called a directive, which can be attached to a field or fragment inclusion, and can affect execution of the query in any way the server desires.
@include(if: Boolean)
Only include this field in the result if the argument istrue
.@skip(if: Boolean)
Skip this field if the argument istrue
.
Directives can be useful to get out of situations where you otherwise would need to do string manipulation to add and remove fields in your query. It allows us to dynamically change the structure and shape of our queries using variables. By setting it false, we are hiding albums to be seen.
Server implementations may also add experimental features by defining completely new directives.
Conclusion
Both REST and GraphQL are prominent ways to design how an API will function and how applications will access data from it. Both REST and GraphQL can be used in a project, so it is good to know the basics of both and how to test them.
As it can be seen, this short tutorial aims only those, who are new to GraphQL, and want to analyze and test it. Of course, it is not limited to what I have covered here, but is enough to start with. After reading this article, you can execute all CRUD operations by using variables and directives. The documentation is very detailed and user-friendly to understand easily and to go further.
☕️ Happy testing! ☕️
You can follow me on Medium for more articles, connect with me on LinkedIn
These articles you might like as well.