How to test your GraphQL API | Beginner’s Guide

A Photo By Ekrem Kurt

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.

Source: https://graphql.org/

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.

Source: Google Trends

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.

https://spotify-graphql-server.herokuapp.com/graphql

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.

* Click on import section

* 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.

  1. We need to pass bearer token value.
  2. 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
* A sample of Query consists of name, ID, image, and albums

In Query type, we are allowed to use variables as well. We just need to replace the static value in the query with $variableNameas 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.

* Using environment variable from Postman

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.

* A simple example of executing tests in Postman

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.

* A sample of Mutation with Update
* A sample of Mutation with Delete

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 is true.
  • @skip(if: Boolean) Skip this field if the argument is true.
* A sample of using Directive with @include option

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.

* withAlbums variable is set to true again

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.

Sources:

--

--

--

Inquisitive person by nature, technology fancier, and quality assurance provider. Eager to learn more on test automation.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Really AWS Quick Overview

Test-Driven Development

Box-Model in

Top 50 HTML Interview Question 2020

NuNet private Alpha release 0.1.2

Automate deployment of CTF challenges using CI/CD

Robotika, debugging

Starting from Scratch pt.1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ekrem Kurt

Ekrem Kurt

Inquisitive person by nature, technology fancier, and quality assurance provider. Eager to learn more on test automation.

More from Medium

APIs let’s start with the basics shall we? Then read this…

How to automate workflow-based applications using Workflow Design Pattern?

Everything You Need To Know When Migrating to Oracle Cloud

How to choose a mobile test automation framework?