Skip to content

Commit eef0c84

Browse files
authored
Provide cache dump (#46)
* Provide cache dump * Update AGP (and Gradle) to avoid https://issuetracker.google.com/issues/306301014
1 parent 5da6603 commit eef0c84

File tree

14 files changed

+89
-13
lines changed

14 files changed

+89
-13
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# Next version (unreleased)
22

3-
PUT_CHANGELOG_HERE
3+
- Expiration support (see [the documentation](https://apollographql.github.io/apollo-kotlin-normalized-cache-incubating/expiration.html) for details)
4+
- Compatibility with the IntelliJ plugin cache viewer (#42)
45

56
# Version 0.0.3
67
_2024-09-20_
78

8-
PUT_CHANGELOG_HERE
9+
Tweaks to the `ApolloResolver` API: `resolveField()` now takes a `ResolverContext`
910

1011
# Version 0.0.2
1112
_2024-07-08_

gradle/libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
kotlin-plugin = "2.0.0"
3-
android-plugin = "8.2.2"
4-
apollo = "4.0.1"
3+
android-plugin = "8.7.0"
4+
apollo = "4.0.2-SNAPSHOT"
55
okio = "3.9.0"
66
atomicfu = "0.23.1" # Must be the same version as the one used by apollo-testing-support or native compilation will fail
77
sqldelight = "2.0.1"

gradle/wrapper/gradle-wrapper.jar

130 Bytes
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

gradlew

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
1717
#
18+
# SPDX-License-Identifier: Apache-2.0
19+
#
1820

1921
##############################################################################
2022
#
@@ -84,7 +86,8 @@ done
8486
# shellcheck disable=SC2034
8587
APP_BASE_NAME=${0##*/}
8688
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
87-
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
89+
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
90+
' "$PWD" ) || exit
8891

8992
# Use the maximum available, or set MAX_FD != -1 to use that value.
9093
MAX_FD=maximum

gradlew.bat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
@rem See the License for the specific language governing permissions and
1414
@rem limitations under the License.
1515
@rem
16+
@rem SPDX-License-Identifier: Apache-2.0
17+
@rem
1618

1719
@if "%DEBUG%"=="" @echo off
1820
@rem ##########################################################################

normalized-cache-incubating/build.gradle.kts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ kotlin {
3333
@OptIn(ExperimentalKotlinGradlePluginApi::class)
3434
applyDefaultHierarchyTemplate {
3535
group("common") {
36-
group("noWasm") {
37-
group("concurrent") {
38-
group("apple")
39-
withJvm()
40-
}
36+
group("concurrent") {
37+
group("apple")
4138
withJvm()
4239
}
40+
group("jsCommon") {
41+
group("js") {
42+
withJs()
43+
}
44+
group("wasmJs") {
45+
withWasmJs()
46+
}
47+
}
4348
}
4449
}
4550

normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/ApolloStore.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.apollographql.cache.normalized
33
import com.apollographql.apollo.api.CustomScalarAdapters
44
import com.apollographql.apollo.api.Fragment
55
import com.apollographql.apollo.api.Operation
6+
import com.apollographql.apollo.api.json.JsonNumber
67
import com.apollographql.apollo.interceptor.ApolloInterceptor
78
import com.apollographql.cache.normalized.api.CacheHeaders
89
import com.apollographql.cache.normalized.api.CacheKey
@@ -223,3 +224,39 @@ fun ApolloStore(
223224
* Interface that marks all interceptors added when configuring a `store()` on ApolloClient.Builder.
224225
*/
225226
internal interface ApolloStoreInterceptor : ApolloInterceptor
227+
228+
internal fun ApolloStore.cacheDumpProvider(): () -> Map<String, Map<String, Pair<Int, Map<String, Any?>>>> {
229+
return {
230+
dump().map { (cacheClass, cacheRecords) ->
231+
cacheClass.normalizedCacheName() to cacheRecords.mapValues { (_, record) ->
232+
record.size to record.fields.mapValues { (_, value) ->
233+
value.toExternal()
234+
}
235+
}
236+
}.toMap()
237+
}
238+
}
239+
240+
private fun Any?.toExternal(): Any? {
241+
return when (this) {
242+
null -> null
243+
is String -> this
244+
is Boolean -> this
245+
is Int -> this
246+
is Long -> this
247+
is Double -> this
248+
is JsonNumber -> this
249+
is CacheKey -> this.serialize()
250+
is List<*> -> {
251+
map { it.toExternal() }
252+
}
253+
254+
is Map<*, *> -> {
255+
mapValues { it.value.toExternal() }
256+
}
257+
258+
else -> error("Unsupported record value type: '$this'")
259+
}
260+
}
261+
262+
internal expect fun KClass<*>.normalizedCacheName(): String

normalized-cache-incubating/src/commonMain/kotlin/com/apollographql/cache/normalized/ClientCacheExtensions.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.apollographql.cache.normalized
44

55
import com.apollographql.apollo.ApolloCall
66
import com.apollographql.apollo.ApolloClient
7+
import com.apollographql.apollo.CacheDumpProviderContext
78
import com.apollographql.apollo.annotations.ApolloDeprecatedSince
89
import com.apollographql.apollo.annotations.ApolloDeprecatedSince.Version.v4_0_0
910
import com.apollographql.apollo.api.ApolloRequest
@@ -155,6 +156,7 @@ fun ApolloClient.Builder.store(store: ApolloStore, writeToCacheAsynchronously: B
155156
.addInterceptor(FetchPolicyRouterInterceptor)
156157
.addInterceptor(ApolloCacheInterceptor(store))
157158
.writeToCacheAsynchronously(writeToCacheAsynchronously)
159+
.addExecutionContext(CacheDumpProviderContext(store.cacheDumpProvider()))
158160
}
159161

160162
@Deprecated(level = DeprecationLevel.ERROR, message = "Exceptions no longer throw", replaceWith = ReplaceWith("watch()"))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.apollographql.cache.normalized
2+
3+
import kotlin.reflect.KClass
4+
5+
internal actual fun KClass<*>.normalizedCacheName(): String {
6+
return qualifiedName ?: toString()
7+
}

0 commit comments

Comments
 (0)