Skip to content

Commit cd89b82

Browse files
authored
Allow to register multiple Apollo Compiler plugins (#6523)
* Allow to register several compiler plugins * fix tests * forgot to add file * code review
1 parent 8b7fba6 commit cd89b82

File tree

37 files changed

+797
-609
lines changed

37 files changed

+797
-609
lines changed

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

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@ public final class com/apollographql/apollo/compiler/AdapterInitializer$Companio
66
public final fun serializer ()Lkotlinx/serialization/KSerializer;
77
}
88

9+
public final class com/apollographql/apollo/compiler/After : com/apollographql/apollo/compiler/Order {
10+
public fun <init> (Ljava/lang/String;)V
11+
public final fun getId ()Ljava/lang/String;
12+
}
13+
914
public final class com/apollographql/apollo/compiler/ApolloCompiler {
1015
public static final field INSTANCE Lcom/apollographql/apollo/compiler/ApolloCompiler;
1116
public final fun buildCodegenSchema (Ljava/util/List;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Lcom/apollographql/apollo/compiler/CodegenSchemaOptions;Ljava/util/List;Lcom/apollographql/apollo/compiler/SchemaTransform;)Lcom/apollographql/apollo/compiler/CodegenSchema;
1217
public final fun buildDataBuilders (Lcom/apollographql/apollo/compiler/CodegenSchema;Lcom/apollographql/apollo/compiler/UsedCoordinates;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/codegen/SchemaLayout;Ljava/util/List;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
13-
public final fun buildIrOperations (Lcom/apollographql/apollo/compiler/CodegenSchema;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/DocumentTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;)Lcom/apollographql/apollo/compiler/ir/IrOperations;
14-
public final fun buildSchemaAndOperationsSources (Lcom/apollographql/apollo/compiler/CodegenSchema;Ljava/util/List;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/LayoutFactory;Lcom/apollographql/apollo/compiler/OperationIdsGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/DocumentTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
15-
public final fun buildSchemaAndOperationsSources (Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo/compiler/CodegenSchemaOptions;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/LayoutFactory;Lcom/apollographql/apollo/compiler/OperationIdsGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/DocumentTransform;Lcom/apollographql/apollo/compiler/SchemaTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
18+
public final fun buildIrOperations (Lcom/apollographql/apollo/compiler/CodegenSchema;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/OperationsTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;)Lcom/apollographql/apollo/compiler/ir/IrOperations;
19+
public final fun buildSchemaAndOperationsSources (Lcom/apollographql/apollo/compiler/CodegenSchema;Ljava/util/List;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/LayoutFactory;Lcom/apollographql/apollo/compiler/OperationIdsGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/OperationsTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
20+
public final fun buildSchemaAndOperationsSources (Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo/compiler/CodegenSchemaOptions;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/LayoutFactory;Lcom/apollographql/apollo/compiler/OperationIdsGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/OperationsTransform;Lcom/apollographql/apollo/compiler/SchemaTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
1621
public final fun buildSchemaAndOperationsSourcesFromIr (Lcom/apollographql/apollo/compiler/CodegenSchema;Lcom/apollographql/apollo/compiler/ir/IrOperations;Lcom/apollographql/apollo/compiler/UsedCoordinates;Ljava/util/List;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout;Lcom/apollographql/apollo/compiler/OperationIdsGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
1722
public final fun buildSchemaSources (Lcom/apollographql/apollo/compiler/CodegenSchema;Lcom/apollographql/apollo/compiler/UsedCoordinates;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/codegen/SchemaLayout;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
1823
}
@@ -26,21 +31,15 @@ public final class com/apollographql/apollo/compiler/ApolloCompilerKt {
2631
}
2732

2833
public abstract interface class com/apollographql/apollo/compiler/ApolloCompilerPlugin {
29-
public fun documentTransform ()Lcom/apollographql/apollo/compiler/DocumentTransform;
30-
public fun foreignSchemas ()Ljava/util/List;
31-
public fun irOperationsTransform ()Lcom/apollographql/apollo/compiler/Transform;
32-
public fun javaOutputTransform ()Lcom/apollographql/apollo/compiler/Transform;
33-
public fun kotlinOutputTransform ()Lcom/apollographql/apollo/compiler/Transform;
34-
public fun layout (Lcom/apollographql/apollo/compiler/CodegenSchema;)Lcom/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout;
34+
public abstract fun beforeCompilationStep (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;Lcom/apollographql/apollo/compiler/ApolloCompilerRegistry;)V
3535
public fun operationIds (Ljava/util/List;)Ljava/util/List;
36-
public fun schemaListener ()Lcom/apollographql/apollo/compiler/SchemaListener;
37-
public fun schemaTransform ()Lcom/apollographql/apollo/compiler/SchemaTransform;
3836
}
3937

4038
public final class com/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment {
41-
public fun <init> (Ljava/util/Map;Lcom/apollographql/apollo/compiler/ApolloCompilerPluginLogger;)V
39+
public fun <init> (Ljava/util/Map;Lcom/apollographql/apollo/compiler/ApolloCompilerPluginLogger;Ljava/io/File;)V
4240
public final fun getArguments ()Ljava/util/Map;
4341
public final fun getLogger ()Lcom/apollographql/apollo/compiler/ApolloCompilerPluginLogger;
42+
public final fun getOutputDirectory ()Ljava/io/File;
4443
}
4544

4645
public abstract interface class com/apollographql/apollo/compiler/ApolloCompilerPluginLogger {
@@ -54,6 +53,27 @@ public abstract interface class com/apollographql/apollo/compiler/ApolloCompiler
5453
public abstract fun create (Lcom/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment;)Lcom/apollographql/apollo/compiler/ApolloCompilerPlugin;
5554
}
5655

56+
public abstract interface class com/apollographql/apollo/compiler/ApolloCompilerRegistry {
57+
public abstract fun registerExtraCodeGenerator (Lcom/apollographql/apollo/compiler/CodeGenerator;)V
58+
public abstract fun registerForeignSchemas (Ljava/util/List;)V
59+
public abstract fun registerIrTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/Transform;)V
60+
public abstract fun registerJavaOutputTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/Transform;)V
61+
public abstract fun registerKotlinOutputTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/Transform;)V
62+
public abstract fun registerLayout (Lcom/apollographql/apollo/compiler/LayoutFactory;)V
63+
public abstract fun registerOperationIdsGenerator (Lcom/apollographql/apollo/compiler/OperationIdsGenerator;)V
64+
public abstract fun registerOperationsTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/OperationsTransform;)V
65+
public abstract fun registerSchemaTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/SchemaTransform;)V
66+
}
67+
68+
public final class com/apollographql/apollo/compiler/Before : com/apollographql/apollo/compiler/Order {
69+
public fun <init> (Ljava/lang/String;)V
70+
public final fun getId ()Ljava/lang/String;
71+
}
72+
73+
public abstract interface class com/apollographql/apollo/compiler/CodeGenerator {
74+
public abstract fun generate (Lcom/apollographql/apollo/ast/GQLDocument;)V
75+
}
76+
5777
public final class com/apollographql/apollo/compiler/CodegenMetadata {
5878
public static final field Companion Lcom/apollographql/apollo/compiler/CodegenMetadata$Companion;
5979
public fun <init> (Ljava/lang/String;Lcom/apollographql/apollo/compiler/TargetLanguage;Ljava/util/List;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V
@@ -182,10 +202,6 @@ public abstract interface class com/apollographql/apollo/compiler/CommonCodegenO
182202
public abstract fun getUseSemanticNaming ()Ljava/lang/Boolean;
183203
}
184204

185-
public abstract interface class com/apollographql/apollo/compiler/DocumentTransform {
186-
public abstract fun transform (Lcom/apollographql/apollo/ast/Schema;Lcom/apollographql/apollo/ast/GQLDocument;Ljava/util/List;)Lcom/apollographql/apollo/ast/GQLDocument;
187-
}
188-
189205
public final class com/apollographql/apollo/compiler/EntryPoints {
190206
public fun <init> ()V
191207
public final fun buildCodegenSchema (Ljava/util/Map;IZLjava/util/List;Ljava/util/function/Consumer;Ljava/io/File;Ljava/io/File;)V
@@ -356,6 +372,10 @@ public abstract interface class com/apollographql/apollo/compiler/OperationsCode
356372
public abstract interface class com/apollographql/apollo/compiler/OperationsCodegenOptions : com/apollographql/apollo/compiler/JavaOperationsCodegenOptions, com/apollographql/apollo/compiler/KotlinOperationsCodegenOptions {
357373
}
358374

375+
public abstract interface class com/apollographql/apollo/compiler/OperationsTransform {
376+
public abstract fun transform (Lcom/apollographql/apollo/ast/Schema;Lcom/apollographql/apollo/ast/GQLDocument;Ljava/util/List;)Lcom/apollographql/apollo/ast/GQLDocument;
377+
}
378+
359379
public final class com/apollographql/apollo/compiler/OptionsKt {
360380
public static final field ADD_TYPENAME_ALWAYS Ljava/lang/String;
361381
public static final field ADD_TYPENAME_IF_ABSTRACT Ljava/lang/String;
@@ -374,6 +394,9 @@ public final class com/apollographql/apollo/compiler/OptionsKt {
374394
public static final fun validate (Lcom/apollographql/apollo/compiler/CodegenOptions;)V
375395
}
376396

397+
public abstract interface class com/apollographql/apollo/compiler/Order {
398+
}
399+
377400
public final class com/apollographql/apollo/compiler/RuntimeAdapterInitializer : com/apollographql/apollo/compiler/AdapterInitializer {
378401
public static final field INSTANCE Lcom/apollographql/apollo/compiler/RuntimeAdapterInitializer;
379402
public final fun serializer ()Lkotlinx/serialization/KSerializer;

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import com.apollographql.apollo.compiler.codegen.kotlin.KotlinCodegen
3434
import com.apollographql.apollo.compiler.codegen.kotlin.KotlinOutput
3535
import com.apollographql.apollo.compiler.codegen.kotlin.toSourceOutput
3636
import com.apollographql.apollo.compiler.codegen.plus
37-
import com.apollographql.apollo.compiler.internal.ApolloDocumentTransform
37+
import com.apollographql.apollo.compiler.internal.ApolloOperationsTransform
3838
import com.apollographql.apollo.compiler.internal.checkApolloInlineFragmentsHaveTypeCondition
3939
import com.apollographql.apollo.compiler.internal.checkApolloReservedEnumValueNames
4040
import com.apollographql.apollo.compiler.internal.checkApolloTargetNameClashes
@@ -190,7 +190,7 @@ object ApolloCompiler {
190190
upstreamCodegenModels: List<String>,
191191
upstreamFragmentDefinitions: List<GQLFragmentDefinition>,
192192
options: IrOptions,
193-
documentTransform: DocumentTransform?,
193+
operationsTransform: OperationsTransform?,
194194
logger: Logger?,
195195
): IrOperations {
196196
val schema = codegenSchema.schema
@@ -233,13 +233,13 @@ object ApolloCompiler {
233233
/**
234234
* Step 2, Modify the AST to add typename, key fields and call any user-provided transform.
235235
*/
236-
var document = ApolloDocumentTransform(options.addTypename ?: defaultAddTypename).transform(
236+
var document = ApolloOperationsTransform(options.addTypename ?: defaultAddTypename).transform(
237237
schema = schema,
238238
document = GQLDocument(userDefinitions, sourceLocation = null),
239239
upstreamFragmentDefinitions
240240
)
241-
if (documentTransform != null) {
242-
document = documentTransform.transform(schema, document, upstreamFragmentDefinitions)
241+
if (operationsTransform != null) {
242+
document = operationsTransform.transform(schema, document, upstreamFragmentDefinitions)
243243
}
244244

245245
/**
@@ -475,7 +475,7 @@ object ApolloCompiler {
475475
irOperationsTransform: Transform<IrOperations>?,
476476
javaOutputTransform: Transform<JavaOutput>?,
477477
kotlinOutputTransform: Transform<KotlinOutput>?,
478-
documentTransform: DocumentTransform?,
478+
operationsTransform: OperationsTransform?,
479479
schemaTransform: SchemaTransform?,
480480
logger: Logger?,
481481
operationManifestFile: File?,
@@ -498,7 +498,7 @@ object ApolloCompiler {
498498
irOperationsTransform,
499499
javaOutputTransform,
500500
kotlinOutputTransform,
501-
documentTransform,
501+
operationsTransform,
502502
logger,
503503
operationManifestFile
504504
)
@@ -517,7 +517,7 @@ object ApolloCompiler {
517517
irOperationsTransform: Transform<IrOperations>?,
518518
javaOutputTransform: Transform<JavaOutput>?,
519519
kotlinOutputTransform: Transform<KotlinOutput>?,
520-
documentTransform: DocumentTransform?,
520+
operationsTransform: OperationsTransform?,
521521
logger: Logger?,
522522
operationManifestFile: File?,
523523
): SourceOutput {
@@ -526,7 +526,7 @@ object ApolloCompiler {
526526
executableFiles = executableFiles,
527527
upstreamCodegenModels = emptyList(),
528528
upstreamFragmentDefinitions = emptyList(),
529-
documentTransform = documentTransform,
529+
operationsTransform = operationsTransform,
530530
options = irOptions,
531531
logger = logger
532532
)
@@ -638,7 +638,7 @@ fun Collection<File>.toInputFiles(): List<InputFile> = map { InputFile(it, "") }
638638

639639
internal fun <T> T.maybeTransform(transform: Transform<T>?) = transform?.transform(this) ?: this
640640

641-
interface LayoutFactory {
641+
fun interface LayoutFactory {
642642
fun create(codegenSchema: CodegenSchema): SchemaAndOperationsLayout?
643643
}
644644

0 commit comments

Comments
 (0)