https://kotlinlang.org logo
Title
v

vio

07/18/2021, 1:37 PM
hi all! I've been playing around with an apollo client to test a graphql api, wich I really like. I use a ktor server and
sessions
to allow authentication with cookies, but I'm not sure how to configure the apollo client to allow cookies. Any suggestions are helpful, thank you! 🙏🏻 My code looks like this:
val url = "....

                   val okHttp = OkHttpClient
                        .Builder()
                        .build()

                   val apolloClient = ApolloClient
                        .builder()
                        .serverUrl(url)
                        .okHttpClient(okHttp)
                        .build()
I found something using a
cookieJar
but can't seem to find the right libraries for kotlin something like this, (this code is not compiling):
val cookieManager = CookieManager()
 CookieHandler.setDefault(cookieManager)
 cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL)

val okHttp = OkHttpClient
                .Builder()
                .cookieJar(JavaNetCookieJar(cookieManager)) // this has syntax errors
                 .build()
m

mbonnin

07/18/2021, 2:26 PM
Are you on Android? Looks like https://github.com/franmontiel/PersistentCookieJar is a persistent implementation of a CookieJar for OkHttp on Android
Else you can implement your own, looks like it's 2 methods (from stackoverflow):
new CookieJar() {
        private final HashMap<HttpUrl, List<Cookie>> cookieStore = new HashMap<>();

        @Override
        public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
            cookieStore.put(url, cookies);
        }

        @Override
        public List<Cookie> loadForRequest(HttpUrl url) {
            List<Cookie> cookies = cookieStore.get(url);
            return cookies != null ? cookies : new ArrayList<Cookie>();
        }
    }
v

vio

07/18/2021, 2:42 PM
thank you, I'm not on android, still I gave this a try, the client gets initialised but still no cookies 🤔 not sure how the jar is suppose to work
val jar = object : CookieJar {
       private val cookieStore = HashMap<HttpUrl, List<Cookie>>()                 
       override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
            cookieStore[url] = cookies
       }

       override fun loadForRequest(url: HttpUrl): List<Cookie> {
            val cookies = cookieStore[url]
            return cookies ?: ArrayList()
       }
}

val okHttp = OkHttpClient
       .Builder()
       .cookieJar(jar)
       .build()

val apolloClient = ApolloClient
       .builder()
       .serverUrl(url)
       .okHttpClient(okHttp)
       .build()
m

mbonnin

07/18/2021, 4:55 PM
What you can do is add a logging interceptor to check what's happening.
🙏 1
Or hook a proxy like Charles
v

vio

07/20/2021, 6:54 AM
been playing around, and it seems that the jar remains always empty, not sure where to add to the jar and where to consume
m

mbonnin

07/20/2021, 12:10 PM
Maybe store per-domain instead of per-url ?
Or put a breakpoint (or println 😅 ) in
saveFromResponse
Sorry I'm not the most cookie proficient person. You can also try to isolate the issue in okhttp and ask in #squarelibraries
v

vio

07/28/2021, 4:14 PM
Hi @mbonnin, sorry for the late reply, I've came back to this code only today, but storing the cookies per-domain made it work 🙂 Thanks again for your help 🙏
👍 1
m

mbonnin

07/28/2021, 4:15 PM
Noice! Thanks for the update!