使用Cypress .request调用GraphQL端点 [英] Call GraphQL endpoints using Cypress .request
问题描述
我已经用graphql 搜索了 cypress请求,但是我看到很多人提到
模拟服务器
, stub
等.但是我找不到如何在 cy.request中使用GraphQL的有效示例..
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.
推荐答案
也许您可以在使用 cy.request
时尝试这种方式,这与您使用 restful
的通常方式非常相似>在 cy.request
maybe you can this this try when using cy.request
pretty much like the usual way you use restful
in cy.request
示例查询名称为 findUser
,变量为 username
您的查询应类似于 findUser(username:"hello"){id,name}
等
example your query name is findUser
with variable of username
your query should look something like findUser(username:"hello"){id, name}
and so on
但不只是此,您还需要将其作为 json
传递给它,如果它是一个查询,那么它将是 {"query":findUser(username:"hello"){id,name}}
实际上就是您的身体.
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);
})
这篇关于使用Cypress .request调用GraphQL端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!