2

I'm trying to create a simple Spring GraphQL subscription handler. Here's my 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:

type Query {
    sayHello: String
}

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

Spring configuration:

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

When I try to run above subscription using graphiql hosted by the spring I receive following error:

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

When I run the same graphql request using Postman I receive following response:

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

What is causing the subscription not to return data from my controller?

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176

2 Answers2

0

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.

You'll need to enable WebSocket support in your application first. The GraphiQL UI should use the WebSocket transport transparently for this.

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
0

May be you missed to use websocket property in your application.properties files

Add given property

spring.graphql.websocket.path=/graphql

Also remember to add websocket dependency

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
        
sahil
  • 11
  • 1