Skip to content

Commit 02dbe74

Browse files
martinbonninBoD
andauthored
Better messages and debuggability for ApolloCompilerPlugin (apollographql#5821)
* better error messages * Plugin -> ApolloCompilerPlugin * Better messages and debuggability for ApolloCompilerPlugin * better check, we need apollo-compiler in the classpath always * add a warning if no ApolloCompilerPlugin is found * rename to ApolloCompilerPlugin * Update libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/serviceloader.kt Co-authored-by: Benoit 'BoD' Lubek <[email protected]> --------- Co-authored-by: Benoit 'BoD' Lubek <[email protected]>
1 parent 4193d14 commit 02dbe74

File tree

27 files changed

+62
-37
lines changed

27 files changed

+62
-37
lines changed

libraries/apollo-compiler/api/apollo-compiler.api

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ public final class com/apollographql/apollo3/compiler/ApolloCompilerKt {
2424
public static final fun toInputFiles (Ljava/util/Collection;)Ljava/util/List;
2525
}
2626

27+
public abstract interface class com/apollographql/apollo3/compiler/ApolloCompilerPlugin {
28+
public fun javaOutputTransform ()Lcom/apollographql/apollo3/compiler/Transform;
29+
public fun kotlinOutputTransform ()Lcom/apollographql/apollo3/compiler/Transform;
30+
public fun layout (Lcom/apollographql/apollo3/compiler/CodegenSchema;)Lcom/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout;
31+
public fun operationIds (Ljava/util/List;)Ljava/util/List;
32+
}
33+
2734
public final class com/apollographql/apollo3/compiler/CodegenMetadata {
2835
public static final field Companion Lcom/apollographql/apollo3/compiler/CodegenMetadata$Companion;
2936
public fun <init> (Lcom/apollographql/apollo3/compiler/TargetLanguage;Ljava/util/List;)V
@@ -360,13 +367,6 @@ public final class com/apollographql/apollo3/compiler/PackageNameGenerator$Norma
360367
public fun packageName (Ljava/lang/String;)Ljava/lang/String;
361368
}
362369

363-
public abstract interface class com/apollographql/apollo3/compiler/Plugin {
364-
public fun javaOutputTransform ()Lcom/apollographql/apollo3/compiler/Transform;
365-
public fun kotlinOutputTransform ()Lcom/apollographql/apollo3/compiler/Transform;
366-
public fun layout (Lcom/apollographql/apollo3/compiler/CodegenSchema;)Lcom/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout;
367-
public fun operationIds (Ljava/util/List;)Ljava/util/List;
368-
}
369-
370370
public final class com/apollographql/apollo3/compiler/RuntimeAdapterInitializer : com/apollographql/apollo3/compiler/AdapterInitializer {
371371
public static final field INSTANCE Lcom/apollographql/apollo3/compiler/RuntimeAdapterInitializer;
372372
public final fun serializer ()Lkotlinx/serialization/KSerializer;

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/Plugin.kt renamed to libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ApolloCompilerPlugin.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@ import com.apollographql.apollo3.compiler.ir.IrOperations
1111
import com.apollographql.apollo3.compiler.operationoutput.OperationDescriptor
1212
import com.apollographql.apollo3.compiler.operationoutput.OperationId
1313

14+
@Deprecated(
15+
"Use ApolloCompilerPlugin instead (don't forget to replace the META-INF.service file to `com.apollographql.apollo3.compiler.ApolloCompilerPlugin`",
16+
ReplaceWith("ApolloCompilerPlugin"),
17+
DeprecationLevel.ERROR
18+
)
19+
typealias Plugin = ApolloCompilerPlugin
20+
1421
/**
1522
* Entry point for customizing the behaviour of the Apollo Compiler besides the
1623
* already existing options
1724
*/
18-
interface Plugin {
25+
interface ApolloCompilerPlugin {
1926
/**
2027
* @return the layout or null to use the default layout
2128
* @param codegenSchema the codegenSchema
@@ -75,6 +82,7 @@ interface DocumentTransform {
7582
* [transform] is called after any processing done by the Apollo compiler such as adding `__typename`.
7683
*/
7784
fun transform(schema: Schema, operation: GQLOperationDefinition): GQLOperationDefinition
85+
7886
/**
7987
* Transforms the given fragment.
8088
*

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/OperationOutputGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import com.apollographql.apollo3.compiler.operationoutput.OperationOutput
1212
* If you don't need batch compute, use [OperationOutputGenerator.Default]
1313
*/
1414
@Suppress("DEPRECATION")
15-
@Deprecated("Use Apollo compiler plugins instead")
15+
@Deprecated("Use ApolloCompilerPlugin.operationIds() instead. See https://go.apollo.dev/ak-compiler-plugins for more details.")
1616
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
1717
interface OperationOutputGenerator {
1818
/**
@@ -31,7 +31,7 @@ interface OperationOutputGenerator {
3131
* When using the compiler outside a Gradle context, [version] is not used, making it the empty string is fine.
3232
*/
3333
val version: String
34-
get() = error("Use Apollo compiler plugins instead of passing operationOutputGenerator from your Gradle classpath")
34+
get() = error("OperationOutputGenerator implementations must override `version`.")
3535

3636
class Default(private val operationIdGenerator: OperationIdGenerator) : OperationOutputGenerator {
3737
override fun generate(operationDescriptorList: Collection<OperationDescriptor>): OperationOutput {

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/PackageNameGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import java.io.File
99
* - schema files containing type definitions or introspection json
1010
*/
1111
@Suppress("DEPRECATION")
12-
@Deprecated("Use Apollo compiler plugins instead")
12+
@Deprecated("Use ApolloCompilerPlugin.layout() instead. See https://go.apollo.dev/ak-compiler-plugins for more details.")
1313
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
1414
interface PackageNameGenerator {
1515
/**
@@ -29,7 +29,7 @@ interface PackageNameGenerator {
2929
* When using the compiler outside a Gradle context, [version] is not used, making it the empty string is fine.
3030
*/
3131
val version: String
32-
get() = error("Use Apollo compiler plugins instead of passing packageNameGenerator from your Gradle classpath")
32+
get() = error("PackageNameGenerator implementations must override `version`.")
3333

3434
class Flat(private val packageName: String): PackageNameGenerator {
3535
override fun packageName(filePath: String): String {

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/api/Service.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ interface Service {
271271
* Default value: [OperationIdGenerator.Sha256]
272272
*/
273273
@Suppress("DEPRECATION")
274-
@Deprecated("Use Apollo compiler plugins instead")
274+
@Deprecated("Use ApolloCompilerPlugin.operationIds() instead. See https://go.apollo.dev/ak-compiler-plugins for more details.")
275275
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
276276
val operationOutputGenerator: Property<OperationOutputGenerator>
277277

@@ -301,7 +301,7 @@ interface Service {
301301
*
302302
* @see [packageName]
303303
*/
304-
@Deprecated("Use Apollo compiler plugins instead")
304+
@Deprecated("Use ApolloCompilerPlugin.layout() instead. See https://go.apollo.dev/ak-compiler-plugins for more details.")
305305
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
306306
val packageNameGenerator: Property<PackageNameGenerator>
307307

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/ApolloGenerateSourcesBaseTask.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.gradle.api.DefaultTask
1313
import org.gradle.api.file.ConfigurableFileCollection
1414
import org.gradle.api.file.DirectoryProperty
1515
import org.gradle.api.file.RegularFileProperty
16+
import org.gradle.api.provider.Property
1617
import org.gradle.api.tasks.Classpath
1718
import org.gradle.api.tasks.Input
1819
import org.gradle.api.tasks.InputFile
@@ -52,6 +53,9 @@ abstract class ApolloGenerateSourcesBaseTask : DefaultTask() {
5253
@get:Classpath
5354
abstract val classpath: ConfigurableFileCollection
5455

56+
@get:Input
57+
abstract val hasPlugin: Property<Boolean>
58+
5559
@Inject
5660
abstract fun getWorkerExecutor(): WorkerExecutor
5761
}
@@ -77,6 +81,9 @@ fun ApolloGenerateSourcesBaseTask.requiresBuildscriptClasspath(): Boolean {
7781
if (operationOutputGenerator != null) {
7882
logger.lifecycle("Apollo: operationOutputGenerator is deprecated, use Apollo compiler plugins instead. See https://go.apollo.dev/ak-compiler-plugins for more details.")
7983
}
84+
check(!hasPlugin.get()) {
85+
"Apollo: using ApolloCompilerPlugin and operationOutputGenerator/operationIdGenerator/packageNameGenerator at the same time is not supported. See https://go.apollo.dev/ak-compiler-plugins for more details."
86+
}
8087

8188
return true
8289
}

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/ApolloGenerateSourcesFromIrTask.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ abstract class ApolloGenerateSourcesFromIrTask : ApolloGenerateSourcesBaseTask()
7878
it.operationManifestFile.set(operationManifestFile)
7979
it.outputDir.set(outputDir)
8080
it.metadataOutputFile.set(metadataOutputFile)
81+
it.hasPlugin = hasPlugin.get()
8182
}
8283
}
8384
}
@@ -97,7 +98,7 @@ private abstract class GenerateSourcesFromIr : WorkAction<GenerateSourcesFromIrP
9798
val codegenSchemaFile = codegenSchemas.findCodegenSchemaFile()
9899

99100
val codegenSchema = codegenSchemaFile.toCodegenSchema()
100-
val plugin = apolloCompilerPlugin()
101+
val plugin = apolloCompilerPlugin(hasPlugin)
101102

102103
ApolloCompiler.buildSchemaAndOperationsSourcesFromIr(
103104
codegenSchema = codegenSchema,
@@ -117,6 +118,7 @@ private abstract class GenerateSourcesFromIr : WorkAction<GenerateSourcesFromIrP
117118
}
118119

119120
private interface GenerateSourcesFromIrParameters : WorkParameters {
121+
var hasPlugin: Boolean
120122
val codegenSchemas: ConfigurableFileCollection
121123
val irOperations: RegularFileProperty
122124
val codegenOptions: RegularFileProperty

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/ApolloGenerateSourcesTask.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ abstract class ApolloGenerateSourcesTask : ApolloGenerateSourcesBaseTask() {
6969
}
7070

7171
workQueue.submit(GenerateSources::class.java) {
72+
it.hasPlugin = hasPlugin.get()
7273
it.graphqlFiles = graphqlFiles.isolate()
7374
it.schemaFiles = schemaFiles.isolate()
7475
it.fallbackSchemaFiles = fallbackSchemaFiles.isolate()
@@ -87,7 +88,7 @@ private abstract class GenerateSources : WorkAction<GenerateSourcesParameters> {
8788
with(parameters) {
8889
val schemaInputFiles = (schemaFiles.takeIf { it.isNotEmpty() } ?: fallbackSchemaFiles).toInputFiles()
8990
val executableInputFiles = graphqlFiles.toInputFiles()
90-
val plugin = apolloCompilerPlugin()
91+
val plugin = apolloCompilerPlugin(hasPlugin)
9192

9293
ApolloCompiler.buildSchemaAndOperationsSources(
9394
schemaFiles = schemaInputFiles,
@@ -113,6 +114,7 @@ private abstract class GenerateSources : WorkAction<GenerateSourcesParameters> {
113114
}
114115

115116
private interface GenerateSourcesParameters : WorkParameters {
117+
var hasPlugin: Boolean
116118
var graphqlFiles: List<Pair<String, File>>
117119
var schemaFiles: List<Pair<String, File>>
118120
var fallbackSchemaFiles: List<Pair<String, File>>

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/DefaultApolloExtension.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,7 @@ abstract class DefaultApolloExtension(
880880
task.operationOutputGenerator = service.operationOutputGenerator.orElse(service.operationIdGenerator.map { OperationOutputGenerator.Default(it) }).orNull
881881
service.operationOutputGenerator.disallowChanges()
882882

883+
task.hasPlugin.set(service.pluginDependencies.isNotEmpty())
883884
task.classpath.from(classpath)
884885

885886
task.outputDir.set(service.outputDir.orElse(BuildDirLayout.outputDir(project, service)))

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo3/gradle/internal/defaults.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import com.apollographql.apollo3.compiler.MANIFEST_OPERATION_OUTPUT
77
import com.apollographql.apollo3.compiler.MANIFEST_PERSISTED_QUERY
88
import com.apollographql.apollo3.compiler.OperationIdGenerator
99
import com.apollographql.apollo3.compiler.OperationOutputGenerator
10-
import com.apollographql.apollo3.compiler.Plugin
10+
import com.apollographql.apollo3.compiler.ApolloCompilerPlugin
1111
import com.apollographql.apollo3.compiler.TargetLanguage
1212
import com.apollographql.apollo3.compiler.operationoutput.OperationDescriptor
1313
import com.apollographql.apollo3.compiler.operationoutput.OperationId
@@ -88,7 +88,7 @@ internal fun DefaultService.operationManifestFormat(): Provider<String> {
8888
}
8989
}
9090

91-
internal fun Plugin.toOperationOutputGenerator(): OperationOutputGenerator {
91+
internal fun ApolloCompilerPlugin.toOperationOutputGenerator(): OperationOutputGenerator {
9292
return object : OperationOutputGenerator {
9393
override fun generate(operationDescriptorList: Collection<OperationDescriptor>): OperationOutput {
9494
var operationIds = operationIds(operationDescriptorList.toList())

0 commit comments

Comments
 (0)