简体   繁体   English

简单 Spring GraphQL 订阅返回错误

[英]Simple Spring GraphQL Subscription returning error

I'm trying to create a simple Spring GraphQL subscription handler.我正在尝试创建一个简单的 Spring GraphQL 订阅处理程序。 Here's my controller:这是我的 controller:

@Controller
public class GreetingController {
    @QueryMapping
    String sayHello() {
        return "Hello!";
    }

    @SubscriptionMapping
    Flux<String> greeting(@Argument int count) {
        return Flux.fromStream(Stream.generate(() -> "Hello @ " + Instant.now()))
                .delayElements(Duration.ofSeconds(1))
                .take(count);
    }
}

Here's the GraphQL schema:这是 GraphQL 架构:

type Query {
    sayHello: String
}

type Subscription {
    greeting(count: Int): String
}

Spring configuration: Spring配置:

spring:
    graphql:
        graphiql:
            enabled: true
            path: /graphiql

When I try to run above subscription using graphiql hosted by the spring I receive following error:当我尝试使用 spring 托管的 graphiql 在订阅之上运行时,我收到以下错误:

{
  "errors": [
    {
      "isTrusted": true
    }
  ]
}

When I run the same graphql request using Postman I receive following response:当我使用 Postman 运行相同的 graphql 请求时,我收到以下响应:

{
    "data": {
        "upstreamPublisher": {
            "scanAvailable": true,
            "prefetch": -1
        }
    }
}

What is causing the subscription not to return data from my controller?是什么导致订阅不从我的 controller 返回数据?

As explained in the linked GitHub issue, a subscription requires the ability to stream data within a persistent transport connection - this is not available other plain HTTP.如链接的 GitHub 问题中所述,订阅需要能够在持久传输连接中获取 stream 数据 - 这在其他普通 HTTP 中不可用。

You'll need to enable WebSocket support in your application first .您需要先在您的应用程序中启用 WebSocket 支持 The GraphiQL UI should use the WebSocket transport transparently for this.为此,GraphiQL UI 应该透明地使用 WebSocket 传输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM