当使用with filter时,提供一个filter函数,该函数与有效负载(发布的值)、变量、上下文和操作信息一起执行,并且它必须返回布尔值或Promise,指示有效负载是否应传递给订阅服务器。
以下是订阅解析程序的定义,其中with filter将过滤掉所有不是请求的存储库的commentAdded事件。
import { withFilter } from 'graphql-subscriptions';
const rootResolver = {
Query: () => { ... },
Mutation: () => { ... },
Subscription: {
commentAdded: {
subscribe: withFilter(() => pubsub.asyncIterator('commentAdded'), (payload, variables) => {
return payload.commentAdded.repository_name === variables.repoFullName;
}),
}
},
};
以上内容是从官网上直接翻译过来的, 如果想了解更多详情的朋友们可以去官网了解一下。
https://www.apollographql.com/docs/graphql-subscriptions/setup/