Testing Graphql Mutation with Cypress

In this article, we will be seeing how we can write mutation to delete data in GraphQL.

Pre-requisites

  1. Idea how to write Graphql mutation

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

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

To delete some data, we will be needing some data on the server-side. We have already seen how we can insert data using mutation. We will be using the same query to insert data first

mutation {
insert_users(objects: {id: "6db07512-4f09-44ab-ad13-10118f0832fa", name: "TestName",
rocket: "TestNameRocket"}){
returning{
id
name
rocket
}
}
}

This query uses three fields id which is of type uuid, name which of type string and rocket which is again of type string. Once this query is executed a user will be inserted with the provided field. Output will look something like this

Once you have data created on the server-side or in the database lets try to delete the created data by writing graphql mutation

Keep in mind id is always unique.

You can see output response have the data deleted related to specific user id

Writing Spec to test Mutation Query

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

This method will be accepting mutation query that needs to be tested, the endpoint for API and returns the response body on execution. Creating a reusable method to create a unique id, name, and rocket at runtime

Writing specs

This spec will create a user with a unique id and delete the user with the same id and assert the fields in response.

uuid : https://www.npmjs.com/package/uuid

randomstring: https://www.npmjs.com/package/randomstring

Last updated

Was this helpful?