https://kotlinlang.org logo
Title
m

Milan Vadhel

10/22/2021, 11:35 AM
Hello Everyone Is there any way to handle exception handling by only one class in GraphQL like an interceptor in REST Api?
j

Jeremie

10/22/2021, 3:09 PM
does this help? (it might now but im throwing it out there, looks like it might be what you want) https://github.com/erdo/apollo3-android-sample/tree/master/app/src/main/java/foo/bar/foreapollo3/api
b

bod

10/22/2021, 3:13 PM
Hi 👋 Apollo Android does have interceptors as well, but can you tell us what you had in mind as to how to handle errors?
m

Milan Vadhel

10/25/2021, 5:32 AM
@Jeremie Thanks for reply, Let me check and try it.
@bod I want to implement one interceptor like in REST API we are implementing it with OkHttp.
b

bod

10/25/2021, 6:59 AM
Ok, well in fact you can even pass OkHttp interceptors directly to Apollo,
m

Milan Vadhel

10/25/2021, 7:02 AM
@Singleton
@Provides
fun provideApolloClient(okHttpClient: OkHttpClient): ApolloClient {
    return ApolloClient
        .builder()
        .addApplicationInterceptor()
        .defaultHttpCachePolicy(HttpCachePolicy.NETWORK_FIRST)
        .defaultResponseFetcher(ApolloResponseFetchers.NETWORK_FIRST)
        .serverUrl(URLConstant.GRAPH_QL_BASE_URL)
        .okHttpClient(okHttpClient)
        .build()
}
addApplicationInterceptor() is only accepts ApolloInterceptor.
b

bod

10/25/2021, 7:12 AM
you can set your interceptor on the
okHttpClient
🙂 See an example here: https://www.apollographql.com/docs/android/tutorial/10-authenticate-your-queries/
m

Milan Vadhel

10/25/2021, 7:13 AM
Ohh Yehh, Thanks @bod
👍 1