Skip to content

Provide cache dump #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Next version (unreleased)

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

# Version 0.0.3
_2024-09-20_

PUT_CHANGELOG_HERE
Tweaks to the `ApolloResolver` API: `resolveField()` now takes a `ResolverContext`

# Version 0.0.2
_2024-07-08_
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
kotlin-plugin = "2.0.0"
android-plugin = "8.2.2"
apollo = "4.0.1"
android-plugin = "8.7.0"
apollo = "4.0.2-SNAPSHOT"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another use case for an immutable/apollo-owned repo.

okio = "3.9.0"
atomicfu = "0.23.1" # Must be the same version as the one used by apollo-testing-support or native compilation will fail
sqldelight = "2.0.1"
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
5 changes: 4 additions & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

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

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
2 changes: 2 additions & 0 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down
15 changes: 10 additions & 5 deletions normalized-cache-incubating/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ kotlin {
@OptIn(ExperimentalKotlinGradlePluginApi::class)
applyDefaultHierarchyTemplate {
group("common") {
group("noWasm") {
group("concurrent") {
group("apple")
withJvm()
}
group("concurrent") {
group("apple")
withJvm()
}
group("jsCommon") {
group("js") {
withJs()
}
group("wasmJs") {
withWasmJs()
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.apollographql.cache.normalized
import com.apollographql.apollo.api.CustomScalarAdapters
import com.apollographql.apollo.api.Fragment
import com.apollographql.apollo.api.Operation
import com.apollographql.apollo.api.json.JsonNumber
import com.apollographql.apollo.interceptor.ApolloInterceptor
import com.apollographql.cache.normalized.api.CacheHeaders
import com.apollographql.cache.normalized.api.CacheKey
Expand Down Expand Up @@ -223,3 +224,39 @@ fun ApolloStore(
* Interface that marks all interceptors added when configuring a `store()` on ApolloClient.Builder.
*/
internal interface ApolloStoreInterceptor : ApolloInterceptor

internal fun ApolloStore.cacheDumpProvider(): () -> Map<String, Map<String, Pair<Int, Map<String, Any?>>>> {
return {
dump().map { (cacheClass, cacheRecords) ->
cacheClass.normalizedCacheName() to cacheRecords.mapValues { (_, record) ->
record.size to record.fields.mapValues { (_, value) ->
value.toExternal()
}
}
}.toMap()
}
}

private fun Any?.toExternal(): Any? {
return when (this) {
null -> null
is String -> this
is Boolean -> this
is Int -> this
is Long -> this
is Double -> this
is JsonNumber -> this
is CacheKey -> this.serialize()
is List<*> -> {
map { it.toExternal() }
}

is Map<*, *> -> {
mapValues { it.value.toExternal() }
}

else -> error("Unsupported record value type: '$this'")
}
}

internal expect fun KClass<*>.normalizedCacheName(): String
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.apollographql.cache.normalized

import com.apollographql.apollo.ApolloCall
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.CacheDumpProviderContext
import com.apollographql.apollo.annotations.ApolloDeprecatedSince
import com.apollographql.apollo.annotations.ApolloDeprecatedSince.Version.v4_0_0
import com.apollographql.apollo.api.ApolloRequest
Expand Down Expand Up @@ -155,6 +156,7 @@ fun ApolloClient.Builder.store(store: ApolloStore, writeToCacheAsynchronously: B
.addInterceptor(FetchPolicyRouterInterceptor)
.addInterceptor(ApolloCacheInterceptor(store))
.writeToCacheAsynchronously(writeToCacheAsynchronously)
.addExecutionContext(CacheDumpProviderContext(store.cacheDumpProvider()))
}

@Deprecated(level = DeprecationLevel.ERROR, message = "Exceptions no longer throw", replaceWith = ReplaceWith("watch()"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.apollographql.cache.normalized

import kotlin.reflect.KClass

internal actual fun KClass<*>.normalizedCacheName(): String {
return qualifiedName ?: toString()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.apollographql.cache.normalized

import kotlin.reflect.KClass

internal actual fun KClass<*>.normalizedCacheName(): String {
// qualifiedName is unsupported in JS
return simpleName ?: toString()
}
9 changes: 7 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
pluginManagement {
listOf(repositories, dependencyResolutionManagement.repositories).forEach {
it.mavenCentral()
it.google()
it.apply {
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
mavenCentral()
google()
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
buildscript {
repositories {
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
mavenCentral()
google()
}
Expand Down
3 changes: 3 additions & 0 deletions tests/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ includeBuild("../")
pluginManagement {
listOf(repositories, dependencyResolutionManagement.repositories).forEach {
it.apply {
maven {
url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
mavenCentral()
}
}
Expand Down