Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[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

Comments

@omtians9425
Copy link

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

ApolloClient.builder()
        .enableAutoPersistedQueries(true)
        .useHttpGetMethodForPersistedQueries(true)
        ...

But now in 3.x, mutation was executed with GET, and also query was executed with GET with below code

ApolloClient.Builder()
        .autoPersistedQueries(httpMethodForHashedQueries = HttpMethod.Get)
        ...

Version
3.1.0

Description
Below code is okay (though both query and mutation are executed with POST)

ApolloClient.Builder()
        .autoPersistedQueries(httpMethodForHashedQueries = HttpMethod.Post)
        ...

But we want to use with GET method for using HTTP cache

@omtians9425 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
@BoD
Copy link
Contributor

BoD commented Apr 8, 2022

Hi! 👋 Thanks for reporting this!

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 {
        override fun <D : Operation.Data> intercept(
            request: ApolloRequest<D>,
            chain: ApolloInterceptorChain,
        ): Flow<ApolloResponse<D>> {
            val newRequest = if (request.operation is Mutation) {
                // For mutations, we always want a POST to avoid it being cached
                request.newBuilder().httpMethod(HttpMethod.Post).build()
            } else {
                request
            }
            return chain.proceed(newRequest)
        }
    })

@omtians9425
Copy link
Author

Thank you for a solution and quick fix 🎉

@martinbonnin
Copy link
Contributor

This is now in 3.2.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants