maxgdn
02/12/2022, 7:41 PMval apolloOkHttpBuilder = OkHttpClient.Builder()
.connectTimeout(connectTimeout,TimeUnit.SECONDS)
.writeTimeout(writeTimeout,TimeUnit.SECONDS)
.readTimeout(readTimeout,TimeUnit.SECONDS)
.addInterceptor(ErrorInterceptor(context))
.addInterceptor(AuthorizationInterceptor())
.addInterceptor(MetadataInterceptor())
…
val apolloClient = ApolloClient.Builder()
.serverUrl(url)
.normalizedCache(
cacheFactory,
cacheKeyGenerator = cacheKeyGenerator,
cacheResolver = cacheKeyResolver,
)
.storePartialResponses(true)
.fetchPolicy(FetchPolicy.CacheFirst)
.okHttpClient(
apolloOkHttpBuilder.build()
)
.interceptors(listOf(ApolloErrorInterceptor()))
.build()
ErrorInterceptor is based off of https://stackoverflow.com/a/65469952/13215280
If an error exists, it returns an http status code 999 and adds a header with the content of it. It retrieves the msg in the ApolloErrorInterceptor
class ApolloErrorInterceptor: ApolloInterceptor {
override fun <D : Operation.Data> intercept(
request: ApolloRequest<D>,
chain: ApolloInterceptorChain
): Flow<ApolloResponse<D>> {
val response = chain.proceed(request)
return response.catch { e ->
when(e) {
is ApolloHttpException -> {
val header = e.headers.find { it.name == EXCEPTION_HEADER_NAME }
header?.let { throw ApolloNetworkException(it.value) }
}
}
}
}
}
This currently allows us to get the error but for some reason the cache is now breaking and optimisticUpdates are failing to be applied.
Specifically java.lang.IllegalStateException: no cache configured
I found a similar thread but I don’t understand it completely. https://github.com/apollographql/apollo-kotlin/issues/2968
Is it possible to accomplish this? Any resources/docs/links?
Many Thanks!apollo_version = '3.0.0'
which is awesome! I was able to perform a refactor and remove a ton of code.mbonnin
02/12/2022, 9:44 PM.interceptors(listOf(ApolloErrorInterceptor()))
Looks like this is the issue. This is going to reset the ApolloCacheInterceptor that is internally set by normalizedCache
interceptors()
actually. This is a dangerous API 🤔addInterceptor()
instead of interceptors()
and you should be goodmaxgdn
02/12/2022, 9:53 PMinterceptors()
to addInterceptor()
has mended the java.lang.IllegalStateException: no cache configured
from throwing.