Apollo-android: Unable to update OkHttpClient header from Apollo client instance

Created on 16 Jun 2017  路  6Comments  路  Source: apollographql/apollo-android

I am using OkHttpclient interceptors to add token header, but once expired there is no way to update the token from the apollo client instance?? Any suggestions

All 6 comments

Apollo client is completely agnostic to how you set-up and modify http request headers in OkHttpClient interceptors. So do the same as as you would do with lets say Retrofit library, or any other way. For example what can you do is inside the interceptor have some sort of ApiTokenProvider that will provide new token every time new request is being sent:

ApiTokenProvider apiTokenProvider = ....

....

.addInterceptor(chain -> {
        Request original = chain.request();
        Request.Builder builder = original.newBuilder().method(original.method(), original.body());
        builder.header("User-Agent", "Android Apollo Client");
        builder.header(API_TOKEN_HEADER, apiTokenProvider.provide());
        return chain.proceed(builder.build());
      })

@sav007, maybe you didn't understood the question very well. I am totally aware how to add headers using OkHttpClient, interceptors, the real deal is I wanted a single Apollo client throughout the app, I don't want to create a new instance of Apollo each time I do a call, but instead if Apollo client has a getter at least to modify the client would be convenient rather than creating different instance each time as that is a heavy operation or it's not a responsibility/task of each view to handle the backbone of the app. But thank you for your reply, it's useful.

I don't understand your question. A getter for what? What are you trying to modify? Could you kindly give more detail or a clear example of what you are trying to do.

Sorry for not being clear about this issue. The issue is as follows

Step 1 - Create an Apollo client instance within android application class

public class MyApplication extends Application {

    private ApolloClient apolloClient;

    @Override
    public void onCreate() {
        super.onCreate();
        apolloClient = ApolloClient.builder()
                .serverUrl("/")
                .okHttpClient(createOkHttpWithValidToken())
                .build();
    }

    private OkHttpClient createOkHttpWithValidToken() {
        return new OkHttpClient.Builder()
                .addNetworkInterceptor(new Interceptor() {
                    @Override
                    public Response intercept(Chain chain) throws IOException {
                        return chain.proceed(chain.request().newBuilder().header("header-token-key", "header-token-value").build());
                    }
                }).build();
    }

    public ApolloClient getApolloClient() {
        return apolloClient;
    }
}

Step 2: Get a reference of ApolloClient and make a query or mutation call
Example:

MyApplication myApplication = (MyApplication ) getApplication();
myApplication.getApolloClient().mutate("...do something

or

myApplication.getApolloClient().query("...do something

Now, at some point the token expires, and wanted to update the header from the myApplication.getApolloClient() instance.

1-Do I need to create a new instance of ApolloClient each time there is a new token?
2-Or can I use same ApolloClient instance and update OkHttpClient or replace the old one with new

There could be different kind of options to use, but didn't like the idea of creating a new instance

Thank you for your concerns again!

Make a subclass of interceptor. Give it a setter for the token value. Pass an instance of it to apollo. Whenever you have a new token, set it in the interceptor

Hi @digitalbuddha,

Is there any way to fetch response using GET in android apollo Client?

Was this page helpful?
0 / 5 - 0 ratings