hi all! I've been playing around with an apollo cl...
# apollo-kotlin
v
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:
Copy code
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):
Copy code
val cookieManager = CookieManager()
 CookieHandler.setDefault(cookieManager)
 cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL)

val okHttp = OkHttpClient
                .Builder()
                .cookieJar(JavaNetCookieJar(cookieManager)) // this has syntax errors
                 .build()
m
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):
Copy code
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
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
Copy code
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
What you can do is add a logging interceptor to check what's happening.
๐Ÿ™ 1
Or hook a proxy like Charles
v
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
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
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
Noice! Thanks for the update!