I recently had this issue and I was able to resolve this by adding a command in the command.js in cypress's support folder.
Cypress.Commands.add('apiMutation', (mutations) => {
cy.request({
method:'POST',
url:'api/graphql',
body: { query : "mutation" + mutations }
})
})
The problem is that cy.request requires you to have a query inside the body or ofcourse that's what I feel.
You can override this by add the word mutation inside the body before the actual query(mutation). Like below
body: { query : "mutation" + mutations }
And you can make your request like:
describe("Reservation Validations", () => {
it("should return a valid addAdditions mutation", () => {
cy.apiMutation('{addAdditions(reservationId: 1001 reservedResourceId: 100 additions: { resourceId: 10 quantity: 12 }){resourceId type resortArticle}}'
).then((Response) => {
expect(Response.body.data).to.have.property("addAdditions")
expect(Response.body.data.addAdditions).to.not.be.undefined
})
})
})
cy.request. - jonrsharpecy.requestfor transport. You could decouple your tests from GraphQL entirely by using the UI to log in - I know the Cypress folks consider that an anti-pattern, but your users shouldn't have to care how you're organising the API so neither should your E2E tests. - jonrsharpe