Skip to content

Commit

Permalink
Replace fetchThrows and refetch throws with filtering out exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
xmadera committed Sep 9, 2024
1 parent aeb4920 commit 09f6388
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,15 @@ internal interface ApiManager {
*
* @param query to be executed and watched.
* @param fetchPolicy fetch policy to apply to initial fetch.
* @param fetchThrows whether to emit [NetworkResult.Failure]s during the initial fetch.
* @param refetchThrows whether to emit [NetworkResult.Failure]s during a refetch.
* @param filterOutExceptions whether to filter out error responses (like cache misses) from the flow.
* @return Flow of [DATA] wrapped in [NetworkResult]
*/
fun <DATA : Query.Data> executeQueryWatcher(
query: Query<DATA>,
fetchPolicy: FetchPolicy,
fetchThrows: Boolean = true,
refetchThrows: Boolean = false,
filterOutExceptions: Boolean = false,
): Flow<NetworkResult<DATA>> = apiAdapter
.watchQueryWatcher(query, fetchPolicy, fetchThrows, refetchThrows)
.watchQueryWatcher(query, fetchPolicy, filterOutExceptions)
.map { result -> runWrapping { errorInterceptor { result.getOrThrow() } } }

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import com.apollographql.apollo.api.ApolloResponse
import com.apollographql.apollo.api.Mutation
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.Query
import com.apollographql.apollo.cache.normalized.cacheInfo
import com.apollographql.apollo.cache.normalized.fetchPolicy
import com.apollographql.apollo.cache.normalized.watch
import com.apollographql.apollo.exception.ApolloException
import com.apollographql.apollo.exception.ApolloHttpException
import com.apollographql.apollo.exception.ApolloNetworkException
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -41,23 +39,24 @@ internal class ApolloApiAdapter(
*
* @param query to be executed and watched.
* @param fetchPolicy fetch policy to apply to initial fetch.
* @param fetchThrows whether to throw if an [ApolloException] happens during the initial fetch.
* @param refetchThrows whether to throw if an [ApolloException] happens during a refetch.
* @param filterOutExceptions whether to filter out error responses (like cache misses) from the flow.
* @return Flow of [DATA] wrapped in Kotlin result
*/
fun <DATA : Query.Data> watchQueryWatcher(
query: Query<DATA>,
fetchPolicy: FetchPolicy,
fetchThrows: Boolean = true,
refetchThrows: Boolean = false,
filterOutExceptions: Boolean,
): Flow<Result<DATA>> =
apolloClient
.query(query)
.fetchPolicy(fetchPolicy.asApolloFetchPolicy())
.watch()
.filter {
// Filter out according to [fetchThrows] and [refetchThrows]
true
if (filterOutExceptions) {
it.exception == null
} else {
true
}
}
.map { apolloResponse -> runCatching { executeOperation { apolloResponse } } }

Expand Down

0 comments on commit 09f6388

Please sign in to comment.