Test Automation Stack

What is Graphql

Graphql is a query language for APIs which allows us to query to get exactly what we need in the response data.

More about graphql basics can be found here

Parts of Grapqhl request body

A typical graphql request body contains

  1. query : Specifying what all the data we need in response body
  2. mutation : To create new data
  3. variables : To pass dynamic values
  4. operation name : To specify what operation the query is performing, this also comes handy when we have to execute multiple queries

Writing our first test to query data from graphql endpoint

A sample get query would look like below, where we are fetching all the Products and we need product id, name, description and price in the response. Hence we are passing these details in the getAllProducts query

Now if we need to write our cypress test for the above example, it will look like

Mutation to create new entity

In REST we use POST method to create a new entity but in Graphql we have to use mutation to create a new entity and the sample graphql mutation to create new product looks like below

Let’s test this by writing a cypress test

As we can see only the body has been changed, where we are using mutation to pass the details for creating new product and rest of the request parameters remains same.

In graphql the status code is 200 always, hence we need to rely on validating the response body. In the above test, we are validating the newly created Product Name which is returned as part of response body upon successful Product creation.

Executing our tests will look like below

Code for the above examples can be found here

Leave a comment

I’m Gurudatt

Profile Pic

With over 12 years of quality assurance experience, i have used various test automation tools and testing concepts and helped organization and teams to speed up testing process effectively. I’m also a Cypress.io Ambassador and BrowserStack Champion.

Let’s connect

Discover more from Test Automation Stack

Subscribe now to keep reading and get access to the full archive.

Continue reading