Last active
August 8, 2020 15:44
-
-
Save CompileConnected/1701801ac8f14265c47c2cffac192d5d to your computer and use it in GitHub Desktop.
GraphQLExtensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.apollographql.apollo.api.Response | |
| import com.apollographql.apollo.GraphQLCall | |
| import com.apollographql.apollo.exception.ApolloException | |
| import kotlinx.coroutines.CompletableDeferred | |
| suspend fun <T> GraphQLCall<T>.toDeferred(): Response<T> { | |
| val deferred = CompletableDeferred<Response<T>>() | |
| deferred.invokeOnCompletion { | |
| if (deferred.isCancelled) { | |
| cancel() | |
| } | |
| } | |
| enqueue(object : GraphQLCall.Callback<T>() { | |
| override fun onResponse(response: Response<T>) { | |
| if (deferred.isActive) { | |
| deferred.complete(response) | |
| } | |
| } | |
| override fun onFailure(e: ApolloException) { | |
| if (deferred.isActive) { | |
| deferred.completeExceptionally(e) | |
| } | |
| } | |
| }) | |
| return deferred.await() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment