I have googled cypress request with graphql but I see lots of people mentioning mock up server, stub and so on. But I am not able to find a ful example of how to use GraphQL with cy.request.
10
votes
2 Answers
22
votes
maybe you can this this try when using cy.request pretty much like the usual way you use restful in cy.request
example your query name is findUser with variable of username
your query should look something like findUser(username:"hello"){id, name} and so on
but instead of just this, you need to pass it as json if it is a query then it'll be {"query": findUser(username:"hello"){id, name}} this will actually be your body.
an example is below...
const query = `{
findUser(username:"hello") {
id
}
}`;
cy.request({
url: 'http://localhost/graphql/', // graphql endpoint
body: { query }, // or { query: query } depending if you are writing with es6
failOnStatusCode: false // not a must but in case the fail code is not 200 / 400
}).then((res) => {
cy.log(res);
});