From 2fa5bac6d847c85d187930b2e49165e8cbe76fd3 Mon Sep 17 00:00:00 2001 From: BoD Date: Fri, 6 Dec 2024 09:48:38 +0100 Subject: [PATCH] Account for cycles in getReachableCacheKeys --- .../cache/normalized/GarbageCollection.kt | 2 +- .../src/commonMain/graphql/operations.graphql | 3 ++ .../kotlin/ReachableCacheKeysTest.kt | 34 +++++++++++++------ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/GarbageCollection.kt b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/GarbageCollection.kt index 11aa8708..354b9ebf 100644 --- a/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/GarbageCollection.kt +++ b/normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/GarbageCollection.kt @@ -21,7 +21,7 @@ fun NormalizedCache.getReachableCacheKeys(): Set { val cacheKeysToCheck = mutableListOf() for ((key, record) in records) { reachableCacheKeys.add(CacheKey(key)) - cacheKeysToCheck.addAll(record.referencedFields()) + cacheKeysToCheck.addAll(record.referencedFields() - reachableCacheKeys) } if (cacheKeysToCheck.isNotEmpty()) { getReachableCacheKeys(cacheKeysToCheck, reachableCacheKeys) diff --git a/tests/garbage-collection/src/commonMain/graphql/operations.graphql b/tests/garbage-collection/src/commonMain/graphql/operations.graphql index 7d22554d..34795e16 100644 --- a/tests/garbage-collection/src/commonMain/graphql/operations.graphql +++ b/tests/garbage-collection/src/commonMain/graphql/operations.graphql @@ -37,6 +37,9 @@ query RepositoryListQuery { fragment RepositoryFragment on Repository { id + starGazers { + id + } } query ProjectListQuery { diff --git a/tests/garbage-collection/src/commonTest/kotlin/ReachableCacheKeysTest.kt b/tests/garbage-collection/src/commonTest/kotlin/ReachableCacheKeysTest.kt index 1d878fcd..77216fab 100644 --- a/tests/garbage-collection/src/commonTest/kotlin/ReachableCacheKeysTest.kt +++ b/tests/garbage-collection/src/commonTest/kotlin/ReachableCacheKeysTest.kt @@ -48,11 +48,18 @@ class ReachableCacheKeysTest { "repositories": [ { "__typename": "Repository", - "id": "1" + "id": "1", + "starGazers": [ + { + "__typename": "User", + "id": "0" + } + ] }, { "__typename": "Repository", - "id": "2" + "id": "2", + "starGazers": [] } ] }, @@ -66,11 +73,13 @@ class ReachableCacheKeysTest { "repositories": [ { "__typename": "Repository", - "id": "3" + "id": "3", + "starGazers": [] }, { "__typename": "Repository", - "id": "4" + "id": "4", + "starGazers": [] } ] }, @@ -83,15 +92,18 @@ class ReachableCacheKeysTest { "repositories": [ { "__typename": "Repository", - "id": "5" + "id": "5", + "starGazers": [] }, { "__typename": "Repository", - "id": "6" + "id": "6", + "starGazers": [] }, { "__typename": "Repository", - "id": "7" + "id": "7", + "starGazers": [] } ] } @@ -99,11 +111,13 @@ class ReachableCacheKeysTest { "repositories": [ { "__typename": "Repository", - "id": "7" + "id": "7", + "starGazers": [] }, { "__typename": "Repository", - "id": "8" + "id": "8", + "starGazers": [] } ] } @@ -153,7 +167,7 @@ class ReachableCacheKeysTest { store.writeFragment( RepositoryFragmentImpl(), CacheKey("Repository:500"), - RepositoryFragment(id = "500", __typename = "Repository"), + RepositoryFragment(id = "500", __typename = "Repository", starGazers = emptyList()), ) reachableCacheKeys = store.accessCache { it.getReachableCacheKeys() } assertContentEquals(