Testing Graphql with Cypress

Graphql is a query language for API. GraphQL gives you what you ask for what you want in a single query.

Pre-requisites

  • Idea how to write Graphql query

  • The idea on how cy.request works. For more refer to documentation https://docs.cypress.io/api/commands/request

  • You can use open source graphql playground to practice queries https://api.spacex.land/graphql/.

  • We will be using this open-source API to automate using Cypress.

Writing Query

We will be seeing very super simple query to automate

// Some code

{
users(limit: 2) {
id
name
rocket
}
}

This query will return two users from the server. Id in output response will be unique always and name and rocket can be any random data.

The Output will look something like this

Writing Spec to test Query

Create custom command to get user data in support/command.js

This method will be accepting query that needs to be tested and the URI and return the response body.

Writing specs

This spec will make a request to the Spacex endpoint and assert the response. As we are requesting data from the server for 2 users, the output response should have 2 users present in it.

Last updated