2

I would like to be able to filter subscriptions for some operation based on objects id. For example I would like to do something like this:

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

Where the event would only be emitted if the task with id "1" has completed.

Is there a built in way of doing this with HotChocolate using some type of filtering?

or

Do I have to add this type of filtering myself, by doing something like this in the resolver:

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

Thank you

O.MeeKoh
  • 1,976
  • 3
  • 24
  • 53

1 Answers1

5

You could do something like this:

        [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;
            }
        }

To trigger it you could them publish to on-task-completed-123, which would only then be sent to subscribers who provided 123 as the taskId.