Skip to main content
GraphQL

Subscriptions (SSE pub/sub)

QueryParseValidateExpert

What you will learn

  • Subscriptions run over WebSocket (graphql-ws) or SSE โ€” test connection lifecycle: subscribe, receive, unsubscribe, reconnect
  • Assert message ordering in the subscription stream โ€” events can arrive in unexpected order under load
  • Test backpressure: if the client processes slowly, server must buffer or drop โ€” never OOM

Description

Automate a real-time GraphQL-style subscription stream: subscribe, trigger an event, and assert the pushed message arrives in order.

Test Scenario

// Subscribe over SSE, publish, and wait for the pushed event (Node EventSource or Playwright request streaming).
const es = new EventSource(baseUrl + '/api/graphql-subscriptions?events=ORDER_CREATED');
const received = new Promise((resolve) => {
  es.addEventListener('ORDER_CREATED', (e) => resolve(JSON.parse(e.data)));
});

await request.post('/api/graphql-subscriptions', {
  data: { event: 'ORDER_CREATED', data: { id: 'order-9', total: 42 } },
});

const msg = await received;
expect(msg.data.id).toBe('order-9');
es.close();
Endpoint/api/graphql-subscriptions

Testing Tips

  • โ€ขSubscribe with EventSource to /api/graphql-subscriptions?events=ORDER_CREATED โ€” the first frame is event: connected
  • โ€ขPublish via POST /api/graphql-subscriptions { event, data }; the same payload is pushed to subscribers
  • โ€ขAssert the Nth pushed event by type/id; ignore the periodic heartbeat frames and reconnect on drop
// Playwright API Testing - GraphQL
import { test, expect } from '@playwright/test';
test('GraphQL query', async ({ request }) => {
const response = await request.post('/api/graphql', {
data: {
query: `
query {
users {
id
name
email
}
}
`
}
});
expect(response.ok()).toBeTruthy();
const { data } = await response.json();
expect(data.users).toBeDefined();
});

Challenge ID: graphql-subscriptions

2 frameworks available

API Playground

Send requests and inspect responses

Endpoint: /api/graphql-subscriptions

Press Enter to send