forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubscriptionServer.java
81 lines (64 loc) · 2.79 KB
/
SubscriptionServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package io.vertx.example.web.graphql;
import graphql.GraphQL;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLSchema;
import graphql.schema.idl.RuntimeWiring;
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import io.reactivex.Flowable;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Launcher;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.graphql.ApolloWSHandler;
import io.vertx.reactivex.RxHelper;
import org.reactivestreams.Publisher;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;
public class SubscriptionServer extends AbstractVerticle {
public static void main(String[] args) {
Launcher.executeCommand("run", SubscriptionServer.class.getName());
}
private List<Link> links;
@Override
public void start() {
prepareData();
Router router = Router.router(vertx);
router.route("/graphql").handler(ApolloWSHandler.create(createGraphQL()));
HttpServerOptions httpServerOptions = new HttpServerOptions()
.setWebsocketSubProtocols("graphql-ws");
vertx.createHttpServer(httpServerOptions)
.requestHandler(router)
.listen(8080);
}
private void prepareData() {
User peter = new User("Peter");
User paul = new User("Paul");
User jack = new User("Jack");
links = new ArrayList<>();
links.add(new Link("https://vertx.io", "Vert.x project", peter));
links.add(new Link("https://www.eclipse.org", "Eclipse Foundation", paul));
links.add(new Link("http://reactivex.io", "ReactiveX libraries", jack));
links.add(new Link("https://www.graphql-java.com", "GraphQL Java implementation", peter));
}
private GraphQL createGraphQL() {
String schema = vertx.fileSystem().readFileBlocking("links.graphqls").toString();
SchemaParser schemaParser = new SchemaParser();
TypeDefinitionRegistry typeDefinitionRegistry = schemaParser.parse(schema);
RuntimeWiring runtimeWiring = newRuntimeWiring()
.type("Subscription", builder -> builder.dataFetcher("links", this::linksFetcher))
.build();
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema graphQLSchema = schemaGenerator.makeExecutableSchema(typeDefinitionRegistry, runtimeWiring);
return GraphQL.newGraphQL(graphQLSchema)
.build();
}
private Publisher<Link> linksFetcher(DataFetchingEnvironment env) {
return Flowable.interval(1, TimeUnit.SECONDS) // Ticks
.zipWith(Flowable.fromIterable(links), (tick, link) -> link) // Emit link on each tick
.observeOn(RxHelper.scheduler(context)); // Observe on the verticle context thread
}
}