From 3e63a03067210e83e5553b6d8c312e2930fa15c5 Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 2 Jun 2025 11:40:25 +0200 Subject: [PATCH 01/10] Update Apollo compiler plugin to 4.3 API --- gradle/libs.versions.toml | 2 +- gradle/repositories.gradle.kts | 2 +- ...ormalized-cache-apollo-compiler-plugin.api | 4 +-- .../ApolloCacheCompilerPlugin.kt | 23 ++++++++++++++++ .../ApolloCacheCompilerPluginProvider.kt | 15 ----------- .../internal/AddKeyFieldsDocumentTransform.kt | 26 ++++++++++++------- .../internal/ApolloCacheCompilerPlugin.kt | 26 ------------------- .../internal/CacheSchemaListener.kt | 15 ++++++----- ...phql.apollo.compiler.ApolloCompilerPlugin} | 2 +- ...yFieldsExecutableDocumentTransformTest.kt} | 11 ++++---- .../kotlin/test/CachePartialResultTest.kt | 2 ++ .../commonTest/kotlin/test/StoreErrorsTest.kt | 1 + 12 files changed, 62 insertions(+), 67 deletions(-) create mode 100644 normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt delete mode 100644 normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt delete mode 100644 normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt rename normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/{com.apollographql.apollo.compiler.ApolloCompilerPluginProvider => com.apollographql.apollo.compiler.ApolloCompilerPlugin} (81%) rename normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/{AddKeyFieldsDocumentTransformTest.kt => AddKeyFieldsExecutableDocumentTransformTest.kt} (93%) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 16cb839a..f5f783d8 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] kotlin-plugin = "2.1.21" android-plugin = "8.7.0" -apollo = "4.2.0" +apollo = "4.2.1-SNAPSHOT" 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.1.0" diff --git a/gradle/repositories.gradle.kts b/gradle/repositories.gradle.kts index ab0f8166..7eb33783 100644 --- a/gradle/repositories.gradle.kts +++ b/gradle/repositories.gradle.kts @@ -1,6 +1,6 @@ listOf(pluginManagement.repositories, dependencyResolutionManagement.repositories).forEach { it.apply { -// mavenLocal() + mavenLocal() // maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") // maven("https://storage.googleapis.com/apollo-previews/m2/") mavenCentral() diff --git a/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api b/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api index d4eca44a..d04839e9 100644 --- a/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api +++ b/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api @@ -1,6 +1,6 @@ -public final class com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider : com/apollographql/apollo/compiler/ApolloCompilerPluginProvider { +public final class com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin : com/apollographql/apollo/compiler/ApolloCompilerPlugin { public fun ()V - public fun create (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;)Lcom/apollographql/apollo/compiler/ApolloCompilerPlugin; + public fun beforeCompilationStep (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;Lcom/apollographql/apollo/compiler/ApolloCompilerRegistry;)V } public final class com/apollographql/cache/apollocompilerplugin/VersionKt { diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt new file mode 100644 index 00000000..11936f58 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt @@ -0,0 +1,23 @@ +@file:OptIn(ApolloExperimental::class) + +package com.apollographql.cache.apollocompilerplugin + +import com.apollographql.apollo.annotations.ApolloExperimental +import com.apollographql.apollo.ast.ForeignSchema +import com.apollographql.apollo.compiler.ApolloCompilerPlugin +import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment +import com.apollographql.apollo.compiler.ApolloCompilerRegistry +import com.apollographql.cache.apollocompilerplugin.internal.AddKeyFieldsExecutableDocumentTransform +import com.apollographql.cache.apollocompilerplugin.internal.CacheCodeGenerator +import com.apollographql.cache.apollocompilerplugin.internal.cacheGQLDefinitions + +class ApolloCacheCompilerPlugin : ApolloCompilerPlugin { + override fun beforeCompilationStep( + environment: ApolloCompilerPluginEnvironment, + registry: ApolloCompilerRegistry, + ) { + registry.registerForeignSchemas(listOf(ForeignSchema("cache", "v0.1", cacheGQLDefinitions))) + registry.registerExecutableDocumentTransform("com.apollographql.cache.addKeyFields", transform = AddKeyFieldsExecutableDocumentTransform) + registry.registerExtraCodeGenerator(CacheCodeGenerator(environment)) + } +} diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt deleted file mode 100644 index 02626809..00000000 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt +++ /dev/null @@ -1,15 +0,0 @@ -@file:OptIn(ApolloExperimental::class) - -package com.apollographql.cache.apollocompilerplugin - -import com.apollographql.apollo.annotations.ApolloExperimental -import com.apollographql.apollo.compiler.ApolloCompilerPlugin -import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment -import com.apollographql.apollo.compiler.ApolloCompilerPluginProvider -import com.apollographql.cache.apollocompilerplugin.internal.ApolloCacheCompilerPlugin - -class ApolloCacheCompilerPluginProvider : ApolloCompilerPluginProvider { - override fun create(environment: ApolloCompilerPluginEnvironment): ApolloCompilerPlugin { - return ApolloCacheCompilerPlugin(environment) - } -} diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt index a60a6f80..d3a36f81 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt @@ -4,6 +4,7 @@ package com.apollographql.cache.apollocompilerplugin.internal import com.apollographql.apollo.annotations.ApolloExperimental import com.apollographql.apollo.annotations.ApolloInternal +import com.apollographql.apollo.ast.GQLDocument import com.apollographql.apollo.ast.GQLField import com.apollographql.apollo.ast.GQLFragmentDefinition import com.apollographql.apollo.ast.GQLFragmentSpread @@ -19,18 +20,26 @@ import com.apollographql.apollo.ast.definitionFromScope import com.apollographql.apollo.ast.rawType import com.apollographql.apollo.ast.responseName import com.apollographql.apollo.ast.rootTypeDefinition -import com.apollographql.apollo.compiler.DocumentTransform +import com.apollographql.apollo.compiler.ExecutableDocumentTransform /** * Add key fields and `__typename` to selections on types that declare them via `@typePolicy`. */ -internal class AddKeyFieldsDocumentTransform : DocumentTransform { - override fun transform(schema: Schema, fragment: GQLFragmentDefinition): GQLFragmentDefinition { - return fragment.withRequiredFields(schema) - } - - override fun transform(schema: Schema, operation: GQLOperationDefinition): GQLOperationDefinition { - return operation.withRequiredFields(schema) +internal object AddKeyFieldsExecutableDocumentTransform : ExecutableDocumentTransform { + override fun transform( + schema: Schema, + document: GQLDocument, + extraFragmentDefinitions: List, + ): GQLDocument { + return document.copy( + definitions = document.definitions.map { + when (it) { + is GQLFragmentDefinition -> it.withRequiredFields(schema) + is GQLOperationDefinition -> it.withRequiredFields(schema) + else -> it + } + } + ) } private fun GQLOperationDefinition.withRequiredFields(schema: Schema): GQLOperationDefinition { @@ -152,7 +161,6 @@ internal class AddKeyFieldsDocumentTransform : DocumentTransform { return copy(selections = newSelectionSet) } - @OptIn(ApolloExperimental::class) private fun buildField(name: String): GQLField { return GQLField( name = name, diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt deleted file mode 100644 index 7ec7282c..00000000 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt +++ /dev/null @@ -1,26 +0,0 @@ -@file:OptIn(ApolloExperimental::class) - -package com.apollographql.cache.apollocompilerplugin.internal - -import com.apollographql.apollo.annotations.ApolloExperimental -import com.apollographql.apollo.ast.ForeignSchema -import com.apollographql.apollo.compiler.ApolloCompilerPlugin -import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment -import com.apollographql.apollo.compiler.DocumentTransform -import com.apollographql.apollo.compiler.SchemaListener - -internal class ApolloCacheCompilerPlugin( - private val environment: ApolloCompilerPluginEnvironment, -) : ApolloCompilerPlugin { - override fun foreignSchemas(): List { - return listOf(ForeignSchema("cache", "v0.1", cacheGQLDefinitions)) - } - - override fun schemaListener(): SchemaListener { - return CacheSchemaListener(environment) - } - - override fun documentTransform(): DocumentTransform { - return AddKeyFieldsDocumentTransform() - } -} diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaListener.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaListener.kt index 01da16b3..40be7a7f 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaListener.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaListener.kt @@ -3,9 +3,11 @@ package com.apollographql.cache.apollocompilerplugin.internal import com.apollographql.apollo.annotations.ApolloExperimental +import com.apollographql.apollo.ast.GQLDocument import com.apollographql.apollo.ast.Schema +import com.apollographql.apollo.ast.toSchema import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment -import com.apollographql.apollo.compiler.SchemaListener +import com.apollographql.apollo.compiler.CodeGenerator import com.apollographql.cache.apollocompilerplugin.VERSION import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock @@ -29,17 +31,18 @@ private object Symbols { val TypePolicy = ClassName("com.apollographql.cache.normalized.api", "TypePolicy") } -internal class CacheSchemaListener( +internal class CacheCodeGenerator( private val environment: ApolloCompilerPluginEnvironment, -) : SchemaListener { - override fun onSchema(schema: Schema, outputDirectory: File) { +) : CodeGenerator { + override fun generate(schema: GQLDocument, outputDirectory: File) { + val validSchema = schema.toSchema() val packageName = (environment.arguments["packageName"] as? String ?: throw IllegalArgumentException("packageName argument is required and must be a String")) + ".cache" val file = FileSpec.builder(packageName, "Cache") .addType( TypeSpec.objectBuilder("Cache") - .addProperty(maxAgeProperty(schema)) - .addProperty(typePoliciesProperty(schema)) + .addProperty(maxAgeProperty(validSchema)) + .addProperty(typePoliciesProperty(validSchema)) .build() ) .addFileComment( diff --git a/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPluginProvider b/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPlugin similarity index 81% rename from normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPluginProvider rename to normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPlugin index 1b38e2b9..8ae9afaa 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPluginProvider +++ b/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPlugin @@ -1 +1 @@ -com.apollographql.cache.apollocompilerplugin.ApolloCacheCompilerPluginProvider +com.apollographql.cache.apollocompilerplugin.ApolloCacheCompilerPlugin diff --git a/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransformTest.kt b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsExecutableDocumentTransformTest.kt similarity index 93% rename from normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransformTest.kt rename to normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsExecutableDocumentTransformTest.kt index 74518bfe..20c91c0f 100644 --- a/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransformTest.kt +++ b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsExecutableDocumentTransformTest.kt @@ -3,7 +3,6 @@ package com.apollographql.cache.apollocompilerplugin.internal import com.apollographql.apollo.annotations.ApolloExperimental -import com.apollographql.apollo.ast.GQLOperationDefinition import com.apollographql.apollo.ast.SourceAwareException import com.apollographql.apollo.ast.internal.SchemaValidationOptions import com.apollographql.apollo.ast.parseAsGQLDocument @@ -14,7 +13,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith -class AddKeyFieldsDocumentTransformTest { +class AddKeyFieldsExecutableDocumentTransformTest { @Test fun keyFieldsOnObject() { // language=GraphQL @@ -326,8 +325,8 @@ private fun checkTransform(schemaText: String, operationText: String, expected: foreignSchemas = emptyList(), ) ).getOrThrow() - val operation = operationText.toGQLDocument().definitions.first() as GQLOperationDefinition - assertEquals(expected, AddKeyFieldsDocumentTransform().transform(schema, operation).toUtf8().trim()) + val document = operationText.toGQLDocument() + assertEquals(expected, AddKeyFieldsExecutableDocumentTransform.transform(schema, document, emptyList()).toUtf8().trim()) } private fun checkTransformThrows(schemaText: String, operationText: String, expectedMessage: String) { @@ -339,9 +338,9 @@ private fun checkTransformThrows(schemaText: String, operationText: String, expe foreignSchemas = emptyList(), ) ).getOrThrow() - val operation = operationText.toGQLDocument().definitions.first() as GQLOperationDefinition + val document = operationText.toGQLDocument() assertFailsWith { - AddKeyFieldsDocumentTransform().transform(schema, operation) + AddKeyFieldsExecutableDocumentTransform.transform(schema, document, emptyList()) }.apply { assertEquals(expectedMessage, message) } diff --git a/tests/partial-results/src/commonTest/kotlin/test/CachePartialResultTest.kt b/tests/partial-results/src/commonTest/kotlin/test/CachePartialResultTest.kt index a58d4482..797594d7 100644 --- a/tests/partial-results/src/commonTest/kotlin/test/CachePartialResultTest.kt +++ b/tests/partial-results/src/commonTest/kotlin/test/CachePartialResultTest.kt @@ -624,6 +624,7 @@ class CachePartialResultTest { ), ), onUser = WithFragmentsQuery.OnUser( + __typename = "User", lastName = "Smith", onUser = WithFragmentsQuery.OnUser1( nickName0 = "JS" @@ -669,6 +670,7 @@ class CachePartialResultTest { lead0 = null, ), onUser = WithFragmentsQuery.OnUser( + __typename = "User", lastName = "Smith", onUser = WithFragmentsQuery.OnUser1( nickName0 = "JS" diff --git a/tests/store-errors/src/commonTest/kotlin/test/StoreErrorsTest.kt b/tests/store-errors/src/commonTest/kotlin/test/StoreErrorsTest.kt index 0b82e084..364ce8ea 100644 --- a/tests/store-errors/src/commonTest/kotlin/test/StoreErrorsTest.kt +++ b/tests/store-errors/src/commonTest/kotlin/test/StoreErrorsTest.kt @@ -497,6 +497,7 @@ class StoreErrorsTest { ), ), onUser = WithFragmentsQuery.OnUser( + __typename = "User", lastName = "Smith", onUser = WithFragmentsQuery.OnUser1( nickName0 = "JS" From 6fe312aafe4907ab643f295f18639f4a10702eaa Mon Sep 17 00:00:00 2001 From: BoD Date: Wed, 4 Jun 2025 11:22:16 +0200 Subject: [PATCH 02/10] Add a check for Apollo compiler v4.3, and revert to plugin provider --- gradle/libs.versions.toml | 2 +- ...ormalized-cache-apollo-compiler-plugin.api | 4 +-- .../ApolloCacheCompilerPluginProvider.kt | 26 +++++++++++++++++++ .../ApolloCacheCompilerPlugin.kt | 9 +++---- ...istener.kt => CacheSchemaCodeGenerator.kt} | 6 ++--- ...llo.compiler.ApolloCompilerPluginProvider} | 2 +- 6 files changed, 36 insertions(+), 13 deletions(-) create mode 100644 normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt rename normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/{ => internal}/ApolloCacheCompilerPlugin.kt (62%) rename normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/{CacheSchemaListener.kt => CacheSchemaCodeGenerator.kt} (96%) rename normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/{com.apollographql.apollo.compiler.ApolloCompilerPlugin => com.apollographql.apollo.compiler.ApolloCompilerPluginProvider} (81%) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f5f783d8..f75f971e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] kotlin-plugin = "2.1.21" android-plugin = "8.7.0" -apollo = "4.2.1-SNAPSHOT" +apollo = "4.3.0-SNAPSHOT" 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.1.0" diff --git a/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api b/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api index d04839e9..d4eca44a 100644 --- a/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api +++ b/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api @@ -1,6 +1,6 @@ -public final class com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin : com/apollographql/apollo/compiler/ApolloCompilerPlugin { +public final class com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider : com/apollographql/apollo/compiler/ApolloCompilerPluginProvider { public fun ()V - public fun beforeCompilationStep (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;Lcom/apollographql/apollo/compiler/ApolloCompilerRegistry;)V + public fun create (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;)Lcom/apollographql/apollo/compiler/ApolloCompilerPlugin; } public final class com/apollographql/cache/apollocompilerplugin/VersionKt { diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt new file mode 100644 index 00000000..39b26215 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt @@ -0,0 +1,26 @@ +@file:OptIn(ApolloExperimental::class) + +package com.apollographql.cache.apollocompilerplugin + +import com.apollographql.apollo.annotations.ApolloExperimental +import com.apollographql.apollo.compiler.APOLLO_VERSION +import com.apollographql.apollo.compiler.ApolloCompilerPlugin +import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment +import com.apollographql.cache.apollocompilerplugin.internal.ApolloCacheCompilerPlugin + +class ApolloCacheCompilerPluginProvider : @Suppress("DEPRECATION") com.apollographql.apollo.compiler.ApolloCompilerPluginProvider { + override fun create(environment: ApolloCompilerPluginEnvironment): ApolloCompilerPlugin { + checkCompilerVersion() + return ApolloCacheCompilerPlugin() + } +} + +private fun checkCompilerVersion() { + val matchResult = Regex("""^(\d+)\.(\d+).*$""").matchEntire(APOLLO_VERSION) + val versionMajor = matchResult?.groupValues?.get(1)?.toIntOrNull() + val versionMinor = matchResult?.groupValues?.get(2)?.toIntOrNull() + if (versionMinor == null || versionMajor == null) error("Invalid Apollo Kotlin compiler version: $APOLLO_VERSION") + if (versionMajor < 4 || (versionMajor == 4 && versionMinor < 3)) { + error("The Apollo Cache compiler plugin requires Apollo Kotlin version 4.3.0 or higher (found $APOLLO_VERSION)") + } +} diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt similarity index 62% rename from normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt rename to normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt index 11936f58..c8bb1675 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt @@ -1,23 +1,20 @@ @file:OptIn(ApolloExperimental::class) -package com.apollographql.cache.apollocompilerplugin +package com.apollographql.cache.apollocompilerplugin.internal import com.apollographql.apollo.annotations.ApolloExperimental import com.apollographql.apollo.ast.ForeignSchema import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment import com.apollographql.apollo.compiler.ApolloCompilerRegistry -import com.apollographql.cache.apollocompilerplugin.internal.AddKeyFieldsExecutableDocumentTransform -import com.apollographql.cache.apollocompilerplugin.internal.CacheCodeGenerator -import com.apollographql.cache.apollocompilerplugin.internal.cacheGQLDefinitions -class ApolloCacheCompilerPlugin : ApolloCompilerPlugin { +internal class ApolloCacheCompilerPlugin : ApolloCompilerPlugin { override fun beforeCompilationStep( environment: ApolloCompilerPluginEnvironment, registry: ApolloCompilerRegistry, ) { registry.registerForeignSchemas(listOf(ForeignSchema("cache", "v0.1", cacheGQLDefinitions))) registry.registerExecutableDocumentTransform("com.apollographql.cache.addKeyFields", transform = AddKeyFieldsExecutableDocumentTransform) - registry.registerExtraCodeGenerator(CacheCodeGenerator(environment)) + registry.registerSchemaCodeGenerator(CacheSchemaCodeGenerator(environment)) } } diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaListener.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaCodeGenerator.kt similarity index 96% rename from normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaListener.kt rename to normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaCodeGenerator.kt index 40be7a7f..505a600a 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaListener.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/CacheSchemaCodeGenerator.kt @@ -7,7 +7,7 @@ import com.apollographql.apollo.ast.GQLDocument import com.apollographql.apollo.ast.Schema import com.apollographql.apollo.ast.toSchema import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment -import com.apollographql.apollo.compiler.CodeGenerator +import com.apollographql.apollo.compiler.SchemaCodeGenerator import com.apollographql.cache.apollocompilerplugin.VERSION import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock @@ -31,9 +31,9 @@ private object Symbols { val TypePolicy = ClassName("com.apollographql.cache.normalized.api", "TypePolicy") } -internal class CacheCodeGenerator( +internal class CacheSchemaCodeGenerator( private val environment: ApolloCompilerPluginEnvironment, -) : CodeGenerator { +) : SchemaCodeGenerator { override fun generate(schema: GQLDocument, outputDirectory: File) { val validSchema = schema.toSchema() val packageName = (environment.arguments["packageName"] as? String diff --git a/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPlugin b/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPluginProvider similarity index 81% rename from normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPlugin rename to normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPluginProvider index 8ae9afaa..1b38e2b9 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPlugin +++ b/normalized-cache-apollo-compiler-plugin/src/main/resources/META-INF/services/com.apollographql.apollo.compiler.ApolloCompilerPluginProvider @@ -1 +1 @@ -com.apollographql.cache.apollocompilerplugin.ApolloCacheCompilerPlugin +com.apollographql.cache.apollocompilerplugin.ApolloCacheCompilerPluginProvider From adc1744b11db44de2624cc289c299b2ad3f5c061 Mon Sep 17 00:00:00 2001 From: BoD Date: Wed, 4 Jun 2025 11:26:06 +0200 Subject: [PATCH 03/10] Add a comment about using ApolloCacheCompilerPluginProvider --- .../apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt index 39b26215..e0139b07 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt @@ -8,6 +8,8 @@ import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment import com.apollographql.cache.apollocompilerplugin.internal.ApolloCacheCompilerPlugin +// ApolloCacheCompilerPluginProvider is deprecated in favor of ApolloCompilerPluginProvider, but we want to display a nice error message +// in projects using AK < 4.3.0 class ApolloCacheCompilerPluginProvider : @Suppress("DEPRECATION") com.apollographql.apollo.compiler.ApolloCompilerPluginProvider { override fun create(environment: ApolloCompilerPluginEnvironment): ApolloCompilerPlugin { checkCompilerVersion() From 7543c41754e2b2bd114e8d1d6016e7f2d09dc579 Mon Sep 17 00:00:00 2001 From: BoD Date: Wed, 4 Jun 2025 11:41:06 +0200 Subject: [PATCH 04/10] Make ApolloCacheCompilerPlugin again --- .../api/normalized-cache-apollo-compiler-plugin.api | 5 +++++ .../{internal => }/ApolloCacheCompilerPlugin.kt | 7 +++++-- .../ApolloCacheCompilerPluginProvider.kt | 3 +-- 3 files changed, 11 insertions(+), 4 deletions(-) rename normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/{internal => }/ApolloCacheCompilerPlugin.kt (68%) diff --git a/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api b/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api index d4eca44a..be70db4c 100644 --- a/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api +++ b/normalized-cache-apollo-compiler-plugin/api/normalized-cache-apollo-compiler-plugin.api @@ -1,3 +1,8 @@ +public final class com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin : com/apollographql/apollo/compiler/ApolloCompilerPlugin { + public fun ()V + public fun beforeCompilationStep (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;Lcom/apollographql/apollo/compiler/ApolloCompilerRegistry;)V +} + public final class com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider : com/apollographql/apollo/compiler/ApolloCompilerPluginProvider { public fun ()V public fun create (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;)Lcom/apollographql/apollo/compiler/ApolloCompilerPlugin; diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt similarity index 68% rename from normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt rename to normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt index c8bb1675..40255d17 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloCacheCompilerPlugin.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPlugin.kt @@ -1,14 +1,17 @@ @file:OptIn(ApolloExperimental::class) -package com.apollographql.cache.apollocompilerplugin.internal +package com.apollographql.cache.apollocompilerplugin import com.apollographql.apollo.annotations.ApolloExperimental import com.apollographql.apollo.ast.ForeignSchema import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment import com.apollographql.apollo.compiler.ApolloCompilerRegistry +import com.apollographql.cache.apollocompilerplugin.internal.AddKeyFieldsExecutableDocumentTransform +import com.apollographql.cache.apollocompilerplugin.internal.CacheSchemaCodeGenerator +import com.apollographql.cache.apollocompilerplugin.internal.cacheGQLDefinitions -internal class ApolloCacheCompilerPlugin : ApolloCompilerPlugin { +class ApolloCacheCompilerPlugin : ApolloCompilerPlugin { override fun beforeCompilationStep( environment: ApolloCompilerPluginEnvironment, registry: ApolloCompilerRegistry, diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt index e0139b07..b8588501 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/ApolloCacheCompilerPluginProvider.kt @@ -6,9 +6,8 @@ import com.apollographql.apollo.annotations.ApolloExperimental import com.apollographql.apollo.compiler.APOLLO_VERSION import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment -import com.apollographql.cache.apollocompilerplugin.internal.ApolloCacheCompilerPlugin -// ApolloCacheCompilerPluginProvider is deprecated in favor of ApolloCompilerPluginProvider, but we want to display a nice error message +// ApolloCacheCompilerPluginProvider is deprecated in favor of ApolloCompilerPlugin, but we want to display a nice error message // in projects using AK < 4.3.0 class ApolloCacheCompilerPluginProvider : @Suppress("DEPRECATION") com.apollographql.apollo.compiler.ApolloCompilerPluginProvider { override fun create(environment: ApolloCompilerPluginEnvironment): ApolloCompilerPlugin { From d11a6338adcf9ba357bd9d4ab455d4899ee61bb0 Mon Sep 17 00:00:00 2001 From: Benoit 'BoD' Lubek Date: Wed, 4 Jun 2025 12:07:57 +0200 Subject: [PATCH 05/10] Update normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt Co-authored-by: Martin Bonnin --- .../internal/AddKeyFieldsDocumentTransform.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt index d3a36f81..154c79a0 100644 --- a/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt +++ b/normalized-cache-apollo-compiler-plugin/src/main/kotlin/com/apollographql/cache/apollocompilerplugin/internal/AddKeyFieldsDocumentTransform.kt @@ -23,7 +23,7 @@ import com.apollographql.apollo.ast.rootTypeDefinition import com.apollographql.apollo.compiler.ExecutableDocumentTransform /** - * Add key fields and `__typename` to selections on types that declare them via `@typePolicy`. + * Add `__typename` to every composite selection set and key fields to selection sets on types where `@typePolicy` declare them. */ internal object AddKeyFieldsExecutableDocumentTransform : ExecutableDocumentTransform { override fun transform( From bba565de4e561c4331f4b38074506d4b24ec2de7 Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Thu, 5 Jun 2025 10:28:05 +0200 Subject: [PATCH 06/10] use the preview --- gradle/libs.versions.toml | 2 +- gradle/repositories.gradle.kts | 2 +- samples/pagination/manual/settings.gradle.kts | 4 ++-- .../settings.gradle.kts | 4 ++-- samples/pagination/pagination-support/settings.gradle.kts | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f75f971e..3fbcdf9a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] kotlin-plugin = "2.1.21" android-plugin = "8.7.0" -apollo = "4.3.0-SNAPSHOT" +apollo = "4.3.0-alpha.plugins" 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.1.0" diff --git a/gradle/repositories.gradle.kts b/gradle/repositories.gradle.kts index 7eb33783..4593e532 100644 --- a/gradle/repositories.gradle.kts +++ b/gradle/repositories.gradle.kts @@ -1,6 +1,6 @@ listOf(pluginManagement.repositories, dependencyResolutionManagement.repositories).forEach { it.apply { - mavenLocal() + maven("https://storage.googleapis.com/apollo-previews/m2/") // maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") // maven("https://storage.googleapis.com/apollo-previews/m2/") mavenCentral() diff --git a/samples/pagination/manual/settings.gradle.kts b/samples/pagination/manual/settings.gradle.kts index ac2ce959..5bc3c95a 100644 --- a/samples/pagination/manual/settings.gradle.kts +++ b/samples/pagination/manual/settings.gradle.kts @@ -1,6 +1,6 @@ pluginManagement { repositories { - mavenLocal() + maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } @@ -11,7 +11,7 @@ pluginManagement { } dependencyResolutionManagement { repositories { - mavenLocal() + maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } diff --git a/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts b/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts index ac2ce959..5bc3c95a 100644 --- a/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts +++ b/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts @@ -1,6 +1,6 @@ pluginManagement { repositories { - mavenLocal() + maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } @@ -11,7 +11,7 @@ pluginManagement { } dependencyResolutionManagement { repositories { - mavenLocal() + maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } diff --git a/samples/pagination/pagination-support/settings.gradle.kts b/samples/pagination/pagination-support/settings.gradle.kts index ac2ce959..5bc3c95a 100644 --- a/samples/pagination/pagination-support/settings.gradle.kts +++ b/samples/pagination/pagination-support/settings.gradle.kts @@ -1,6 +1,6 @@ pluginManagement { repositories { - mavenLocal() + maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } @@ -11,7 +11,7 @@ pluginManagement { } dependencyResolutionManagement { repositories { - mavenLocal() + maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } From e04031dbf8f9e604fb84fbdb045a28f381b0c96a Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Thu, 5 Jun 2025 10:52:50 +0200 Subject: [PATCH 07/10] Add a test for Apollo 4.2 --- gradle/libs.versions.toml | 1 + .../build.gradle.kts | 1 + .../internal/ApolloVersionTest.kt | 28 +++++++++++++++++++ .../test-data/testProject/README.md | 1 + .../test-data/testProject/build.gradle.kts | 13 +++++++++ .../test-data/testProject/gradle | 1 + .../test-data/testProject/gradlew | 1 + .../test-data/testProject/settings.gradle.kts | 3 ++ .../src/main/graphql/operations.graphql | 5 ++++ .../src/main/graphql/schema.graphqls | 9 ++++++ .../testProject/src/main/kotlin/hello.kt | 1 + 11 files changed, 64 insertions(+) create mode 100644 normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt create mode 100644 normalized-cache-apollo-compiler-plugin/test-data/testProject/README.md create mode 100644 normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts create mode 120000 normalized-cache-apollo-compiler-plugin/test-data/testProject/gradle create mode 120000 normalized-cache-apollo-compiler-plugin/test-data/testProject/gradlew create mode 100644 normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts create mode 100644 normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/operations.graphql create mode 100644 normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls create mode 100644 normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/kotlin/hello.kt diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3fbcdf9a..73fda4c6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -49,4 +49,5 @@ sqldelight-plugin = { module = "app.cash.sqldelight:gradle-plugin", version.ref [plugins] kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin-plugin" } +kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin-plugin" } apollo = { id = "com.apollographql.apollo", version.ref = "apollo" } diff --git a/normalized-cache-apollo-compiler-plugin/build.gradle.kts b/normalized-cache-apollo-compiler-plugin/build.gradle.kts index 53671f45..f45b9dab 100644 --- a/normalized-cache-apollo-compiler-plugin/build.gradle.kts +++ b/normalized-cache-apollo-compiler-plugin/build.gradle.kts @@ -9,6 +9,7 @@ Librarian.module(project) dependencies { compileOnly(libs.apollo.compiler) testImplementation(libs.apollo.compiler) + testImplementation(gradleTestKit()) implementation(libs.apollo.ast) implementation(libs.kotlin.poet) testImplementation(libs.kotlin.test) diff --git a/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt new file mode 100644 index 00000000..938bd20a --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt @@ -0,0 +1,28 @@ +package com.apollographql.cache.apollocompilerplugin.internal + +import org.gradle.testkit.runner.GradleRunner +import org.gradle.testkit.runner.UnexpectedBuildFailure +import java.io.File +import kotlin.test.Test +import kotlin.test.assertFailsWith +import kotlin.test.assertTrue + +class ApolloVersionTest { + + @Test + fun failsBelowApollo4_3() { + val workingDir = File("build/testProject") + + workingDir.deleteRecursively() + File("test-data/testProject").copyRecursively(workingDir) + try { + GradleRunner.create() + .withProjectDir(workingDir) + .withDebug(true) + .withArguments("generateServiceApolloSources") + .build() + } catch (e: UnexpectedBuildFailure) { + assertTrue(e.message!!.contains("The Apollo Cache compiler plugin requires Apollo Kotlin version 4.3.0 or higher (found 4.2.0)")) + } + } +} \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/README.md b/normalized-cache-apollo-compiler-plugin/test-data/testProject/README.md new file mode 100644 index 00000000..6e5dc4f8 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/README.md @@ -0,0 +1 @@ +A project that uses Apollo Kotlin 4.2.0 to double check we are failing with a "good" error message. \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts b/normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts new file mode 100644 index 00000000..b7d37123 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/build.gradle.kts @@ -0,0 +1,13 @@ +plugins { + alias(libs.plugins.kotlin.jvm) + id("com.apollographql.apollo").version("4.2.0") +} + +apollo { + service("service") { + packageName.set("com.example") + plugin("com.apollographql.cache:normalized-cache-apollo-compiler-plugin") { + argument("packageName", packageName.get()) + } + } +} diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/gradle b/normalized-cache-apollo-compiler-plugin/test-data/testProject/gradle new file mode 120000 index 00000000..84b694e3 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/gradle @@ -0,0 +1 @@ +../../../gradle \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/gradlew b/normalized-cache-apollo-compiler-plugin/test-data/testProject/gradlew new file mode 120000 index 00000000..ab9334b0 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/gradlew @@ -0,0 +1 @@ +../../../gradlew \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts b/normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts new file mode 100644 index 00000000..ffb34c0b --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/settings.gradle.kts @@ -0,0 +1,3 @@ +apply(from = "gradle/repositories.gradle.kts") + +includeBuild("../../..") \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/operations.graphql b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/operations.graphql new file mode 100644 index 00000000..6b999b48 --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/operations.graphql @@ -0,0 +1,5 @@ +query GetProduct { + product { + price + } +} \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls new file mode 100644 index 00000000..7c1bb33f --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/graphql/schema.graphqls @@ -0,0 +1,9 @@ +type Query { + product: Product + foo: Int +} + +type Product @typePolicy(keyFields: "id"){ + id: ID! + price: Float +} \ No newline at end of file diff --git a/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/kotlin/hello.kt b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/kotlin/hello.kt new file mode 100644 index 00000000..5293dabf --- /dev/null +++ b/normalized-cache-apollo-compiler-plugin/test-data/testProject/src/main/kotlin/hello.kt @@ -0,0 +1 @@ +val hello = "world" \ No newline at end of file From 2d88d4956c68b49ffade228bd4369c3d1f1ee423 Mon Sep 17 00:00:00 2001 From: Martin Bonnin Date: Thu, 5 Jun 2025 15:04:57 +0200 Subject: [PATCH 08/10] Use apollo kotlin 4.3.0 --- gradle/libs.versions.toml | 2 +- gradle/repositories.gradle.kts | 2 -- .../cache/apollocompilerplugin/internal/ApolloVersionTest.kt | 1 - samples/pagination/manual/settings.gradle.kts | 2 -- .../pagination-support-with-jetpack-paging/settings.gradle.kts | 2 -- samples/pagination/pagination-support/settings.gradle.kts | 2 -- 6 files changed, 1 insertion(+), 10 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 73fda4c6..c1eb2e31 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] kotlin-plugin = "2.1.21" android-plugin = "8.7.0" -apollo = "4.3.0-alpha.plugins" +apollo = "4.3.0" 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.1.0" diff --git a/gradle/repositories.gradle.kts b/gradle/repositories.gradle.kts index 4593e532..d037218d 100644 --- a/gradle/repositories.gradle.kts +++ b/gradle/repositories.gradle.kts @@ -1,8 +1,6 @@ listOf(pluginManagement.repositories, dependencyResolutionManagement.repositories).forEach { it.apply { - maven("https://storage.googleapis.com/apollo-previews/m2/") // maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") -// maven("https://storage.googleapis.com/apollo-previews/m2/") mavenCentral() google() gradlePluginPortal() diff --git a/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt index 938bd20a..55ea0533 100644 --- a/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt +++ b/normalized-cache-apollo-compiler-plugin/src/test/kotlin/com/apollographql/cache/apollocompilerplugin/internal/ApolloVersionTest.kt @@ -4,7 +4,6 @@ import org.gradle.testkit.runner.GradleRunner import org.gradle.testkit.runner.UnexpectedBuildFailure import java.io.File import kotlin.test.Test -import kotlin.test.assertFailsWith import kotlin.test.assertTrue class ApolloVersionTest { diff --git a/samples/pagination/manual/settings.gradle.kts b/samples/pagination/manual/settings.gradle.kts index 5bc3c95a..d81c4214 100644 --- a/samples/pagination/manual/settings.gradle.kts +++ b/samples/pagination/manual/settings.gradle.kts @@ -1,6 +1,5 @@ pluginManagement { repositories { - maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } @@ -11,7 +10,6 @@ pluginManagement { } dependencyResolutionManagement { repositories { - maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } diff --git a/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts b/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts index 5bc3c95a..d81c4214 100644 --- a/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts +++ b/samples/pagination/pagination-support-with-jetpack-paging/settings.gradle.kts @@ -1,6 +1,5 @@ pluginManagement { repositories { - maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } @@ -11,7 +10,6 @@ pluginManagement { } dependencyResolutionManagement { repositories { - maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } diff --git a/samples/pagination/pagination-support/settings.gradle.kts b/samples/pagination/pagination-support/settings.gradle.kts index 5bc3c95a..d81c4214 100644 --- a/samples/pagination/pagination-support/settings.gradle.kts +++ b/samples/pagination/pagination-support/settings.gradle.kts @@ -1,6 +1,5 @@ pluginManagement { repositories { - maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } @@ -11,7 +10,6 @@ pluginManagement { } dependencyResolutionManagement { repositories { - maven("https://storage.googleapis.com/apollo-previews/m2/") maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") } From a266c16e23fd62e8394edeb4af98e2ec120f93dd Mon Sep 17 00:00:00 2001 From: BoD Date: Thu, 5 Jun 2025 15:14:58 +0200 Subject: [PATCH 09/10] Keep mavenLocal() --- gradle/repositories.gradle.kts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gradle/repositories.gradle.kts b/gradle/repositories.gradle.kts index d037218d..ab0f8166 100644 --- a/gradle/repositories.gradle.kts +++ b/gradle/repositories.gradle.kts @@ -1,6 +1,8 @@ listOf(pluginManagement.repositories, dependencyResolutionManagement.repositories).forEach { it.apply { +// mavenLocal() // maven("https://s01.oss.sonatype.org/content/repositories/snapshots/") +// maven("https://storage.googleapis.com/apollo-previews/m2/") mavenCentral() google() gradlePluginPortal() From e6316eb4a2699eac59915d555dc0bd506363cbfb Mon Sep 17 00:00:00 2001 From: BoD Date: Thu, 5 Jun 2025 15:17:55 +0200 Subject: [PATCH 10/10] Add 4.3.0 requirement --- Writerside/topics/welcome.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Writerside/topics/welcome.md b/Writerside/topics/welcome.md index 8fa66af5..9e79fa10 100644 --- a/Writerside/topics/welcome.md +++ b/Writerside/topics/welcome.md @@ -26,6 +26,8 @@ are added here instead. In the future, the main repository Normalized Cache will {style="warning"} +The Normalized Cache requires Apollo Kotlin v4.3.0 or later. + 1. Add the dependencies to your project ```kotlin