From 72b1766a2687115e6a2b65c4603c57f300e916b2 Mon Sep 17 00:00:00 2001 From: Rohan Dhruva Date: Thu, 29 Apr 2021 02:13:12 -0700 Subject: [PATCH] Make variables available to the CacheKeyResolver in fromFieldRecordSet (#3074) Co-authored-by: Rohan Dhruva --- .../apollo3/cache/normalized/CacheKeyResolver.kt | 3 ++- .../cache/normalized/internal/OperationCacheExtensions.kt | 3 +-- .../kotlin/com/apollographql/apollo3/integration/Utils.kt | 4 ++-- .../com/apollographql/apollo3/IdFieldCacheKeyResolver.kt | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/apollo-normalized-cache-api/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/CacheKeyResolver.kt b/apollo-normalized-cache-api/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/CacheKeyResolver.kt index ef9a7956e32..3571c8c18ef 100644 --- a/apollo-normalized-cache-api/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/CacheKeyResolver.kt +++ b/apollo-normalized-cache-api/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/CacheKeyResolver.kt @@ -12,6 +12,7 @@ import kotlin.jvm.JvmSuppressWildcards abstract class CacheKeyResolver { abstract fun fromFieldRecordSet( field: ResponseField, + variables: Operation.Variables, recordSet: Map ): CacheKey @@ -25,7 +26,7 @@ abstract class CacheKeyResolver { @JvmField val DEFAULT: CacheKeyResolver = object : CacheKeyResolver() { - override fun fromFieldRecordSet(field: ResponseField, recordSet: Map) = CacheKey.NO_KEY + override fun fromFieldRecordSet(field: ResponseField, variables: Operation.Variables, recordSet: Map) = CacheKey.NO_KEY override fun fromFieldArguments(field: ResponseField, variables: Operation.Variables) = CacheKey.NO_KEY } diff --git a/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/OperationCacheExtensions.kt b/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/OperationCacheExtensions.kt index 9130b3eda7a..ade52fcda12 100644 --- a/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/OperationCacheExtensions.kt +++ b/apollo-normalized-cache/src/commonMain/kotlin/com/apollographql/apollo3/cache/normalized/internal/OperationCacheExtensions.kt @@ -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 Operation.normalize( @@ -54,7 +53,7 @@ private fun 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, null, rootKey, fieldSets) } diff --git a/composite/integration-tests-kotlin/src/commonTest/kotlin/com/apollographql/apollo3/integration/Utils.kt b/composite/integration-tests-kotlin/src/commonTest/kotlin/com/apollographql/apollo3/integration/Utils.kt index edb4678b5be..53bb4be77ba 100644 --- a/composite/integration-tests-kotlin/src/commonTest/kotlin/com/apollographql/apollo3/integration/Utils.kt +++ b/composite/integration-tests-kotlin/src/commonTest/kotlin/com/apollographql/apollo3/integration/Utils.kt @@ -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): CacheKey { + override fun fromFieldRecordSet(field: ResponseField, variables: Operation.Variables, recordSet: Map): CacheKey { val id = recordSet["id"] return if (id != null) { formatCacheKey(id.toString()) @@ -70,4 +70,4 @@ suspend fun Channel.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) \ No newline at end of file +fun assertEquals2(actual: Any?, expected: Any?) = assertEquals(expected, actual) diff --git a/composite/integration-tests/src/testShared/kotlin/com/apollographql/apollo3/IdFieldCacheKeyResolver.kt b/composite/integration-tests/src/testShared/kotlin/com/apollographql/apollo3/IdFieldCacheKeyResolver.kt index 9d6bd4a7b60..ad971f3ed96 100644 --- a/composite/integration-tests/src/testShared/kotlin/com/apollographql/apollo3/IdFieldCacheKeyResolver.kt +++ b/composite/integration-tests/src/testShared/kotlin/com/apollographql/apollo3/IdFieldCacheKeyResolver.kt @@ -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): CacheKey { + override fun fromFieldRecordSet(field: ResponseField, variables: Operation.Variables, recordSet: Map): CacheKey { val id = recordSet["id"] return if (id != null) { formatCacheKey(id.toString()) @@ -31,4 +31,4 @@ class IdFieldCacheKeyResolver : CacheKeyResolver() { CacheKey(id) } } -} \ No newline at end of file +}