Skip to content
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

Closed
vincentjames501 opened this issue Nov 30, 2022 · 6 comments

Comments

@vincentjames501
Copy link

Use case

Our company is trying to use Apollo Kotlin for both our Java-based/Clojure-based applications.

Many functions like reopenWhen on WebSocketNetworkTransport 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:

.webSocketReopenWhen { e, attempt ->
          delay(2.0.pow(attempt.toDouble()).toLong())
          // retry after the delay
          true
        }

to Java I'd love to see how it should be done!

@vincentjames501 vincentjames501 changed the title Allow suppling something other than suspending functions to core builders Allow supplying something other than suspending functions to core builders Nov 30, 2022
@vincentjames501
Copy link
Author

I also realize things like #4283 and #3694 are underway but seems like this particular one could be tweaked until then?

@vincentjames501
Copy link
Author

Example:

Create an extension in jvmMain WebSocketNetworkTransportExtensions.kt and add the implementation("org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4") dependency to 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
  })
}

@jpvajda
Copy link
Contributor

jpvajda commented Dec 1, 2022

@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.

@vincentjames501
Copy link
Author

@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!

@BoD
Copy link
Contributor

BoD commented Jun 14, 2024

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!

@BoD BoD closed this as completed Jun 14, 2024
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants