Fail One GraphQL Operation
GraphQL clients often send every operation to /graphql. Use graphqlOperation to fail one operation without breaking unrelated queries on the same URL.
await injectChaos(page, { seed: 42, network: { failures: [{ urlPattern: '/graphql', graphqlOperation: 'GetUser', statusCode: 503, probability: 1, }], },});
await page.goto('/profile');await expect(page.locator('[data-testid="profile-error"]')).toBeVisible();await page.getByRole('button', { name: 'Search products' }).click();await expect(page.locator('[data-testid="products"]')).toBeVisible();cy.injectChaos({ seed: 42, network: { failures: [{ urlPattern: '/graphql', graphqlOperation: 'GetUser', statusCode: 503, probability: 1, }], },});
cy.visit('/profile');cy.get('[data-testid="profile-error"]').should('be.visible');cy.findByRole('button', { name: 'Search products' }).click();cy.get('[data-testid="products"]').should('be.visible');await browser.url('/profile');await injectChaos(browser, { seed: 42, network: { failures: [{ urlPattern: '/graphql', graphqlOperation: 'GetUser', statusCode: 503, probability: 1, }], },});
await $('#reload-profile').click();await expect($('[data-testid="profile-error"]')).toBeDisplayed();await $('[data-testid="search-products"]').click();await expect($('[data-testid="products"]')).toBeDisplayed();await injectChaos(page, { seed: 42, network: { failures: [{ urlPattern: '/graphql', graphqlOperation: /^GetUser$/, statusCode: 503, probability: 1, }], },});
await page.goto('http://localhost:3000/profile');await page.waitForSelector('[data-testid="profile-error"]');await page.click('[data-testid="search-products"]');await page.waitForSelector('[data-testid="products"]');graphqlOperation is an additional matcher. urlPattern and methods still apply, so a rule can target only POST GetUser or only persisted-query GET GetUser when needed.
If a rule with graphqlOperation seems inactive, check the chaos log for a reason: 'graphql-body-unparseable' diagnostic. Multipart, streaming, and XHR Blob bodies cannot be inspected for an operation name and emit this event with applied: false instead of matching.