|
| 1 | +package io.vertx.example.web.graphql; |
| 2 | + |
| 3 | +import graphql.GraphQL; |
| 4 | +import graphql.schema.DataFetchingEnvironment; |
| 5 | +import graphql.schema.GraphQLSchema; |
| 6 | +import graphql.schema.idl.RuntimeWiring; |
| 7 | +import graphql.schema.idl.SchemaGenerator; |
| 8 | +import graphql.schema.idl.SchemaParser; |
| 9 | +import graphql.schema.idl.TypeDefinitionRegistry; |
| 10 | +import io.vertx.core.AbstractVerticle; |
| 11 | +import io.vertx.core.Launcher; |
| 12 | +import io.vertx.core.http.HttpServerOptions; |
| 13 | +import io.vertx.ext.web.Router; |
| 14 | +import io.vertx.ext.web.handler.graphql.*; |
| 15 | +import org.reactivestreams.Publisher; |
| 16 | +import org.reactivestreams.Subscriber; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring; |
| 22 | + |
| 23 | +public class SubscriptionServer extends AbstractVerticle { |
| 24 | + |
| 25 | + public static void main(String[] args) { |
| 26 | + Launcher.executeCommand("run", SubscriptionServer.class.getName()); |
| 27 | + } |
| 28 | + |
| 29 | + private List<Link> links; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void start() { |
| 33 | + prepareData(); |
| 34 | + |
| 35 | + final Router router = Router.router(vertx); |
| 36 | + router.route("/graphql").handler(ApolloWSHandler.create(createGraphQL())); |
| 37 | + |
| 38 | + final HttpServerOptions httpServerOptions = new HttpServerOptions() |
| 39 | + .setWebsocketSubProtocols("graphql-ws"); |
| 40 | + vertx.createHttpServer(httpServerOptions) |
| 41 | + .requestHandler(router) |
| 42 | + .listen(8080); |
| 43 | + } |
| 44 | + |
| 45 | + private void prepareData() { |
| 46 | + User peter = new User("Peter"); |
| 47 | + User paul = new User("Paul"); |
| 48 | + User jack = new User("Jack"); |
| 49 | + |
| 50 | + links = new ArrayList<>(); |
| 51 | + links.add(new Link("https://vertx.io", "Vert.x project", peter)); |
| 52 | + links.add(new Link("https://www.eclipse.org", "Eclipse Foundation", paul)); |
| 53 | + links.add(new Link("http://reactivex.io", "ReactiveX libraries", jack)); |
| 54 | + links.add(new Link("https://www.graphql-java.com", "GraphQL Java implementation", peter)); |
| 55 | + } |
| 56 | + |
| 57 | + private GraphQL createGraphQL() { |
| 58 | + String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString(); |
| 59 | + |
| 60 | + SchemaParser schemaParser = new SchemaParser(); |
| 61 | + TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema); |
| 62 | + |
| 63 | + RuntimeWiring runtimeWiring = newRuntimeWiring() |
| 64 | + .type("Subscription", builder -> builder.dataFetcher("links", this::linksFetcher)) |
| 65 | + .build(); |
| 66 | + |
| 67 | + SchemaGenerator schemaGenerator = new SchemaGenerator(); |
| 68 | + GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); |
| 69 | + |
| 70 | + return GraphQL.newGraphQL(graphQLSchema) |
| 71 | + .build(); |
| 72 | + } |
| 73 | + |
| 74 | + private Publisher<Link> linksFetcher(DataFetchingEnvironment env) { |
| 75 | + return subscriber -> { |
| 76 | + sendData(subscriber, 0); |
| 77 | + }; |
| 78 | + } |
| 79 | + |
| 80 | + private void sendData(Subscriber<? super Link> subscriber, int index) { |
| 81 | + if (index >= links.size()) { |
| 82 | + subscriber.onComplete(); |
| 83 | + } else { |
| 84 | + subscriber.onNext(links.get(index)); |
| 85 | + |
| 86 | + vertx.setTimer(1000, t -> sendData(subscriber, index + 1)); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments