HotChocolate:如何访问 "connection_init" 负载

HotChocolate: How to access "connection_init" payload

我们将在 connection_init 期间发送额外的负载(Apollo 的 https://github.com/apollographql/subscriptions-transport-ws 中的连接参数)。

我无法在官方来源中找到有关如何提取此类信息的任何信息,也无法找到有关任何消息 middlewares/handlers 的任何类型的信息。

并发解决方案 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;
}

慧聪有没有类似的东西?

你要找的是ISocketSessionInterceptor

services
   AddGraphQLServer()
   ... Your Config
   .AddSocketSessionInterceptor<AuthenticationSocketInterceptor>();
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);
    }

您可以通过重写 OnConnectAsync 来访问连接请求负载。

InitializeConnectionMessage 包含一个 Payload 属性 来保存负载