Search code examples
.netgraphqlgraphql-subscriptionsgraphql-dotnet

SubscriptionDocumentExecutor in GraphQL DotNet


I am building a new GraphQL API using the GraphQL for .NET and the GraphQL for .NET - Subscription Transport WebSockets packages and I am trying to test some Subscriptions I have set up in my schema.

However, I am currently getting this error when sending a Subscription request to the API through the UI Playground:

"GraphQL.Execution.UnhandledError: Error executing document.
---> System.NotSupportedException: DocumentExecuter does not support executing subscriptions. You can use SubscriptionDocumentExecuter from GraphQL.SystemReactive package to handle subscriptions.
   at GraphQL.DocumentExecuter.SelectExecutionStrategy(ExecutionContext context) in /_/src/GraphQL/Execution/DocumentExecuter.cs:line 304
   at GraphQL.DocumentExecuter.BuildExecutionContext(ExecutionOptions options, Document document, Operation operation, Variables variables, Metrics metrics) in /_/src/GraphQL/Execution/DocumentExecuter.cs:line 270
   at GraphQL.DocumentExecuter.ExecuteAsync(ExecutionOptions options) in /_/src/GraphQL/Execution/DocumentExecuter.cs:line 142
   --- End of inner exception stack trace ---"

The Github readme states this:

For handling subscriptions you'll need an instance of DocumentExecuter that supports this GraphQL operation type. DocumentExecuter class from the main GraphQL.NET package supports only queries and mutations. We provide SubscriptionDocumentExecuter implementation on top of System.Reactive packages.

I've dug through the source code and I know why this error is happening but I'm not sure where/how to override the default DocumentExecutor with the SubscriptionDocumentExecutor from the GraphQL.SystemReactive package as the Github page mentions? Where do I configure these options?


Solution

  • I think you are building Graphql server on C# side.

    Starting from Verion 4.1.0 (GraphQL) you are reuqired to override the default DocumentExecutor to use subscription. The way to do it is simply add

    services.AddSingleton<IDocumentExecuter, SubscriptionDocumentExecuter>();
    

    to your StartUp class.

    And you dont need to install GraphQL.SystemReactive from nuget. The server package GraphQL.Server.Transports.Subscriptions.WebSockets already have that.