Skip to content

Commit

Permalink
feat: include subscription id in onSubscriptionEnd hook (#963)
Browse files Browse the repository at this point in the history
* feat: include subscription id in onSubscriptionEnd hook

* chore: check that the subscription id matches
  • Loading branch information
RomuloVitoi authored Feb 17, 2023
1 parent 6e0b15d commit 0e8dc55
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ fastify.graphql.addHook('onSubscriptionResolution', async (execution, context) =
This hook will be triggered when a subscription ends.

```js
fastify.graphql.addHook('onSubscriptionEnd', async (context) => {
fastify.graphql.addHook('onSubscriptionEnd', async (context, id) => {
await asyncMethod()
})
```
Expand Down
2 changes: 1 addition & 1 deletion examples/hooks-subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function start () {
console.log('onSubscriptionResolution called')
})

app.graphql.addHook('onSubscriptionEnd', async function (context) {
app.graphql.addHook('onSubscriptionEnd', async function (context, id) {
console.log('onSubscriptionEnd called')
})

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export interface onSubscriptionResolutionHookHandler<TData extends Record<string
export interface onSubscriptionEndHookHandler<TContext = MercuriusContext> {
(
context: TContext,
id: string | number,
): Promise<void> | void;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function onResolutionHookRunner (fn, request) {
}

function onEndHookRunner (fn, request) {
return fn(request.context)
return fn(request.context, request.id)
}

module.exports = {
Expand Down
3 changes: 2 additions & 1 deletion lib/subscription-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ module.exports = class SubscriptionConnection {
get __currentQuery () {
return print(document)
},
id,
pubsub: sc,
lruGatewayResolvers: this.lruGatewayResolvers,
reply: {
Expand Down Expand Up @@ -343,7 +344,7 @@ module.exports = class SubscriptionConnection {
async handleGQLStop (data) {
if (this.context.onSubscriptionEnd) {
try {
await onSubscriptionEndHandler({ context: this.context })
await onSubscriptionEndHandler({ context: this.context, id: data.id })
} catch (error) {
this.fastify.log.error(error)
return this.handleConnectionClose()
Expand Down
7 changes: 4 additions & 3 deletions test/subscription-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,12 @@ test('subscription - should handle onSubscriptionResolution hook errors', async
// onSubscriptionEnd
// -----------------
test('subscription - should call onSubscriptionEnd when subscription ends', async t => {
t.plan(4)
t.plan(5)
const app = await createTestServer(t)

app.graphql.addHook('onSubscriptionEnd', async (context) => {
app.graphql.addHook('onSubscriptionEnd', async (context, id) => {
t.type(context, 'object')
t.equal(id, 1)
t.ok('onSubscriptionEnd called')
})

Expand Down Expand Up @@ -364,7 +365,7 @@ test('subscription - should handle onSubscriptionEnd hook errors', async t => {
t.plan(2)
const app = await createTestServer(t)

app.graphql.addHook('onSubscriptionEnd', async (context) => {
app.graphql.addHook('onSubscriptionEnd', async (context, id) => {
throw new Error('kaboom')
})

Expand Down
6 changes: 4 additions & 2 deletions test/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,12 +540,14 @@ app.graphql.addHook('onSubscriptionResolution', function (execution, context) {
expectAssignable<MercuriusContext>(context)
})

app.graphql.addHook('onSubscriptionEnd', async function (context) {
app.graphql.addHook('onSubscriptionEnd', async function (context, id) {
expectAssignable<MercuriusContext>(context)
expectAssignable<string | number>(id)
})

app.graphql.addHook('onSubscriptionEnd', function (context) {
app.graphql.addHook('onSubscriptionEnd', function (context, id) {
expectAssignable<MercuriusContext>(context)
expectAssignable<string | number>(id)
})

expectError(() => {
Expand Down

0 comments on commit 0e8dc55

Please sign in to comment.