3

我们在 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;
}

HC 是否提供类似的服务?

4

1 回答 1

4

您正在寻找的是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保存有效负载的属性

于 2021-01-17T17:11:48.483 回答