Apollo-android: Add http header with request scoped state

Created on 17 Jan 2019  路  9Comments  路  Source: apollographql/apollo-android

Hi

I'm using apollo-android from a multithreaded jvm server application (not android). I need to add a http header to my graphql request that contains some state specific to the request (let's say something like a user id)

I can't just change the user id property of an OKHttp interceptor to add the header because another thread might get there first and use the wrong user id.

I tried to work around using a thread local variable but it doesn't work because the OK http request runs on a different thread to my original request thread.

Any suggestions on how I might do this?
Thanks

All 9 comments

Well it's quite unusual feature, just one thought can you use multiple OkHttpClients for different users, but share the same connection pool, dispatchers? In this case each OkHttpClient will have own interceptors but use the same resources.

Hi Ivan thanks for your suggestion I鈥檒l look into that.

I wouldn鈥檛 consider it unusual though. It鈥檚 just a spring webmvc server making GraphQL requests. When a user makes a request for particular webpage it鈥檚 typical there will be some request state to send to the GraphQL server: the auth token if nothing else.

A future solution would be to add a headers parameter when making the request using Apollo client?

Hi have little problem, i need get some items from Response Header, can you tell me how i can get it...
Our API gives response.body() with some data, and JWT are in headers, but i can't get it.
Any suggestions on how I might do this?
Thanks

Hi @sav007 馃憢 Airbnb actually uses per request headers frequently for logging purposes. I feel like need to clone OkHttp for every request that needs custom headers (a lot for us) is a bit clumsy. How do you feel about adding this as an option?

Hi Ben! Ivan is on vacation this week but I don't see any reason why it would be a problem to add as a feature. Would someone from your end be able to contribute it?

Yeah, happy to take a crack at it and dust of my apollo knowledge :)

Just an update -- we are using the cloned http workaround, which does work okay, so this is low-pri for me. Should get to it in the next few weeks, but if someone else would like to take a crack at it sooner, please do!

We've started implementing some GraphQL stuff here, and for various reasons, we have use cases which require per-request headers. I decided to take a stab at implementing this in the referenced PR. Take a look and let me know if you think this is a reasonable road to go down?

I think we can close this one.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pratthama-appdesk picture pratthama-appdesk  路  3Comments

john-lanticse picture john-lanticse  路  3Comments

mmahesh2019 picture mmahesh2019  路  4Comments

TayfunCesur picture TayfunCesur  路  4Comments

jamesweb1 picture jamesweb1  路  3Comments