'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.



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pavelsaman