Skip to content

Commit

Permalink
Make variables available to the CacheKeyResolver in fromFieldRecordSet (
Browse files Browse the repository at this point in the history
apollographql#3074)

Co-authored-by: Rohan Dhruva <[email protected]>
  • Loading branch information
rohandhruva and rohandhruva authored Apr 29, 2021
1 parent 005dcd5 commit 72b1766
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlin.jvm.JvmSuppressWildcards
abstract class CacheKeyResolver {
abstract fun fromFieldRecordSet(
field: ResponseField,
variables: Operation.Variables,
recordSet: Map<String, @JvmSuppressWildcards Any?>
): CacheKey

Expand All @@ -25,7 +26,7 @@ abstract class CacheKeyResolver {

@JvmField
val DEFAULT: CacheKeyResolver = object : CacheKeyResolver() {
override fun fromFieldRecordSet(field: ResponseField, recordSet: Map<String, Any?>) = CacheKey.NO_KEY
override fun fromFieldRecordSet(field: ResponseField, variables: Operation.Variables, recordSet: Map<String, Any?>) = CacheKey.NO_KEY

override fun fromFieldArguments(field: ResponseField, variables: Operation.Variables) = CacheKey.NO_KEY
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.apollographql.apollo3.api.variables
import com.apollographql.apollo3.cache.CacheHeaders
import com.apollographql.apollo3.cache.normalized.CacheKey
import com.apollographql.apollo3.cache.normalized.CacheKeyResolver
import com.apollographql.apollo3.cache.normalized.NormalizedCache
import com.apollographql.apollo3.cache.normalized.ReadOnlyNormalizedCache

fun <D : Operation.Data> Operation<D>.normalize(
Expand Down Expand Up @@ -54,7 +53,7 @@ private fun <D> normalizeInternal(
val writer = MapJsonWriter()
adapter.toResponse(writer, responseAdapterCache, data)
return Normalizer(variables) { responseField, fields ->
cacheKeyResolver.fromFieldRecordSet(responseField, fields).let { if (it == CacheKey.NO_KEY) null else it.key }
cacheKeyResolver.fromFieldRecordSet(responseField, variables, fields).let { if (it == CacheKey.NO_KEY) null else it.key }
}.normalize(writer.root() as Map<String, Any?>, null, rootKey, fieldSets)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fun readTestFixture(name: String) = readFile("../integration-tests/testFixtures/
fun readResource(name: String) = readFile("../integration-tests/testFixtures/resources/$name")

object IdFieldCacheKeyResolver : CacheKeyResolver() {
override fun fromFieldRecordSet(field: ResponseField, recordSet: Map<String, Any?>): CacheKey {
override fun fromFieldRecordSet(field: ResponseField, variables: Operation.Variables, recordSet: Map<String, Any?>): CacheKey {
val id = recordSet["id"]
return if (id != null) {
formatCacheKey(id.toString())
Expand Down Expand Up @@ -70,4 +70,4 @@ suspend fun <T> Channel<T>.receiveOrTimeout(timeoutMillis: Long = 500) = withTim
/**
* A helper function to reverse the order of the argument so that we can easily column edit the tests
*/
fun assertEquals2(actual: Any?, expected: Any?) = assertEquals(expected, actual)
fun assertEquals2(actual: Any?, expected: Any?) = assertEquals(expected, actual)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.apollographql.apollo3.cache.normalized.CacheKey
import com.apollographql.apollo3.cache.normalized.CacheKeyResolver

class IdFieldCacheKeyResolver : CacheKeyResolver() {
override fun fromFieldRecordSet(field: ResponseField, recordSet: Map<String, Any?>): CacheKey {
override fun fromFieldRecordSet(field: ResponseField, variables: Operation.Variables, recordSet: Map<String, Any?>): CacheKey {
val id = recordSet["id"]
return if (id != null) {
formatCacheKey(id.toString())
Expand All @@ -31,4 +31,4 @@ class IdFieldCacheKeyResolver : CacheKeyResolver() {
CacheKey(id)
}
}
}
}

0 comments on commit 72b1766

Please sign in to comment.