Warm tip: This article is reproduced from serverfault.com, please click

其他-如何根据GraphQL(NestJS)中的条件发送不同的订阅响应?

(其他 - How to send different subscription response based on condition in GraphQL (NestJS)?)

发布于 2020-12-16 19:02:25

我想根据某些条件向客户发送不同的订阅响应。withFilter仅在满足条件时过滤并发送响应。我想根据某些条件发送两个不同的响应-基于条件的映射响应。有没有withMap类似的东西?

Questioner
vicodin
Viewed
11
Jay McDoniel 2020-12-17 05:12:22

看起来你可以使用该resolve属性并从中映射rsponse。

来自文档的示例:

@Subscription(returns => Comment, {
  resolve(this: AuthorResolver, value) {
    // "this" refers to an instance of "AuthorResolver"
    return value;
  }
})
commentAdded() {
  return pubSub.asyncIterator('commentAdded');
}