You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[3.x] Mutation is executed with GET (HTTP Method) when using ApolloClient.Builder.autoPersistedQueries(httpMethodForHashedQueries = HttpMethod.Get)
#4006
Closed
omtians9425 opened this issue
Apr 8, 2022
· 3 comments
Summary
When we used the previous version of Apollo (2.x), this problem didn't occur.
In 2.x, mutation was executed with POST, and query was executed with GET with below code
But we want to use with GET method for using HTTP cache
The text was updated successfully, but these errors were encountered:
omtians9425
changed the title
[3.x] Mutation is executed with GET (HTTP Method) when using ApolloClient.autoPersistedQueries(httpMethodForHashedQueries = HttpMethod.Get)
[3.x] Mutation is executed with GET (HTTP Method) when using ApolloClient.Builder.autoPersistedQueries(httpMethodForHashedQueries = HttpMethod.Get)
Apr 8, 2022
It looks like the Auto Persisted Queries interceptor in v3 doesn't distinguish queries vs mutations and will always use the method you configure, although it is not a good idea to use GET for mutations, as they could use the cache which is never wanted. In v2 the distinction was correctly made.
We'll make a fix for this!
In the meantime, you can manually override the method to always be POST for mutations, with an interceptor, like this:
ApolloClient.builder()
.autoPersistedQueries(httpMethodForHashedQueries =HttpMethod.Get, httpMethodForDocumentQueries =HttpMethod.Get)
.addInterceptor(object:ApolloInterceptor {
overridefun <D:Operation.Data> intercept(
request:ApolloRequest<D>,
chain:ApolloInterceptorChain,
): Flow<ApolloResponse<D>> {
val newRequest =if (request.operation isMutation) {
// For mutations, we always want a POST to avoid it being cached
request.newBuilder().httpMethod(HttpMethod.Post).build()
} else {
request
}
return chain.proceed(newRequest)
}
})
Summary
When we used the previous version of Apollo (2.x), this problem didn't occur.
In 2.x, mutation was executed with POST, and query was executed with GET with below code
But now in 3.x, mutation was executed with GET, and also query was executed with GET with below code
Version
3.1.0
Description
Below code is okay (though both query and mutation are executed with POST)
But we want to use with GET method for using HTTP cache
The text was updated successfully, but these errors were encountered: