Subscription in server mode


The server mode helps to create Java GraphQL server-side application, as explained in the page.

The graphql-java documents how to create subscription, on its subscription doc. But once you've read that, there is still a journey until having a working implementation of Subscription.

The plugin hides all its complexity to you. You'll study here the sample provided in the project.

A (big) java limitation

The spring page on Web Socket states this limitation.

The consequence for GraphQL server is that a Java server can not serve both standard HTTP request (POST/GET) and WebSocket request on the same URL.

As a consequence, Subscription must be exposed on a specific path.

As a consequence, this plugin exposes the subscription on this URL:

  • If packaged as a jar: the path is /graphql/subscription
  • If packaged as a war: the paths is /WebAppContext/graphql/subscription

Sample explanation

The Subscription server sample, in the graphql-maven-plugin-samples-Forum-server sample does this:

  • A Reactive Subject is created at startup, as a Spring Bean.
  • All post creation is notified to the onNext(Post) method of this Subject.
    • As of data creation, the relevant Data Fetcher is the mutation one, available on github. Take a look at the end of the createPost method.
    • So this Subject is aware of each post creation
  • When subscribeToNewPost subscription arrived, the subscription datafetcher returns a new Publisher from this subject.
    • You can check the Data Fetcher code for this subscription on this page.
    • This new Publisher will automagicaly receive each newly created post, as it is created from the Subject that receives the new Post, from the mutation Data Fetecher (see above).
    • Then graphql-java will transmit on the Web Socket that has been created, when the subscription was submitted.