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

3.x compose support: Catch exceptions and expose them in .exception #5018

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.apollographql.apollo3.api.Operation
import com.apollographql.apollo3.api.Query
import com.apollographql.apollo3.cache.normalized.watch
import com.apollographql.apollo3.exception.ApolloException
import kotlinx.coroutines.flow.catch
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

Expand All @@ -35,6 +36,7 @@ import kotlin.coroutines.EmptyCoroutineContext
fun <D : Operation.Data> ApolloCall<D>.toState(context: CoroutineContext = EmptyCoroutineContext): State<ApolloResponse<D>?> {
val responseFlow = remember {
toFlow()
.catch { emit(ApolloResponse(this@toState, it as? ApolloException ?: throw it)) }
}
return responseFlow.collectAsState(initial = null, context = context)
}
Expand All @@ -59,6 +61,7 @@ fun <D : Operation.Data> ApolloCall<D>.toState(context: CoroutineContext = Empty
fun <D : Query.Data> ApolloCall<D>.watchAsState(context: CoroutineContext = EmptyCoroutineContext): State<ApolloResponse<D>?> {
val responseFlow = remember {
watch()
.catch { emit(ApolloResponse(this@watchAsState, it as? ApolloException ?: throw it)) }
}
return responseFlow.collectAsState(initial = null, context = context)
}