Hotchocolate: Add EventMessage<T> method to context resolver

Created on 16 Apr 2020  ·  7Comments  ·  Source: ChilliCream/hotchocolate

Is your feature request related to a problem? Please describe.
Now i don't get how to extract event message from code-first approach in 10.4.1 not using string keys and direct casts in resolver:

descriptor.Field("onEntity")
  .Type<NonNullType<EntityType>>()
  .Subscribe(async context => await context
    .Service<ITopicEventReceiver>()
    .SubscribeAsync<string, OnEntityEventMessage>(nameof(OnEntityEventMessage)))
  .Resolver(context =>
  {
    // This part
    var message = (OnEntityEventMessage) context.ContextData["HotChocolate.Execution.EventMessage"];

    // ...
  })

Describe the solution you'd like
Add method like Service\, Resolver\, Parent\ named EventMessage\

❓ question 🌶 hot chocolate

Most helpful comment

You basically could simplyfiy your code to:

public class Subscription 
{
    [Subscribe(nameof(OnEntity))]
    public async ValueTask<IAsyncEnumerable<OnCustomerEventMessage>> SubscribeToOnEntity([Service]ITopicEventReceiver eventReceiver) => 
        eventReceiver.SubscribeAsync<string, OnEntityEventMessage>(nameof(OnEntityEventMessage)));


    public async Task<Entity> OnEntity([EventMessage] OnCustomerEventMessage message) 
    {

    }
}

you can now either use the type to bind these or just register this type like is:

SchemaBuilder.New()
    .AddSubscriptionType<Subscription>()
    ....
    .Create()

However, I think it would still be nice to have a helper around this for the case when you really want to do it as a delegate. Will add one for 10.4.2.

All 7 comments

Usually you would have a method backing your resolver in which case we compile a native resolver for your function with method level dependency injection.

public async Task<Foo> MyResolver([EventMessage]MyMessage message) 
{

}

Both the subscribe and the resolve method can be represented as methods rather than delegates.

You basically could simplyfiy your code to:

public class Subscription 
{
    [Subscribe(nameof(OnEntity))]
    public async ValueTask<IAsyncEnumerable<OnCustomerEventMessage>> SubscribeToOnEntity([Service]ITopicEventReceiver eventReceiver) => 
        eventReceiver.SubscribeAsync<string, OnEntityEventMessage>(nameof(OnEntityEventMessage)));


    public async Task<Entity> OnEntity([EventMessage] OnCustomerEventMessage message) 
    {

    }
}

you can now either use the type to bind these or just register this type like is:

SchemaBuilder.New()
    .AddSubscriptionType<Subscription>()
    ....
    .Create()

However, I think it would still be nice to have a helper around this for the case when you really want to do it as a delegate. Will add one for 10.4.2.

@michaelstaib Is this feature released in 10.4.2?

No, we just did bug fixes in 10.4.2.

I am a bit hesitant for this one since we are working on a big feature update for 11 and I think we should do that on 11 instead on 10.

Lets track this on this issue #1744

Was this page helpful?
0 / 5 - 0 ratings