Programmatic cache IDs in Apollo Kotlin
In situations where declarative cache IDs don't fit your use case, you can programmatically generate cache IDs for object types in your normalized cache.
You can generate a given object type's cache ID from one of two sources:
Source | Class | Description |
---|---|---|
From a response object's fields (e.g., Book.id ) | CacheKeyGenerator | This happens after a network request and is essential to merging a query result with existing cached data. This is the most common case. |
From a GraphQL operation's arguments (e.g., author(id: "au456") ) | CacheKeyResolver | This happens before a network request and enables you to avoid a network round trip if all requested data is in the cache already. This is an optional optimization that can avoid some cache misses. |
Apollo Kotlin provides a class for generating cache IDs from each of these sources.
CacheKeyGenerator
The CacheKeyGenerator
class enables you to generate custom cache IDs from an object's field values. This basic example generates every object type's cache ID from its id
field:
val cacheKeyGenerator = object : CacheKeyGenerator {override fun cacheKeyForObject(obj: Map<String, Any?>, context: CacheKeyGeneratorContext): CacheKey? {// Generate the cache ID based on the object's id fieldreturn CacheKey(obj["id"] as String)}}
To use your custom CacheKeyGenerator
, include it in your cache initialization code like so:
val apolloClient = ApolloClient.Builder().serverUrl("https://example.com/graphql").normalizedCache(normalizedCacheFactory = cacheFactory,cacheKeyGenerator = cacheKeyGenerator,).build()
You can get the current object's typename from the context
object and include it in the generated ID, like so:
val cacheKeyGenerator = object : CacheKeyGenerator {override fun cacheKeyForObject(obj: Map<String, Any?>, context: CacheKeyGeneratorContext): CacheKey? {val typename = context.field.type.rawType().nameval id = obj["id"] as Stringreturn CacheKey(typename, id)}}
You can also use the current object's typename to use different cache ID generation logic for different object types.
Note that for cache ID generation to work, your GraphQL operations must return whatever fields your custom code relies on (such as id
above). If a query does not return a required field, the cache ID will be inconsistent, resulting in data duplication.
Also, for interfaces and unions, context.field.type.rawType().name
yields the typename as it is declared in the schema, as opposed to the runtime value of the type received in the response. Instead querying for the __typename
is safer.
To make sure __typename
is included in all operations set the addTypename gradle config:
apollo {service("service") {addTypename.set("always")}}
CacheKeyResolver
The CacheKeyResolver
class enables you to generate custom cache IDs from a field's arguments. This basic example generates every object type's cache ID from the id
argument if it's present:
val cacheKeyResolver = object: CacheKeyResolver() {override fun cacheKeyForField(field: CompiledField, variables: Executable.Variables): CacheKey? {// [field] contains compile-time information about what type of object is being resolved.// Even though we call rawType() here, we're guaranteed that the type is a composite type and not a listval typename = field.type.rawType().name// argumentValue returns the runtime value of the "id" argument// from either the variables or as a literal valueval id = field.argumentValue("id", variables).getOrNull()if (id is String) {// This field has an id argument, so we can use it to compute a cache IDreturn CacheKey(typename, id)}// Return null to use the default handlingreturn null}}
To use your custom CacheKeyResolver
, include it in your cache initialization code like so:
val apolloClient = ApolloClient.Builder().serverUrl("https://example.com/graphql").normalizedCache(normalizedCacheFactory = cacheFactory,cacheKeyGenerator = cacheKeyGenerator,cacheResolver = cacheKeyResolver).build()
Note the following about using a custom CacheKeyResolver
:
- The
cacheKeyForField
function is called for every field in your operation that returns a composite type, so it's important to returnnull
if you don't want to handle a particular field. - The function is not called for fields that return a list of composite types. See below.
Handling lists
Let's say we have this query:
query GetBooks($ids: [String!]!) {books(ids: $ids) {idtitle}}
To have the cache look up all books in the ids
list, we need to override listOfCacheKeysForField
in CacheKeyResolver
:
override fun listOfCacheKeysForField(field: CompiledField, variables: Executable.Variables): List<CacheKey?>? {// Note that the field *can* be a list type hereval typename = field.type.rawType().name// argumentValue returns the runtime value of the "id" argument// from either the variables or as a literal valueval ids = field.argumentValue("ids", variables).getOrNull()if (ids is List<*>) {// This field has an id argument, so we can use it to compute a cache IDreturn ids.map { CacheKey(typename, it as String) }}// Return null to use the default handlingreturn null}
For the sake of simplicity, only one level of list is supported. To support more nested lists, you can implement CacheResolver
. CacheResolver
is a generalization of CacheKeyResolver
that can return any value from the cache, even scalar values:
val cacheResolver = object: CacheResolver {override fun resolveField(field: CompiledField,variables: Executable.Variables,parent: Map<String, @JvmSuppressWildcards Any?>,parentId: String,): Any? {var type = field.typevar listDepth = 0while (true) {when (type) {is CompiledNotNullType -> type = type.ofTypeis CompiledListType -> {listDepth++type = type.ofType}else -> break}}// Now type points to the leaf type and lestDepth is the nesting of lists required// Return a kotlin value for this field// No type checking is done here so it must match the expected GraphQL typeif (listDepth == 2) {return listOf(listOf("0", "1"))}// CacheResolver must always call DefaultCacheResolver last or all fields will be null elsereturn DefaultCacheResolver.resolveField(field, variables, parent, parentId)}}