Pablichjenkov
10/16/2024, 2:53 PMmbonnin
10/16/2024, 3:37 PMApolloInterceptor
?mbonnin
10/16/2024, 3:38 PMresponse.errors
(assuming this is where the server signals the expired token) and retry the downstream chainmbonnin
10/16/2024, 3:40 PMsince new devs may forget to add the error in the GQL query.Wait, the error is actually part of your schema looks like?
mbonnin
10/16/2024, 3:41 PMmbonnin
10/16/2024, 3:42 PMresponse.errors
entry for missing datambonnin
10/16/2024, 3:42 PMPablichjenkov
10/16/2024, 4:03 PMmbonnin
10/16/2024, 4:05 PMApolloInterceptor
is what you want. It's like OkHttp Interceptor
but works at the GraphQL layer so you'll have access to response.errors
Pablichjenkov
10/16/2024, 4:06 PMmbonnin
10/16/2024, 4:06 PM.addInterceptor()
after .normalizedCache()
to only run your interceptor if network is requireddorche
10/16/2024, 4:06 PMclass ReauthApolloInterceptor : ApolloInterceptor {
override fun <D : Operation.Data> intercept(
request: ApolloRequest<D>,
chain: ApolloInterceptorChain
): Flow<ApolloResponse<D>> = chain.proceed(request).map { response ->
if (!request.operation.requiresAuth()) {
return@map response
}
val authorised = response.errors.doesNotContainUnAuthorised()
if (authorised) {
return@map response
}
val newToken = getNewToken()
chain.proceed(request.newBuilder().addNewToken(newToken).build())
}
}
Some half-working code to help you (sorry can't quite share exact code because it's private but this is the general idea)Pablichjenkov
10/16/2024, 4:14 PM