Testing Graphql Mutation with Cypress
In this article, we will be seeing how we can write mutation to delete data in GraphQL.
Pre-requisites
Idea how to write Graphql mutation
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
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
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
Last updated
Was this helpful?