typegraphql - 如何使用上下文而不是 args 过滤 type-graphql 订阅?
问题描述
我正在尝试向不同的用户发送通知,并希望每个用户只看到他的。我已经成功地用@Args 做你了。
以下是订阅:
@Subscription(() => Notification, {
topics: "NOTIFICATIONS",
filter: ({ payload, args }: ResolverFilterData<Notification, NewNotificationArgs>) => {
return payload.userId === args.userId;
},
})
newNotification(
@Root() notification: Notification,
@Args() { userId }: NewRequestArgs
): Notification {
return notification;
}
和突变:
@Mutation(() => Boolean)
async createNotification(
@PubSub("NOTIFICATIONS") notifyAboutNewNotification: Publisher<Notification>
): Promise<Boolean> {
const notification = await Notification.create({
userId: <some id>,
message: <some message>
}).save();
await notifyAboutNewRequest(notification);
return true;
}
现在我希望你使用一个 userId,我已经将它作为 cookie 存储在 context.req.session.userId 中。
解决方案
ResolverFilterData
包含有效载荷、参数、上下文和信息。所以你可以通过 filter: ({ context }) => { ... }
.
推荐阅读
- java - 当需要移植 JVM 时,Java 虚拟机在架构上运行 java 的目的是什么?
- python - 如何使用 NLTK 获取多个单词的同义词?
- javascript - Receiving Post data (not form) in Express
- amazon-web-services - 如果 cloudformation 堆栈被删除并且相同的安全组已存在,则重新创建 SecurityGroup
- typescript - How can I extend a TypeScript interface and infer the arguments?
- phpstorm - Select next occurence with IdeaVim in PhpStorm
- java - 如何使元素被选中的概率降低两倍?
- javascript - 如何在特定日期和时间从谷歌上的操作发送通知
- php - PHP - 创建依赖下拉列表
- vue.js - Vue 插件 - Vue 已定义但从未使用过