-
Notifications
You must be signed in to change notification settings - Fork 660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow supplying something other than suspending functions to core builders #4542
Comments
Example: Create an extension in jvmMain package com.apollographql.apollo3.network.ws
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.future.await
import java.util.concurrent.CompletableFuture
/**
* Configure the [WebSocketNetworkTransport] to reopen the websocket automatically when a network error
* happens
*
* @param reopenWhen a function taking the error and attempt index (starting from zero) as parameters and returning a CompletableFuture
* value of 'true' to reopen automatically or 'false' to forward the error to all listening [Flow].
* It returns a CompletableFuture, so it can be used to introduce delay before retry (e.g. backoff strategy).
*
*/
fun WebSocketNetworkTransport.Builder.reopenWhen(reopenWhen: ((Throwable, attempt: Long) -> CompletableFuture<Boolean>)?) = apply {
this.reopenWhen(reopenWhen?.let {
val adaptedLambda: suspend (Throwable, Long) -> Boolean = { throwable, attempt -> it.invoke(throwable, attempt).await() }
adaptedLambda
})
} |
@vincentjames501 Thanks for reporting this need. At Apollo we are trying to better understand the needs of Java developers for improving our Kotlin client's support of Java. If you have some time we have a survey we've been sharing to gathering some data on the use of Java in different companies, We'd love to gather you feedback for consideration. |
@jpvajda , thanks. I filled it out. Just to note, a lot of people would probably love to use this client from other JVM languages too (like Scala, Clojure, Groovy, etc). In our case we're trying to use it from Clojure. Just another data point I didn't see mentioned on the Survey. Cheers! |
Closing as the dedicated Java runtime addresses this with its callbacks based API. This can be tried out with the v4 betas. Feedback warmly welcome! |
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better. |
Use case
Our company is trying to use Apollo Kotlin for both our Java-based/Clojure-based applications.
Many functions like
reopenWhen
onWebSocketNetworkTransport
take Kotlin specific suspending functions which are near impossible to work with in vanilla Java and especially Clojure (and is pretty awkward to use w/o the Kotlin compiler).Describe the solution you'd like
It makes perfect sense for a Kotlin client to work this way (especially for implementing things like backoffs), though I'd love to see this library's public interfaces abstract some of the Kotlin specific features away to allow other JVM languages to adopt. For example, consider also accepting a function that returns a
java.util.concurrent.CompletableFuture
primitive and just translating to a suspension function under the hood https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-jdk8/kotlinx.coroutines.future/await.html as this is relatively easily usable in all other JVM-based languages w/o extra dependencies.If anyone has any example of how to translate something like:
to Java I'd love to see how it should be done!
The text was updated successfully, but these errors were encountered: