使用 Cypress .request 调用 GraphQL 端点 [英] Call GraphQL endpoints using Cypress .request

查看:28
本文介绍了使用 Cypress .request 调用 GraphQL 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 google 上搜索了 cypress request with graphql,但我看到很多人提到 mock up serverstub 等等.但是我找不到一个关于如何将 GraphQL 与 cy.request 一起使用的完整示例.

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.

示例如下...

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆