Skip to content

Commit 8387bb0

Browse files
committed
test: add original payload patching test
1 parent efe98cf commit 8387bb0

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

test/node/graphql-api/graphql-subscription.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,79 @@ it('supports bypassing a subscription', async () => {
263263
done: false,
264264
})
265265
})
266+
267+
it('supports augmenting original server subscription payload', async () => {
268+
await using testServer = await createTestGraphQLServer({
269+
schema: createSchema({
270+
typeDefs: /* GraphQL */ `
271+
type Comment {
272+
text: String!
273+
}
274+
275+
type Query {
276+
comments: [Comment!]!
277+
}
278+
279+
type Subscription {
280+
commentAdded: Comment!
281+
}
282+
`,
283+
resolvers: {
284+
Subscription: {
285+
commentAdded: {
286+
async *subscribe() {
287+
yield { commentAdded: { text: 'hello world' } }
288+
},
289+
},
290+
},
291+
},
292+
}),
293+
})
294+
295+
const api = graphql.link(testServer.http.url().href)
296+
server.use(
297+
api.subscription('OnCommentAdded', async ({ subscription }) => {
298+
const commentSubscription = subscription.subscribe()
299+
commentSubscription.addEventListener('next', (event) => {
300+
// Prevent the default server-to-client forwarding.
301+
event.preventDefault()
302+
303+
// Publish the modified payload to the client.
304+
const { payload } = event.data
305+
// @ts-expect-error Missing payload type.
306+
payload.data.commentAdded.text =
307+
// @ts-expect-error Missing payload type.
308+
payload.data.commentAdded.text.toUpperCase()
309+
310+
subscription.publish(event.data.payload)
311+
312+
commentSubscription.unsubscribe()
313+
})
314+
}),
315+
)
316+
317+
const client = createClient({
318+
url: testServer.ws.url().href,
319+
})
320+
const subscription = client.iterate({
321+
query: /* GraphQL */ `
322+
subscription OnCommentAdded {
323+
commentAdded {
324+
text
325+
}
326+
}
327+
`,
328+
})
329+
330+
// Must receive the payload from the original server.
331+
await expect(subscription.next()).resolves.toEqual({
332+
value: {
333+
data: {
334+
commentAdded: {
335+
text: 'HELLO WORLD',
336+
},
337+
},
338+
},
339+
done: false,
340+
})
341+
})

0 commit comments

Comments
 (0)