c# - ASP.NET Core 中 GraphQL 中的订阅。如何切换到 SubscriptionDocumentExecuter
问题描述
好吧,我很困惑。按照长教程安装 Nuget 包:“GraphQL.SystemReactive”和“GraphQL.Server.Transports.Subscriptions.WebSockets”。
我在 GraphQL Playground 上收到不再支持订阅的消息
DocumentExecuter does not support executing subscriptions. You can use SubscriptionDocumentExecuter from GraphQL.SystemReactive package to handle subscriptions. occurred"
我不知道如何切换 DocumentExecuter?在 Schema 或 Subscription 中没有这样的方法可以覆盖:
protected override IExecutionStrategy SelectExecutionStrategy(ExecutionContext context)
{
return context.Operation.OperationType switch
{
OperationType.Subscription => SubscriptionExecutionStrategy.Instance,
_ => base.SelectExecutionStrategy(context)
};
}
我该如何解决?这是我的架构
public class DragonSchema : Schema
{
private readonly DragonShopDbContext _dbContext;
private readonly OpinionMessageService _messageService;
public DragonSchema(DragonShopDbContext dbContext,
OpinionMessageService messageService,
IServiceProvider sp) : base(sp)
{
_dbContext = dbContext;
_messageService = messageService;
Subscription = new DragonSubscription(_messageService);
Query = new DragonQuery
(new DragonRepository(_dbContext));
Mutation = new DragonMutation(
new DragonExpertOpinionRepository(_dbContext),
_messageService);
}
}
这是我的订阅定义:
public class DragonSubscription : ObjectGraphType
{
public DragonSubscription(OpinionMessageService messageService)
{
Name = "Subscription";
AddField(new EventStreamFieldType
{
Name = "opinionAdded",
Type = typeof(OpinionAddedMessageType),
Resolver = new FuncFieldResolver<OpinionAddedMessage>
(c => c.Source as OpinionAddedMessage),
Subscriber = new EventStreamResolver<OpinionAddedMessage>
(c => messageService.GetMessages())
});
}
}
解决方案
好的,您无需创建中间件或控制器来处理此问题。
DocumentExecuter 不支持执行订阅,正如您在代码中看到的那样。
protected virtual IExecutionStrategy SelectExecutionStrategy(ExecutionContext context)
{
// TODO: Should we use cached instances of the default execution strategies?
return context.Operation.OperationType switch
{
OperationType.Query => ParallelExecutionStrategy.Instance,
OperationType.Mutation => SerialExecutionStrategy.Instance,
OperationType.Subscription => throw new NotSupportedException($"DocumentExecuter does not support executing subscriptions. You can use SubscriptionDocumentExecuter from GraphQL.SystemReactive package to handle subscriptions."),
_ => throw new InvalidOperationException($"Unexpected OperationType {context.Operation.OperationType}")
};
}
我只是创建了不会抛出此异常的 DocumentExecuter 版本。
public class MyDocumentExecuter : DocumentExecuter, IDocumentExecuterSub
{
protected override IExecutionStrategy SelectExecutionStrategy(ExecutionContext context)
{
return context.Operation.OperationType switch
{
OperationType.Query => ParallelExecutionStrategy.Instance,
OperationType.Mutation => SerialExecutionStrategy.Instance,
OperationType.Subscription => SubscriptionExecutionStrategy.Instance,
_ => base.SelectExecutionStrategy(context)
};
}
}
并在 ASP.NET CORE startup.cs 的 ConfigureServices 方法中添加了依赖注入
services.AddSingleton<IDocumentExecuter, MyDocumentExecuter>();
现在一切正常。
推荐阅读
- python - 查找包含列表项python的字典行的最佳方法
- wpf - WPF中ReactiveUI的底层数据绑定是否使用WPF的原生实现?
- python - 无法使用 BeautifulSoup 访问 HTML 子标签
- typescript - 使函数的行为取决于打字稿中的类型参数
- kubernetes - 如何将 Kubernetes 集群 v1.19.4 升级到 1.19.13
- python-3.x - 如何使用 python 字典?
- python - Rasa、Botfront 和 heroku:部署后前端不起作用
- jquery - 在 Wordpress 中单击按钮添加类并删除
- electron - 在监视器边框处附加窗口 [电子]
- docker - 如何将 HTML 页面添加到自托管 docker 注册表?