GraphQL Summit is back for three days of insights, hands-on learning, and fun to celebrate the GraphQL community. Join us in San Diego Oct 3-5.
Docs
Try Apollo Studio

Persisted queries in Apollo Kotlin


Automatic persisted queries

Apollo Kotlin supports Automatic Persisted Queries (APQ). To take advantage of it, your GraphQL server also needs to support APQ (Apollo Server supports it out of the box).

Enable the feature when you initialize your ApolloClient instance, like so:

val apolloClient = ApolloClient.Builder()
.serverUrl("https://...")
.autoPersistedQueries()
.build()

You can optionally configure the HTTP methods to use. Using GET may take advantage of HTTP caching for instance when using a CDN:

val apolloClient = ApolloClient.Builder()
.serverUrl("https://...")
.autoPersistedQueries(
// For the initial hashed query that does not send the actual Graphql document
httpMethodForHashedQueries = HttpMethod.Get,
// For the follow-up query that sends the full document if the initial hashed query was not found
httpMethodForDocumentQueries = HttpMethod.Get
)
.build()

Note that mutations will always be sent as POST requests, regardless of these settings, as to avoid hitting caches.

Disable persisted queries for certain queries

You may want to disable persisted queries for certain queries, for instance to avoid any caching when the data is updated often. In order to do that, set enableAutoPersistedQueries to false on the ApolloCall:

apolloClient.query(myQuery).enableAutoPersistedQueries(false).toFlow()

operationOutput.json

If your backend uses custom persisted queries, Apollo Kotlin can generate an OperationOutput json from your .graphql queries. They will match what the client is sending exactly so you can persist them on your server.

apollo {
generateOperationOutput.set(true)
}

Custom ID for Persisted Queries

By default, Apollo uses Sha256 hashing algorithm to generate an ID for the query. To provide custom ID generation logic, use the option - operationIdGenerator which accepts an instance that implements the OperationIdGenerator interface (com.apollographql.apollo3.compiler.OperationIdGenerator) as the input. This option can be used to either specify a different hashing algorithm or to fetch the persisted query ID from a different place - e.g. a service or a CLI.

Example Md5 hash generator:

apollo {
operationIdGenerator.set(object: com.apollographql.apollo3.compiler.OperationIdGenerator {
override val version = "my-md5-version1"
override fun apply(operationDocument: String, operationName: String): String {
return operationDocument.md5()
}
})
}
import com.apollographql.apollo3.compiler.OperationIdGenerator
apollo {
operationIdGenerator = new OperationIdGenerator() {
String apply(String operationDocument, String operationName) {
return operationDocument.md5()
}
/**
* Use this version override to indicate an update to the implementation.
* This forces gradle to recompile models.
*/
String version = "my-md5-v1"
}
}

Versioning Id Generator

The result of the ID generator is cached. The cache is not updated when the implementation of the ID Generator changes. To indicate an update to the implementation of the ID Generator, change the version override as shown in the above example.

Edit on GitHub
Previous
HTTP cache
Next
Troubleshooting