我们可以使用cy.request调用graphql吗?赛普拉斯

Tsu*_*una 1 javascript unit-testing graphql graphql-js cypress

我在Google上搜索过类似内容,cypress request with graphql但我看到很多人都在谈论mock up serverstub等等。但是我无法找到完整的示例以及如何cypress.iographqlusing请求一起使用。

有谁知道它是如何cy.request工作的,graphql或者在哪里可以找到它的解决方案?

在此先感谢您的帮助。

Dor*_*ora 7

也许你可以使用这个时候这个尝试cy.request非常喜欢使用通常的方式restfulcy.request

例如,您的查询名称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);
    })
Run Code Online (Sandbox Code Playgroud)