ReactiveX java library in order to use GraphQL Subscription with RxJava2 on WebSocket based on Apollo Protocol.
- connect to a GraphQL server
- Handle init payload
- Subscribe/Unsubscribe which return Observables
- Close connection
- Error handling (partial)
- Encoders / Decoders (generic objects)
jdk version >= 1.8
dependency {
compile 'com.github.billybichon:rx-livegql:1.0'
}
<dependency>
<groupId>com.github.billybichon</groupId>
<artifactId>rx-livegql</artifactId>
<version>1.0</version>
</dependency>
Just copy the rx-livegql-1.0.jar
inside your libs folder and be sure to use:
dependency {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
RxLiveGQL rxliveGQL = new RxLiveGQL();
rxLiveGQl.connect("ws://your.url").subscribe((str) -> {
// successfully connect to the server on the first next
}, throwable -> {
// an error occurred while connecting with the server
}, () -> {
// on complete, the connection is successfully closed
});
rxliveGQL.initServer().subscribe(() -> {
// successfully initialize the connection with the server
}, throwable -> {
// an error occurred while initialize the connection with the server
});
// subscribe
rxLiveGQL.subscribe("query to subscribe", "tag").subscribe(str -> {
// str contains the data of the subscription
}, throwable -> {
// some error occurred or the connection has been closed
});
// Unsubscribe
rxLiveGQL.unsubscribe("tag").subscribe(() -> {
// successfully unsusbcribe
}, throwable -> {
// some error occurred
});
// subscribe
rxLiveGQL.subscribe("query to subscribe", "tag", Obj.Decoder.class).subscribe(obj -> {
// obj contains the data of the subscription transform into an object by the decoder
}, throwable -> {
// some error occurred or the connection has been closed
});
// Unsubscribe
rxLiveGQL.unsubscribe("tag").subscribe(() -> {
// successfully unsubscribe
}, throwable -> {
// some error occurred
});
rxLiveGQL.closeConnection().subscribe(() -> {
// successfully close connection
}, throwable -> {
// some error occurred
});
Libraries on which liveGQL depends:
- RxJava used to implement ReactiveX design
- Tyrus for managing websocket
- Gson for handling json object
- Not sure all errors are catched.