SoftwareTestPilot
All discussions
Arjun PatelJul 9

cy.intercept not matching a specific GraphQL mutation

I have 8 GraphQL calls hitting /graphql. cy.intercept('POST','/graphql') catches all of them but I only want to stub the CreateOrder mutation. Matching by req.body.operationName does not seem reliable. What is the current best practice?

cypressgraphqlnetwork
Related learningExplore Academy skill paths

2 answers

Accepted solution

Arjun PatelJul 10

Match on the request body inside cy.intercept using a function. Do NOT rely on url-only matching for GraphQL. This lets you stub exactly the CreateOrder mutation and leave the rest untouched.

Most helpful answer

Carlos MendesJul 11

One more nice pattern: keep the matcher in a helper so multiple specs can stub the same mutation without duplicating body inspection code.