SunAndMoon91
SunAndMoon91

Reputation: 81

How to send GraphQLrequest using Cypress

I wonder if there is a way to send GraphQL mutations using Cypress? There is cy.intercept() but this is more for waiting for responses.

Upvotes: 0

Views: 406

Answers (1)

pavelsaman
pavelsaman

Reputation: 8322

You can use cy.request(), you only need to know how GraphQL works and what format of the payload you need to send to your endpoint.

An example could be:

describe('GraphQl example', () => {
  it('Send req to graphql endpoint', () => {
    const query = `{ 
      speakers(name: "Miloš") {
        id
        firstName
        lastName
      }
    }`;
    cy
      .request({
        url: 'https://demagog.cz/graphql',
        method: 'POST',
        body: { query }
      })
      .then(res => {
        cy
          .log(res);
      });
  });
});

And I easily get a successful response with data:

enter image description here

Things you need to know:

  • how to use GraphQL endpoints, how to form queries
  • how to use cy.request()
  • and of course some Cypress basics

Upvotes: 2

Related Questions