Demystifying GraphQL - Tester’s edition

Demystifying GraphQL – Tester’s edition

It is high time we put out an edition of the GraphQL tutorial for our QA teams. There is a lot of material and more confusion on how to create GraphQL APIs on the internet. As if the idea of REST was easy for teams to understand – the definition of REST has always stopped just at the abbreviation, Representational State Transfer. Developers and Testers alike don’t really understand the concept. We know how to create REST APIs and how to test them and that works for us.

And while we were already fighting to understand REST APIs, we are told that there is a new kid on the block. Many companies including Coursera have moved onto GraphQL. You have to know how to test this new API that twitter and Starbucks are using to pull data. 

So here we go, a simplified version of the GraphQL story for the testing community. 

We will follow a simple sequence of steps to first understand about APIs and REST APIs. Then we travel to the realms of GraphQL and understand how they are different. Finally we create a master plan to test them. Well, aren’t we here to do just that. 

API ? What is that?

API is the acronym for Application Programming Interface, which is a software intermediary that allows two applications to talk to each other. 

Each time you use an app like Facebook, send an instant message, or check the weather on your phone, you’re using an API.

Let us take the example of a restaurant : 

Graph ql

Stick man representing a waiter. Boxes representing other elements.

Assume you are in a restaurant and you have the menu in your hands. 

For the restaurant kitchen to understand what you want to eat/order, you need to communicate this to Kitchen via the menu. 

Here we have two different systems – You & the restaurant kitchen. 

The waiter is the API, takes orders from you, passes it to the kitchen, gets the food to your table. 

So now, we know briefly about what an API is. It helps two systems communicate. 

Now we will try and understand what a REST API is. 

Building a REST API in 5 minutes

To understand the life of a “Waiter”, you have to understand the restaurant ecosystem. 

Let us build a tiny little REST API using nodejs and express.js

You don’t know nodejs and expressjs ? Don’t worry. The objective of this simple project is to simply show you the API that we create using these frameworks. 

Just follow these simple steps. 

What this essentially means is, we will be deploying our API on express js server. 

Step 1

Download and install NodeJS.

Step 2

Check if node and node package manager are installed

Open your command prompt or terminal and type the following commands one by one

npm -v

node -v

If these statements give you valid results, you have node and node package manager(npm) installed.

Step 3

Create a folder “REST_API”


npm init

When you are prompted for the below, hit enter

package name: (rest-api) version: (1.0.0) description: entry point: (index.js) test command: git repository: keywords: author: license: (ISC)

This generates a package.json file inside your REST_API folder.

Run the commands

 npm install -g express-generator  
npm install express –save

Express is a really cool Node framework that’s designed to help JavaScript developers create servers really quickly. NodeJS may be server side.

Step 4

Open the folder REST_API in VS Code. If you don’t have it, install it from here

Then create the app.js file or whatever you prefer naming it (default is index.js) and add in the following code.

var express = require(“express”); var app = express(); app.listen(3000, () => { console.log(“Server running on port 3000”); });


We just created a Waiter in the restaurant. 

Now let us tell the waiter what to get from the kitchen by adding the following code.

app.get(“/url”, (req, res, next) => { res.json([“Tony”,”Lisa”,”Michael”,”Ginger”,”Food”]); });


We have now specified what the Waiter needs to fetch.

Let us sit at the table and order → Run this simple application 

Open the terminal and run the command

npm app.js

You will see the message – “Server running on port 3000”

On any browser, go to the address, http://localhost:3000/url

You should see the below.

What we did here is

  1. We created a simple waiter for our restaurant
  2. We told the waiter what to get 

Since this is a simple app, the return result is also placed in the same app.js but for an actual application, the results would be fetched out of the database.

I hope this explanation for creating a simple REST API was easy to understand. 

The objective is to show you what an actual REST API is. 

Now that we know what REST API is, let us understand what a GraphQL API is all about. 

Assume we need 10 items to be fetched from the kitchen. 

With REST API , we will need to tell the waiter multiple times to get different items. 

We will be making multiple calls to the application on the other end – which is mostly a database.

We solve this by creating a GraphQL API. A GraphQL API will be able to fetch all what we need in a single call. 

GraphQL follows WYAIWYG pattern – What You Ask Is What You Get.

To understand this, let us first build a simple GraphQL API. This is not a comprehensive tutorial on GraphQL APIs and hence we won’t be getting into its specifics. All we care about is understanding how to test API. For this, we need to visualize this animal called GraphQL. 

A simple GraphQL application 

Users — Movies — Reviews

There are multiple users. 

A user can watch multiple movies and write multiple reviews. 

We will just create a simple API to fetch the list of users. Instead of creating a database, we will just add all details to a javascript file called fakeDatabase.js and use this as our database. 

We will be using a simple GraphQL framework called graphql-yoga.

Step 1

Create a folder called GraphQLProject

cd GraphQLProject 

npm init

npm install graphql-yoga

Step 2

Create a sampleData.js file inside the src folder with the following contents. If there is no src folder, create it.

const users = [ { id: ‘1001’, name: ‘User 1’, email: ‘’, }, { id: ‘1002’, name: ‘User 2’, email: ‘’, }, { id: ‘1003’, name: ‘User 3’, email: ‘’, } ] const movies = [ { id: ‘2001’, title: ‘Goldeneye’, reviews: ‘3001’, }, { id: ‘2002’, title: ‘Wreck-It Ralph’, reviews: ‘3002’, }, { id: ‘2003’, title: ‘Lego Movie’, reviews: ‘3002’, } ] const reviews = [ { id: ‘3001’, movie: ‘2001’, reviewText: ‘Goldeneye review…’, rating: 4, user: ‘1002’, }, { id: ‘3002’, movie: ‘2002’, reviewText: ‘Wreck-It Ralph review…’, rating: 5, user: ‘1001’, }, { id: ‘3003’, movie: ‘2001’, reviewText: ‘Goldeneye review 2…’, rating: 2, user: ‘1002’, }, { id: ‘3004’, movie: ‘2003’, reviewText: ‘Lego Movie review…’, rating: 5, user: ‘1002’, } ] module.exports = { users, movies, reviews, }

This is our sample – fake database.

Step 3

Create an index.js file within the src folder and add the following content to it.

const { GraphQLServer } = require(‘graphql-yoga’) const { users, movies, reviews } = require(‘./sampleData’) // Scalar types: String, Int, Float, Boolean, ID const typeDefs = ` type Query { users: [User]! } type User { id: ID! name: String! email: String! } ` const resolvers = { Query: { users(parent, args, context, info) { return context.users } } } const server = new GraphQLServer({ typeDefs, resolvers, context: { users, movies, reviews } }) server.start(() => console.log(‘Server is running on localhost:4000’))

Step 4

Now open the terminal and run the command 

node src/index.js

Our GraphQL server is running at port 4000 this time.

Open localhost:4000 in your browser

This is a user interface you can use to send queries to the GraphQL server.

We will write a simple Query to fetch all users.

This will fetch us the results as below


We just created a GraphQL API that fetches a list of users from the database, in this case a fake database.

Now let us understand how to test this. 

Testing a GraphQL API

Options: RestAssured, RestSharp, Karate,  TestGraphQL

Steps to test on Rest Assured 

Rest Assured for GraphQL code sample

We will be testing the GraphQL API at this link

Play around with the SpaceX GraphQL API at the above link.

The code sample can be obtained at

Steps to run this code

  1. Download folder or Git clone
  2. Open this code on Intellij IDEA or Eclipse
  3. Allow Eclipse or Intellij to import the dependencies
  4. Open the
  5. Run it

The results are as follows:

Steps to test on Rest Sharp

  1. Convert the GraphQL query into Stringified JSON format – click here to do it online
  2. Secondly, pass the converted String as Body parameters to the Request
  3. Validate the response

  1. [SetUp]
  2.       public void Setup()
  3.       {
  4.           _restClient = new RestClient(“http://localhost:4000”);
  5.       }
  6.       [Test]
  7.       public void TestGraphQL()
  8.       {
  9.          //Arrange
  10.           _restRequest = new RestRequest(“/graphql” , Method.POST);
  11.           _restRequest.AddHeader(“Content-Type”, “application/json”);
  12.           _restRequest.AddParameter(“application/json”,
  13.               “{\”query\”:”query{\n  users{\n    name\n  }\n}”,
  14.               ParameterType.RequestBody);
  15.          //Act
  16.           _restResponse  = _restClient.Execute(_restRequest);
  17.          //Assert
  18.          //do validation
  19.           Console.WriteLine(“Printing results for fun : “+_restResponse.Content);
  20.           }

Steps to test on TestGraphQL

Add the below maven dependency to your Java project and you are done.

com.vimalselvam test-graphql-java 1.0.0

A sample test can be found here

Read more here – Graphql vs Rest.

Test automation made easy

Start your smart continuous testing journey today with Testsigma.



How To Write Calculator Test Cases With Sample Test Cases
How To Write Calculator Test Cases? With Sample Test Cases
Why Test Artificial Intelligence (AI)
Why Test Artificial Intelligence (AI)?
Calendar Test Cases How to Write (Samples)
Calendar Test Cases | How to Write? (Samples)