io.javalin.plugin.graphql.SubscriberGraphQL.kt Maven / Gradle / Ivy
package io.javalin.plugin.graphql
import graphql.ExecutionResult
import io.javalin.websocket.WsMessageContext
import org.reactivestreams.Subscriber
import org.reactivestreams.Subscription
import java.io.IOException
import java.util.concurrent.atomic.AtomicReference
class SubscriberGraphQL(private val wsContext: WsMessageContext) : Subscriber {
private val subscriptionRef = AtomicReference()
override fun onError(error: Throwable) {
throw error
}
override fun onSubscribe(s: Subscription?) {
subscriptionRef.set(s)
request(1)
}
override fun onComplete() {
}
override fun onNext(result: ExecutionResult) {
try {
val data = result.getData()
wsContext.send(data)
} catch (e: IOException) {
e.printStackTrace()
}
request(1)
}
private fun request(n: Int) {
val subscription = subscriptionRef.get()
subscription?.request(n.toLong())
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy