Skip to content

Commit

Permalink
Add GraphQL subscription client example
Browse files Browse the repository at this point in the history
  • Loading branch information
rogelio-o committed Aug 22, 2019
1 parent caf759e commit 391a297
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.vertx.example.web.graphql;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Launcher;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.WebSocket;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.handler.graphql.ApolloWSMessageType;

public class SubscriptionClient extends AbstractVerticle {

public static void main(String[] args) {
Launcher.executeCommand("run", SubscriptionClient.class.getName());
}

@Override
public void start() {
HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
httpClient.webSocket("/graphql").setHandler(websocketRes -> {
if (websocketRes.succeeded()) {
WebSocket webSocket = websocketRes.result();

webSocket.handler(message -> {
System.out.println(message.toJsonObject().encodePrettily());
});

JsonObject request = new JsonObject()
.put("id", "1")
.put("type", ApolloWSMessageType.START.getText())
.put("payload", new JsonObject()
.put("query", "subscription { links { url, postedBy { name } } }"));
webSocket.write(request.toBuffer());
} else {
websocketRes.cause().printStackTrace();
}
});
}

}

0 comments on commit 391a297

Please sign in to comment.