Testing Graphql with Cypress
Last updated
Last updated
// Some code
{
"data": {
"users": [
{
"id": "2f553874-6dbb-4cf8-b86e-dd76d2ee24fd",
"name": "sone random name",
"rocket": "some random rocket"
},
{
"id": "63227b88-c031-4bba-a39c-ae400c427727",
"name": "sone random name",
"rocket": "some random rocket"
}
]
}
}// Some code
Cypress.Commands.add('getUserInfo', (query : string, url: string) => {
if (!query) throw new Error('You need to provide valid query string');
if (!url) throw new Error('You need to provide valid endpoint');
cy.request({
url: url,
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: {
query: query,
},
}).then(({ body }) => {
return body;
});
});// Some code
it('Test to fetch users from spacex endpoint', () => {
const query = `{
users(limit: 2) {
id
name
rocket
}
}`;
cy.getUserInfo(
query,
'https://api.spacex.land/graphql/'
).then(($data) => {
expect($data.data.users).to.have.length(2);
});
});