# CacheKeyResolver

**URL:** <https://community.apollographql.com/t/cachekeyresolver/5870>\
**Category:** General\
**Tags:** client, kotlin\
**Created:** [March 3, 2023, 10:28pm UTC](https://community.apollographql.com/t/cachekeyresolver/5870 "2023-03-03T22:28:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArjanSM](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/arjansm/32/1689_2.png) [@ArjanSM](https://community.apollographql.com/u/ArjanSM)\
**Post date:** [March 3, 2023, 10:28pm UTC](https://community.apollographql.com/t/cachekeyresolver/5870/1 "2023-03-03T22:28:02Z")

</div>

Hi All, 👋

According to the documentation, “`CacheKeyResolvers` enables you to generate custom cache IDs from a field’s arguments”  
Based on my understanding, the `CacheKeyResolver` should be aware of the argument it needs to generate the custom `CacheKey`.  
i.e

```auto
val cacheKeyResolver = object: CacheKeyResolver() {
  override fun cacheKeyForField(field: CompiledField, variables: Executable.Variables): CacheKey? {
...
// resolveArgument returns the runtime value of the "id" argument
    // from either the variables or as a literal value
    val id = field.resolveArgument("id", variables)
...
    }
}

```

As shown above, this excerpt of the `CacheKeyResolver` logic from the documentation would work fairly well if all the graphql operations use `id` as the name of the argument representing an `id`. But what if the names of `id` field argument varies from `id` to `empid` to `employeeid`?  
In that case, `CacheKeyResolver` would have to keep track of every possible argument, in a set or map, that is used to represent an id and eventually use this set to determine if a field argument is an `id`. Note, the id in my schema is a `String!` which guarantees to generate a unique string for every object.

I also noticed that the `CompiledArgument` class, contains a field called `isKey`.  
Just curious, when would its value be set to `true` 🤔 ?

There are a few ways to get past this but they seem a little verbose to me.  
I just thought it would be good idea to check with the community.  
Eager to know everybody’s thoughts. 🙂

Thanks!

---

<div class="post-metadata">

**Author:** ![mbonnin](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/mbonnin/32/13_2.png) [@mbonnin](https://community.apollographql.com/u/mbonnin)\
**Post date:** [March 4, 2023, 12:07am UTC](https://community.apollographql.com/t/cachekeyresolver/5870/2 "2023-03-04T00:07:43Z")

</div>

Hi 👋

> But what if the names of `id` field argument varies from `id` to `empid` to `employeeid` ?

You can decide based on \_\_typename:

```kotlin
val cacheKeyGenerator = object : CacheKeyGenerator {
  override fun cacheKeyForObject(obj: Map<String, Any?>, context: CacheKeyGeneratorContext): CacheKey? {
    val typename = obj["__typename"] as String
    val id = when (typename) {
      "Employee" -> obj["employeeid"] as String
      // More cases here
      else -> obj["id"] as String
    }

    return CacheKey(typename, id)
  }
}

```

> I also noticed that the `CompiledArgument` class, contains a field called `isKey` .  
> Just curious, when would its value be set to `true` 🤔 ?

This is used if you use the [declarative cache](https://www.apollographql.com/docs/kotlin/caching/declarative-ids/) feature

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Justin\_Gonzalez](https://sea1.discourse-cdn.com/flex019/user_avatar/community.apollographql.com/justin_gonzalez/32/5359_2.png) [@Justin\_Gonzalez](https://community.apollographql.com/u/Justin_Gonzalez)\
**Post date:** [February 6, 2025, 8:49pm UTC](https://community.apollographql.com/t/cachekeyresolver/5870/3 "2025-02-06T20:49:18Z")

</div>


