All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.javalin.plugin.graphql.SubscriberGraphQL.kt Maven / Gradle / Ivy

There is a newer version: 4.6.8
Show newest version
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