add context subscription

This commit is contained in:
2024-10-29 02:21:18 +07:00
parent ba54d3466c
commit 9423a36eeb
7 changed files with 58 additions and 19 deletions

View File

@@ -109,7 +109,10 @@ export class MessageSchema extends PothosSchema {
...query,
data: args.input,
});
ctx.pubSub.publish('MESSAGE_SENT', message);
if (ctx.isSubscription) {
throw new Error('Not allowed');
}
ctx.http.pubSub.publish('MESSAGE_SENT', message);
return message;
},
}),
@@ -124,9 +127,12 @@ export class MessageSchema extends PothosSchema {
this.builder.subscriptionFields((t) => ({
messageSent: t.field({
subscribe: (_, __, ctx) => {
if (!ctx.isSubscription) {
throw new Error('Not allowed');
}
return {
[Symbol.asyncIterator]: () =>
ctx.pubSub.asyncIterator('MESSAGE_SENT'),
ctx.websocket.pubSub.asyncIterator('MESSAGE_SENT'),
};
},
type: this.message(), // Add the type property