Persisted queries
Automatic persisted queries
Apollo Android supports Automatic Persisted Queries. Your server needs to supports it (apollo-server supports it out of the box). Enable the feature in apollo-android like so:
1ApolloClient.builder()
2 /* ... */
3 .enableAutoPersistedQueries(true)
4 /* ... */
5 .build()
You can optionally configure apollo-android to send GET HTTP verbs for queries, to benefit from caching if your server uses a CDN:
1ApolloClient.builder()
2 /* ... */
3 .enableAutoPersistedQueries(true)
4 .useHttpGetMethodForQueries(true)
5 /* ... */
6 .build()
operationOutput.json
If your backend uses custom persisted queries, Apollo Android 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.
1apollo {
2 generateOperationOutput = true
3}
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.apollo.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:
1apollo {
2 operationIdGenerator.set(object: com.apollographql.apollo.compiler.OperationIdGenerator {
3 override val version = "my-md5-version1"
4
5 override fun apply(operationDocument: String, operationFilepath: String): String {
6 return operationDocument.md5()
7 }
8 })
9}
1import com.apollographql.apollo.compiler.OperationIdGenerator
2
3apollo {
4 operationIdGenerator = new OperationIdGenerator() {
5 String apply(String operationDocument, String operationFilepath) {
6 return operationDocument.md5()
7 }
8
9 /**
10 * Use this version override to indicate an update to the implementation.
11 * This forces gradle to recompile models.
12 */
13 String version = "my-md5-v1"
14 }
15}
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.