user2932893
user2932893

Reputation: 449

Subscriptions in GraphQL in ASP.NET Core. How do I switch to SubscriptionDocumentExecuter

Ok I am perplexed. After following long tutorial installing Nuget package : "GraphQL.SystemReactive" and "GraphQL.Server.Transports.Subscriptions.WebSockets".

I receive a message on GraphQL Playground that subscription are no longer supported

DocumentExecuter does not support executing subscriptions. You can use SubscriptionDocumentExecuter from GraphQL.SystemReactive package to handle subscriptions. occurred"

I have no idea how I can switch DocumentExecuter? In Schema or Subscription there is no such method to override:

protected override IExecutionStrategy SelectExecutionStrategy(ExecutionContext context)
{
    return context.Operation.OperationType switch
    {
        OperationType.Subscription => SubscriptionExecutionStrategy.Instance,
        _ => base.SelectExecutionStrategy(context)
    };
}

How do I fix this? This is my Schema

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

This is my subscription definition:

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

Upvotes: 1

Views: 947

Answers (1)

user2932893
user2932893

Reputation: 449

Ok You don't need to create your Middleware or Controller to deal with this.

DocumentExecuter does not support executing subscriptions as you can see in the code.

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}")
    };
}

I just create my version of the DocumentExecuter that don't throw this Exception.

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

And added Dependency Injection in ConfigureServices method in ASP.NET CORE startup.cs

services.AddSingleton<IDocumentExecuter, MyDocumentExecuter>();

Now everthing works.

Upvotes: 1

Related Questions