【问题标题】:SubscriptionDocumentExecutor in GraphQL DotNetGraphQL DotNet 中的 SubscriptionDocumentExecutor
【发布时间】:2021-09-02 20:01:09
【问题描述】:

我正在使用 GraphQL for .NET 和 GraphQL for .NET - Subscription Transport WebSockets 包构建新的 GraphQL API,并且我正在尝试测试我在架构中设置的一些订阅。

但是,我目前在通过 UI Playground 向 API 发送订阅请求时遇到此错误:

"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 ---"

Github 自述文件指出:

为了处理订阅,您需要一个支持此 GraphQL 操作类型的 DocumentExecuter 实例。 GraphQL.NET 主包中的 DocumentExecuter 类仅支持查询和突变。我们在 System.Reactive 包之上提供 SubscriptionDocumentExecuter 实现。

我已经深入研究了源代码,并且知道为什么会发生此错误,但我不确定在哪里/如何使用 SubscriptionDocumentExecutor 中的 SubscriptionDocumentExecutor 作为 Github 页面覆盖默认的 DocumentExecutor提到?我在哪里配置这些选项?

【问题讨论】:

    标签: .net graphql graphql-subscriptions graphql-dotnet


    【解决方案1】:

    我认为您正在 C# 端构建 Graphql 服务器。

    从版本 4.1.0 (GraphQL) 开始,您需要覆盖默认 DocumentExecutor 以使用订阅。 方法就是添加

    services.AddSingleton<IDocumentExecuter, SubscriptionDocumentExecuter>();
    

    到你的 StartUp 课程。

    而且您不需要从 nuget 安装 GraphQL.SystemReactive。 服务器包 GraphQL.Server.Transports.Subscriptions.WebSockets 已经有了。

    【讨论】:

    • 谢谢威尔,我实际上已经想通了,但你是完全正确的。我会将此标记为答案。
    • 谢谢。当我从 graphiql 执行订阅查询时,我现在得到 { "data": null }
    猜你喜欢
    • 2021-06-24
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多