Cypress w/graphql - having issues getting AUTH with testing via UI. Better way to stub mutation?

跟風遠走 提交于 2019-12-02 00:40:09

I didn't do the stub and all those, as you were asking how the mutation would work with cy.request in my other post. I did it this way and it just basically works. Hopefully this would help

I created a const first though

export const join_graphQL = (query, extra={}) => {
    return `mutation {
        ${query}(join: { email: "${extra.email}", id: "${extra.id}" }) {
                id, name, email
            }    
    }`
};

request config const

export const graphqlReqConfig = (body={}, api=graphQlapi, method='post') => {
    return {
        method,
        body,
        url: api,
        failOnStatusCode: false
    }
};

mutation query with cy.request

const mutationQuery = join_graphQL('mutationName', {
    email: "email",
    id: 38293
});

cy.request(graphqlReqConfig({
    query: mutationQuery
})).then((res) => {
    const data = res.body.data['mutationName'];  // your result
});

hopefully it's not too messy to see.

basically the fields need to be string such as "${extra.email}" else it will give you error. Not sure how the graphql works deeply but if I just do ${extra.email} I would get an error which I forgot what error it was.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!