diff --git a/package.json b/package.json index b3a40a70c..58b2e1299 100644 --- a/package.json +++ b/package.json @@ -189,6 +189,7 @@ "fs-extra": "^11.2.0", "fs-teardown": "^0.3.0", "glob": "^11.0.0", + "graphql-ws": "^5.16.0", "jsdom": "^25.0.1", "json-bigint": "^1.0.0", "lint-staged": "^15.2.10", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 96e9a96d2..12258e584 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -151,6 +151,9 @@ devDependencies: glob: specifier: ^11.0.0 version: 11.0.0 + graphql-ws: + specifier: ^5.16.0 + version: 5.16.0(graphql@16.9.0) jsdom: specifier: ^25.0.1 version: 25.0.1 @@ -5609,10 +5612,18 @@ packages: resolution: {integrity: sha512-EtKwoO6kxCL9WO5xipiHTZlSzBm7WLT627TqC/uVRd0HKmq8NXyebnNYxDoBi7wt8eTWrUrKXCOVaFq9x1kgag==} dev: true + /graphql-ws@5.16.0(graphql@16.9.0): + resolution: {integrity: sha512-Ju2RCU2dQMgSKtArPbEtsK5gNLnsQyTNIo/T7cZNp96niC1x0KdJNZV0TIoilceBPQwfb5itrGl8pkFeOUMl4A==} + engines: {node: '>=10'} + peerDependencies: + graphql: '>=0.11 <=16' + dependencies: + graphql: 16.9.0 + dev: true + /graphql@16.9.0: resolution: {integrity: sha512-GGTKBX4SD7Wdb8mqeDLni2oaRGYQWjWHGKPQ24ZMnUtKfcsVoiv4uX8+LJr1K6U5VW2Lu1BwJnj7uiori0YtRw==} engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0} - dev: false /hard-rejection@2.1.0: resolution: {integrity: sha512-VIZB+ibDhx7ObhAe7OVtoEbuP4h/MuOTHJ+J8h/eBXotJYl0fBgR72xDFCKgIh22OJZIOVNxBMWuhAr10r8HdA==} diff --git a/test/node/graphql-api/graphql-subscription.test.ts b/test/node/graphql-api/graphql-subscription.test.ts new file mode 100644 index 000000000..ea755198f --- /dev/null +++ b/test/node/graphql-api/graphql-subscription.test.ts @@ -0,0 +1,62 @@ +// @vitest-environment node +import { graphql } from 'msw' +import { setupServer } from 'msw/node' +import { createClient } from 'graphql-ws' + +const server = setupServer() + +beforeAll(() => { + server.listen() +}) + +afterEach(() => { + server.resetHandlers() +}) + +afterAll(() => { + server.close() +}) + +it('intercepts and mocks a GraphQL subscription', async () => { + const api = graphql.link('http://localhost:4000/graphql') + + server.use( + api.pubsub.handler, + api.subscription('OnCommendAdded', () => { + api.pubsub.publish({ + data: { + commentAdded: { + id: '1', + text: 'Hello world', + }, + }, + }) + }), + ) + + const client = createClient({ + url: 'ws://localhost:4000/graphql', + }) + + const subscription = client.iterate({ + query: ` +subscription OnCommendAdded { + commentAdded { + id + text + } +} + `, + }) + + await expect(subscription.next()).resolves.toMatchObject({ + value: { + data: { + commentAdded: { + id: '1', + text: 'Hello world', + }, + }, + }, + }) +})