Search code examples
graphqlnestjspublish-subscribesubscription

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


I would like to send to clients different subscription response, based on some condition. withFilter filter and send response only when condition is met. I want to send two different response based on some condition - map response, based on condition. Is there any withMap or something like that?


Solution

  • Looks like you could use the resolve property and map the rsponse from within that.

    Example from the docs:

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