2

I'm trying to use apollo-android library to communicate with graphql server. The problem is that backend uses headers to authenticate requests and I found no ways to add them. Also in auth request the token is sent in headers and I found no ways to read it from the response.

People advise to set auth headers via interceptors in OkHttpClient but this approach is not applicable in my situation because client have to send different sets of headers in different requests.

So, is there any workaround in this situation? Should I use simple rest client like Retrofit or maybe create new ApolloClient and OkHttpClient instances on each new request with desired set of headers? Or maybe there is another workarounds?

Vlad Kudoyar
  • 371
  • 3
  • 8

1 Answers1

0

People advise to set auth headers via interceptors in OkHttpClient but this approach is not applicable in my situation because client have to send different sets of headers in different requests.

Have setter methods and fields on your interceptors that accept the varying headers. Call those setter methods prior to making an ApolloClient request that needs the headers.

Or, teach the interceptor how to apply different headers for different requests based on the request characteristics visible to the interceptor (e.g., URL).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Is this use case possible? I make request1(r1) request2(r2) request3(r3) one after another and set header1(h1) header2(h2) header3(h3) respectively. While r1 is executed with h1, r2 with h2 and r3 with h3 are enqueued. When r2 is executed the interceptor will have h3 headers so r2 can be executed with h3. The question is when the interceptor will be called, at execution or in enqueue time? – Vlad Kudoyar Apr 15 '18 at 20:36
  • @VladKudoyar: I do not know the precise timing of interceptors, though my guess would be that they are employed at execution time. You would need to test that theory, though. – CommonsWare Apr 15 '18 at 20:49