如何根据某些对象的 ID 过滤订阅事件?

How to filter Subscription events based on some object's id?

我希望能够根据对象 ID 过滤某些操作的订阅。例如我想做这样的事情:

subscription{
  onTaskCompleted(taskId: "1"){
    taskCompleted{
      status
      items{
        reason
        iD
      }
    }
    taskFailed{
      status
      details{
        detail
        status        
      }
    }
  }
}

仅当 ID 为“1”的任务已完成时才会发出事件。

是否有使用某种过滤类型 HotChocolate 执行此操作的内置方法?

我是否必须通过在解析器中执行类似的操作来自己添加这种类型的过滤:

if(_taskIds.Contains(taskId))
{
   TaskCompletedExecution taskFinished = new TaskCompletedExecution(taskCompleted);
   await eventSender.SendAsync(nameof(TaskListSubscriptions.OnTaskCompleted), taskFinished, 
   cancellationToken);
}

谢谢

你可以这样做:

        [SubscribeAndResolve]
        public async IAsyncEnumerable<TaskCompletedExecution> OnTaskCompletedAsync(
            string taskId,
            [Service] ITopicEventReceiver eventReceiver,
            CancellationToken cancellationToken)
        {           
            var stream = await eventReceiver.SubscribeAsync<string, TaskCompletedExecution>(
                $"on-task-completed-{taskId}", cancellationToken);
            
            await foreach (var data in stream.ReadEventsAsync().WithCancellation(cancellationToken))
            {
                yield return data;
            }
        }

要触发它,您可以将它们发布到 on-task-completed-123,然后才会将其发送给提供 123 作为任务 ID 的订阅者。