Hi! I’m looking into using compression for the res...
# apollo-kotlin
e
Hi! I’m looking into using compression for the response and I can’t get it to work. I’ve tried setting the
Accept-Encoding: gzip
header, first on the
OkHttpClient
, then on the
ApolloClient
, but neither solution worked, as requests fail with
JsonEncodingException
because of “malformed JSON”. Tried the same with the 2 sample apps mentioned in the docs. MortyComposeKMM has the same issue, while the apollo-kotlin-tutorial app works no matter if I set the header on the Http client or the Apollo client.
My setup is a bit weird, in that I have the Apollo client in a multiplatform module, but it shares the
OkHttpClient
configured in the
app
module (because of a migration in progress and a mix of REST and graphQL backends). I should mention that our iOS app (which at the moment is completely separate) works with
gzip
without issues.
And as an example, this is what I changed in the MortyKMM app: https://github.com/joreilly/MortyComposeKMM/blob/4b6cccbf901c5ddf3683995b8a9abcae2[…]otlin/dev/johnoreilly/mortycomposekmm/shared/MortyRepository.kt
Copy code
private val apolloClient = ApolloClient.Builder()
        .serverUrl("<https://rickandmortyapi.com/graphql>")
        .addHttpInterceptor(
            HeadersInterceptor(listOf(HttpHeader("Accept-Encoding", "gzip")))
        )
        .addHttpInterceptor(LoggingInterceptor())
        .build()
m
What does the LogginInterceptor say? Can you dump the HTTP request/response?
e
Tried both there as well, the one on the OkHttpClient (
HttpLoggingInterceptor
) will print the unzipped JSON, while the Apollo
LoggingInterceptor
will print the binary content
I’ll post a real example in a bit
Copy code
Request headers:

Content-Type: application/json
Content-Length: 164
X-APOLLO-OPERATION-ID: ...
X-APOLLO-OPERATION-NAME: PublishedProductCollections
Accept: multipart/mixed; deferSpec=20220824, application/json
Accept-Encoding: gzip
User-Agent: ...
Market: se
Authorization: Bearer ...

Request body: 
{"operationName":"PublishedProductCollections","variables":{},"query":"query PublishedProductCollections { ... }"}

Response headers:

content-type: application/json; charset=utf-8
cf-ray: ...
cf-cache-status: HIT
age: 291
cache-control: public, max-age=180, s-maxage=180
content-encoding: gzip
vary: Origin, Accept-Encoding
x-envoy-upstream-service-time: 75
x-graphql-cache: hit
set-cookie: ...
server: cloudflare

Response body:

(OkHttp) HttpLoggingInterceptor: 
{"data":{"publishedProductCollections":[...]}}

(Apollo) LoggingInterceptor:
�????????????|WKn��...
This is actually from my own app
y
It's automatic and defaulted
👍 1
OkHttp adds the accept header for you
If you set it yourself then you become responsible for decompressing also
How certain are you that it wasn't already working transparently before you changed anything?
e
Oh, I didn’t know that, thank you! I guess then I don’t need to do anything at all.
y
I think you'll see differences in encoding between a application and network interceptor if you want to confirm
e
I will do that, and also double-check with my colleague who handles the backend. Thank you both for your prompt help!
👍 1
137 Views