首页 > 解决方案 > 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())
        });
    }
}

标签: c#asp.net-coregraphql

解决方案


好的,您无需创建中间件或控制器来处理此问题。

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>();

现在一切正常。


推荐阅读