Skip to content

Commit ad4304e

Browse files
committed
Extend plugins to also apply their logic to subscriptions
1 parent e4902e5 commit ad4304e

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/core/graphql/gql-context.host.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ export class GqlContextHostImpl implements GqlContextHost, OnModuleDestroy {
5252
});
5353
};
5454

55+
onSubscribe: Plugin['onSubscribe'] = ({
56+
subscribeFn,
57+
setSubscribeFn,
58+
args,
59+
}) => {
60+
const ctx = args.contextValue;
61+
setSubscribeFn((...args) => {
62+
return this.als.run(ctx, subscribeFn, ...args);
63+
});
64+
};
65+
5566
onModuleDestroy() {
5667
this.als.disable();
5768
}

src/core/graphql/graphql-error-formatter.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ export class GraphqlErrorFormatter {
5151
}),
5252
});
5353

54+
onSubscribe: Plugin['onSubscribe'] = () => ({
55+
onSubscribeError: ({ error, setError }) => {
56+
const formatted = this.formatError(error);
57+
setError(formatted);
58+
},
59+
});
60+
5461
formatError = (error: unknown) => {
5562
if (!(error instanceof GraphQLError)) {
5663
// I don't think this happens.

src/core/graphql/graphql-session.plugin.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,11 @@ export class GraphqlSessionPlugin {
99
args.contextValue.session$.complete();
1010
},
1111
});
12+
onSubscribe: Plugin['onSubscribe'] = () => ({
13+
onSubscribeResult: ({ args }) => ({
14+
onEnd: () => {
15+
args.contextValue.session$.complete();
16+
},
17+
}),
18+
});
1219
}

0 commit comments

Comments
 (0)