Skip to content

server_subscription

Etienne edited this page Mar 13, 2021 · 19 revisions

Subscription in server mode

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

The graphql-java project documents how to create subscription, in its subscription doc. But once you've read that, there is a journey left to have a working implementation of a Subscription.

The plugin hides all its complexity to you. This page is based on one of the samples provided in the project.

A (big) java limitation

The spring page on Web Socket describes 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

You'll find below an explanation of the Forum server sample. This sample is available on github in:

  • The Maven project, at [this URL] TODO: put the URL

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.
    • You'll find information on Reactive Subject here
  • Every 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 notified for each post creation
  • When a subscribeToNewPost subscription arrives, 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.
Clone this wiki locally