我已经搜索了cypress request with graphql
,但是我看到很多人提到mock up server
,stub
等。但是我找不到如何将GraphQl与Cy.Request一起使用的完整示例。
也许您可以在使用cy.request
时尝试此尝试,就像您在cy.request
中使用restful
的通常方式
示例您的查询名称是findUser
,带有username
的变量您的查询应该看起来像findUser(username:"hello"){id, name}
等等
但是,如果是查询,则需要将其作为json
传递,而是{"query": findUser(username:"hello"){id, name}}
,这实际上将是您的身体。
一个示例如下...
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);
});
一旦添加method: "post"
const query = `
query getItems {
items {
items {
id
}
}
}
`;
cy.request({
method: "post",
url: 'http://localhost:4000/graphql',
body: { query },
}).then((res) => {
console.log(res.body);
});