apollo-kotlin icon indicating copy to clipboard operation
apollo-kotlin copied to clipboard

Initializing the Normalized Cache with `CacheKeyResolver` & Relay Style Pagination Constructs.

Open ArjanSM opened this issue 1 year ago • 1 comments

Question

Hi Team, 👋 In continuation to the issue#5666, I noticed that in the latest v4.0.0 Snapshot the normalized cache cannot be initialized using CacheKeyResolvers along with the MetadataGenerator, RecordMerger, FieldNameGeneratorand EmbeddedFieldsProvider. Are there any plans to introduce an API which can initialize the Normalized Cache using the CacheKeyResolvers and the Relay Style Pagination constructs?

Thanks

ArjanSM avatar Jun 13 '24 13:06 ArjanSM

Hi!

In the incubating version we've started transitioning to ApolloResolver to replace CacheResolver. It is the same thing but with a slightly different API that will help with evolving the library in a non-breaking way. The version of ApolloStore() that accepts the FieldNameGenerator etc. is expecting an ApolloResolver.

However there is no ApolloResolver version of CacheKeyResolver - we should definitely add it. In the meantime you can transform your existing resolver into an ApolloResolver with something like:

class ApolloCacheKeyAdapter(private val cacheKeyResolver: CacheKeyResolver) : ApolloResolver {
  override fun resolveField(context: ResolverContext): Any? {
    return cacheKeyResolver.resolveField(variables = context.variables, parent = context.parent, parentId = context.parentKey, field = context.field)
  }
}

and pass ApolloCacheKeyAdapter(yourExistingCacheKeyResolver) to ApolloStore().

BoD avatar Jun 13 '24 15:06 BoD

Done in https://github.com/apollographql/apollo-kotlin/pull/5970. Additional work on the incubating cache now happens in https://github.com/apollographql/apollo-kotlin-normalized-cache-incubating.

BoD avatar Oct 25 '24 10:10 BoD

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Kotlin usage and allow us to serve you better.

github-actions[bot] avatar Oct 25 '24 10:10 github-actions[bot]