Skip to content

Commit

Permalink
Catch ApolloException in toState and watchAsState (#5116)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD authored Jul 24, 2023
1 parent aa154f6 commit 0a5d12d
Showing 1 changed file with 4 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ 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 com.benasher44.uuid.uuid4
import kotlinx.coroutines.flow.catch
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

Expand All @@ -35,6 +37,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.Builder(operation, uuid4(), it as? ApolloException ?: throw it).build()) }
}
return responseFlow.collectAsState(initial = null, context = context)
}
Expand All @@ -59,6 +62,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.Builder(operation, uuid4(), it as? ApolloException ?: throw it).build()) }
}
return responseFlow.collectAsState(initial = null, context = context)
}

0 comments on commit 0a5d12d

Please sign in to comment.