feat(example): minimal GraphQL with query/mutation and an SSE subscription over rooms

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-06-28 13:37:37 -05:00
1 parent 0c29c76a8a
commit c39eb667ac
5 files changed
+125

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
import { describe, it, expect, afterAll } from 'vitest';
import { app } from '../../example/graphql';
let server: any; let base: string;
afterAll(() => server?.close());
const gql = (query: string, variables?: any) =>
fetch(`${base}/graphql`, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ query, variables }) }).then((r) => r.json() as Promise<any>);
describe('graphql example', () => {
it('runs a query and a mutation, and streams a subscription over SSE', async () => {
server = await app.listen(0); base = `http://127.0.0.1:${(server.address() as any).port}`;
expect(await gql('{ messages { text } }')).toMatchObject({ data: { messages: expect.any(Array) } });
const res = await fetch(`${base}/graphql/stream`, { headers: { accept: 'text/event-stream' } });
const reader = res.body!.getReader();
await new Promise((r) => setTimeout(r, 50)); // let the subscription subscribe to the room before publishing
await gql('mutation($t:String!){ postMessage(text:$t){ text } }', { t: 'hello-sub' });
const dec = new TextDecoder(); let buf = '';
while (!buf.includes('hello-sub')) buf += dec.decode((await reader.read()).value);
expect(buf).toContain('hello-sub');
await reader.cancel();
});
});