HotChocolate:如何访问“connection_init”有效负载

Luk*_*zka 5 c# .net-core asp.net-core hotchocolate

我们在connection_init期间发送额外的有效负载( Apollo的https://github.com/apollographql/subscriptions-transport-ws中的connectionParams )。

我无法在官方来源中找到有关如何提取此类信息的任何信息,也无法找到有关任何消息中间件/处理程序的任何类型的信息。

并发解决方案 graphql-dotnet 允许我像这样实现 IOperationMessageListener

public class SusbcriptionInitListener: IOperationMessageListener
{
    public Task BeforeHandleAsync(MessageHandlingContext context) => Task.CompletedTask;
    
    // This method will be triggered with every incoming message
    public async Task HandleAsync(MessageHandlingContext context)
    {
        var message = context.Message;
        
        // I can then filter for specific message type and do something with the raw playload
        if (message.Type == MessageType.GQL_CONNECTION_INIT)
        {
            string myInformation = message.Payload.GetValue("MyInfomration").ToString();
            
            DoSomethingWithMyInformation(myInformation);
        }
    }

    public Task AfterHandleAsync(MessageHandlingContext context) => Task.CompletedTask;
}
Run Code Online (Sandbox Code Playgroud)

HC有类似的东西吗?

Pas*_*enn 8

您正在寻找的是ISocketSessionInterceptor

services
   AddGraphQLServer()
   ... Your Config
   .AddSocketSessionInterceptor<AuthenticationSocketInterceptor>();
Run Code Online (Sandbox Code Playgroud)
public interface ISocketSessionInterceptor
    {
        ValueTask<ConnectionStatus> OnConnectAsync(
            ISocketConnection connection,
            InitializeConnectionMessage message,
            CancellationToken cancellationToken);

        ValueTask OnRequestAsync(
            ISocketConnection connection,
            IQueryRequestBuilder requestBuilder,
            CancellationToken cancellationToken);

        ValueTask OnCloseAsync(
            ISocketConnection connection,
            CancellationToken cancellationToken);
    }
Run Code Online (Sandbox Code Playgroud)

您可以通过覆盖来访问连接请求负载 OnConnectAsync

包含保存有效负载的InitializeConnectionMessage属性Payload