c# - HotChocolate:如何访问 "connection_init"有效负载

标签 c# asp.net-core .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;
}
HC 提供类似的东西吗?

最佳答案

您要找的是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持有有效载荷的属性

关于c# - HotChocolate:如何访问 "connection_init"有效负载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65703280/

相关文章:

c# - 类库项目中的 .NET 设置文件

c# - 创建窗口句柄时出错

c# - 未找到映射

c# - 如何提高 .NET Core Web API 的性能?

c# - 使用继承自相同接口(interface) Console .Net Core 的类进行依赖注入(inject)

c# - 如何从 C# 代码中拉伸(stretch) DataGrid 列?

.NET Core、Windows Azure 存储。在控制台应用程序上崩溃但在网络应用程序上不崩溃?

c# - 查询字符串和属性路由一起用于 Controller .NET 核心 Web API

asp.net-mvc - 在没有 AspNetCore Web 应用程序的情况下使用存储在 Razor 类库 3.0 (RCL) 中的 Razor 模板

c# - 找不到命令 'dotnet ef'