我们在 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/