Skip to content
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

[🐘gradle-plugin] use Worker API and ServiceLoader #5590

Merged
merged 9 commits into from
Feb 2, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions gradle/libraries.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ apollo-normalizedcache-sqlite-incubating = { group = "com.apollographql.apollo3"
apollo-rx2-support-java = { group = "com.apollographql.apollo3", name = "apollo-rx2-support-java", version.ref = "apollo" }
apollo-plugin = { group = "com.apollographql.apollo3", name = "apollo-gradle-plugin", version.ref = "apollo" }
apollo-runtime = { group = "com.apollographql.apollo3", name = "apollo-runtime", version.ref = "apollo" }
apollo-compiler = { group = "com.apollographql.apollo3", name = "apollo-compiler", version.ref = "apollo" }
apollo-execution = { group = "com.apollographql.apollo3", name = "apollo-execution-incubating", version.ref = "apollo" }
# Used by the apollo-tooling project which uses a published version of Apollo
apollo-runtime-published = { group = "com.apollographql.apollo3", name = "apollo-runtime", version.ref = "apollo-published" }
Expand Down
45 changes: 42 additions & 3 deletions libraries/apollo-compiler/api/apollo-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public final class com/apollographql/apollo3/compiler/ApolloCompiler {
public final fun buildCodegenSchema (Ljava/util/List;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Lcom/apollographql/apollo3/compiler/CodegenSchemaOptions;)Lcom/apollographql/apollo3/compiler/CodegenSchema;
public final fun buildExecutableSchemaSources (Lcom/apollographql/apollo3/compiler/CodegenSchema;Lcom/apollographql/apollo3/compiler/CodegenMetadata;Ljava/util/List;Ljava/lang/String;Ljava/lang/String;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
public final fun buildIrOperations (Lcom/apollographql/apollo3/compiler/CodegenSchema;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/IrOptions;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;)Lcom/apollographql/apollo3/compiler/ir/IrOperations;
public final fun buildSchemaAndOperationsSources (Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/CodegenSchemaOptions;Lcom/apollographql/apollo3/compiler/IrOptions;Lcom/apollographql/apollo3/compiler/CodegenOptions;Lkotlin/jvm/functions/Function1;Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
public final fun buildSchemaAndOperationsSourcesFromIr (Lcom/apollographql/apollo3/compiler/CodegenSchema;Lcom/apollographql/apollo3/compiler/ir/IrOperations;Ljava/util/Map;Ljava/util/List;Lcom/apollographql/apollo3/compiler/CodegenOptions;Lcom/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout;Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;Ljava/util/List;Ljava/util/List;Ljava/io/File;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
public final fun buildSchemaSources (Lcom/apollographql/apollo3/compiler/CodegenSchema;Ljava/util/Map;Lcom/apollographql/apollo3/compiler/CodegenOptions;Lcom/apollographql/apollo3/compiler/codegen/SchemaLayout;Ljava/util/List;Ljava/util/List;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
public final fun buildSchemaAndOperationsSources (Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/CodegenSchemaOptions;Lcom/apollographql/apollo3/compiler/IrOptions;Lcom/apollographql/apollo3/compiler/CodegenOptions;Lcom/apollographql/apollo3/compiler/LayoutFactory;Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/Transform;Lcom/apollographql/apollo3/compiler/Transform;Lcom/apollographql/apollo3/compiler/Transform;Lcom/apollographql/apollo3/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
public final fun buildSchemaAndOperationsSourcesFromIr (Lcom/apollographql/apollo3/compiler/CodegenSchema;Lcom/apollographql/apollo3/compiler/ir/IrOperations;Ljava/util/Map;Ljava/util/List;Lcom/apollographql/apollo3/compiler/CodegenOptions;Lcom/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout;Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/Transform;Lcom/apollographql/apollo3/compiler/Transform;Lcom/apollographql/apollo3/compiler/Transform;Ljava/io/File;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
public final fun buildSchemaSources (Lcom/apollographql/apollo3/compiler/CodegenSchema;Ljava/util/Map;Lcom/apollographql/apollo3/compiler/CodegenOptions;Lcom/apollographql/apollo3/compiler/codegen/SchemaLayout;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo3/compiler/Transform;Lcom/apollographql/apollo3/compiler/Transform;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
}

public abstract interface class com/apollographql/apollo3/compiler/ApolloCompiler$Logger {
Expand Down Expand Up @@ -279,6 +279,10 @@ public abstract interface class com/apollographql/apollo3/compiler/KotlinOperati
public abstract interface class com/apollographql/apollo3/compiler/KotlinSchemaCodegenOptions : com/apollographql/apollo3/compiler/CommonCodegenOpt, com/apollographql/apollo3/compiler/KotlinCodegenOpt, com/apollographql/apollo3/compiler/SchemaCodegenOpt {
}

public abstract interface class com/apollographql/apollo3/compiler/LayoutFactory {
public abstract fun create (Lcom/apollographql/apollo3/compiler/CodegenSchema;)Lcom/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout;
}

public abstract interface class com/apollographql/apollo3/compiler/OperationIdGenerator {
public static final field Companion Lcom/apollographql/apollo3/compiler/OperationIdGenerator$Companion;
public abstract fun apply (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
Expand Down Expand Up @@ -352,6 +356,14 @@ public final class com/apollographql/apollo3/compiler/PackageNameGenerator$Norma
public fun packageName (Ljava/lang/String;)Ljava/lang/String;
}

public abstract interface class com/apollographql/apollo3/compiler/Plugin {
public fun irOperationsTransform ()Lcom/apollographql/apollo3/compiler/Transform;
public fun javaOutputTransform ()Lcom/apollographql/apollo3/compiler/Transform;
public fun kotlinOutputTransform ()Lcom/apollographql/apollo3/compiler/Transform;
public fun layout (Lcom/apollographql/apollo3/compiler/CodegenSchema;)Lcom/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout;
public fun operationOutputGenerator ()Lcom/apollographql/apollo3/compiler/OperationOutputGenerator;
}

public final class com/apollographql/apollo3/compiler/RuntimeAdapterInitializer : com/apollographql/apollo3/compiler/AdapterInitializer {
public static final field INSTANCE Lcom/apollographql/apollo3/compiler/RuntimeAdapterInitializer;
public final fun serializer ()Lkotlinx/serialization/KSerializer;
Expand Down Expand Up @@ -403,6 +415,10 @@ public final class com/apollographql/apollo3/compiler/TargetLanguage : java/lang
public static fun values ()[Lcom/apollographql/apollo3/compiler/TargetLanguage;
}

public abstract interface class com/apollographql/apollo3/compiler/Transform {
public abstract fun transform (Ljava/lang/Object;)Ljava/lang/Object;
}

public final class com/apollographql/apollo3/compiler/UsedCoordinatesKt {
public static final fun mergeWith (Ljava/util/Map;Ljava/util/Map;)Ljava/util/Map;
}
Expand All @@ -420,6 +436,7 @@ public abstract interface class com/apollographql/apollo3/compiler/codegen/Execu
}

public final class com/apollographql/apollo3/compiler/codegen/LayoutImplKt {
public static final fun SchemaAndOperationsLayout (Lcom/apollographql/apollo3/compiler/CodegenSchema;Lcom/apollographql/apollo3/compiler/PackageNameGenerator;ZZ)Lcom/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout;
}

public abstract interface class com/apollographql/apollo3/compiler/codegen/OperationsLayout : com/apollographql/apollo3/compiler/codegen/CommonLayout {
Expand All @@ -443,6 +460,28 @@ public final class com/apollographql/apollo3/compiler/codegen/ResolverClassName$
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class com/apollographql/apollo3/compiler/codegen/ResolverEntry {
public static final field Companion Lcom/apollographql/apollo3/compiler/codegen/ResolverEntry$Companion;
public fun <init> (Lcom/apollographql/apollo3/compiler/codegen/ResolverKey;Lcom/apollographql/apollo3/compiler/codegen/ResolverClassName;)V
public final fun getClassName ()Lcom/apollographql/apollo3/compiler/codegen/ResolverClassName;
public final fun getKey ()Lcom/apollographql/apollo3/compiler/codegen/ResolverKey;
}

public final class com/apollographql/apollo3/compiler/codegen/ResolverEntry$$serializer : kotlinx/serialization/internal/GeneratedSerializer {
public static final field INSTANCE Lcom/apollographql/apollo3/compiler/codegen/ResolverEntry$$serializer;
public fun childSerializers ()[Lkotlinx/serialization/KSerializer;
public fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Lcom/apollographql/apollo3/compiler/codegen/ResolverEntry;
public synthetic fun deserialize (Lkotlinx/serialization/encoding/Decoder;)Ljava/lang/Object;
public fun getDescriptor ()Lkotlinx/serialization/descriptors/SerialDescriptor;
public fun serialize (Lkotlinx/serialization/encoding/Encoder;Lcom/apollographql/apollo3/compiler/codegen/ResolverEntry;)V
public synthetic fun serialize (Lkotlinx/serialization/encoding/Encoder;Ljava/lang/Object;)V
public fun typeParametersSerializers ()[Lkotlinx/serialization/KSerializer;
}

public final class com/apollographql/apollo3/compiler/codegen/ResolverEntry$Companion {
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class com/apollographql/apollo3/compiler/codegen/ResolverKey {
public static final field Companion Lcom/apollographql/apollo3/compiler/codegen/ResolverKey$Companion;
public fun <init> (Lcom/apollographql/apollo3/compiler/codegen/ResolverKeyKind;Ljava/lang/String;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import com.apollographql.apollo3.compiler.codegen.SchemaAndOperationsLayout
import com.apollographql.apollo3.compiler.codegen.SchemaLayout
import com.apollographql.apollo3.compiler.codegen.SourceOutput
import com.apollographql.apollo3.compiler.codegen.java.JavaCodegen
import com.apollographql.apollo3.compiler.codegen.java.JavaOutput
import com.apollographql.apollo3.compiler.codegen.java.toSourceOutput
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinCodegen
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinOutput
import com.apollographql.apollo3.compiler.codegen.kotlin.toSourceOutput
import com.apollographql.apollo3.compiler.codegen.plus
import com.apollographql.apollo3.compiler.hooks.ApolloCompilerJavaHooks
Expand Down Expand Up @@ -323,6 +325,8 @@ object ApolloCompiler {
schemaLayout: SchemaLayout?,
compilerKotlinHooks: List<ApolloCompilerKotlinHooks>?,
compilerJavaHooks: List<ApolloCompilerJavaHooks>?,
javaOutputTransform: Transform<JavaOutput>?,
kotlinOutputTransform: Transform<KotlinOutput>?,
): SourceOutput {
val irSchema = buildIrSchema(codegenSchema, usedCoordinates)

Expand All @@ -343,6 +347,7 @@ object ApolloCompiler {
codegenOptions = codegenOptions,
layout = layout,
compilerJavaHooks = compilerJavaHooks ?: defaultCompilerJavaHooks,
javaOutputTransform = javaOutputTransform
).toSourceOutput()
} else {
KotlinCodegen.buildSchemaSources(
Expand All @@ -352,6 +357,7 @@ object ApolloCompiler {
codegenOptions = codegenOptions,
layout = layout,
compilerKotlinHooks = compilerKotlinHooks ?: defaultCompilerKotlinHooks,
kotlinOutputTransform = kotlinOutputTransform
).toSourceOutput()
}
}
Expand All @@ -366,10 +372,16 @@ object ApolloCompiler {
operationOutputGenerator: OperationOutputGenerator?,
compilerKotlinHooks: List<ApolloCompilerKotlinHooks>?,
compilerJavaHooks: List<ApolloCompilerJavaHooks>?,
irOperationsTransform: Transform<IrOperations>?,
javaOutputTransform: Transform<JavaOutput>?,
kotlinOutputTransform: Transform<KotlinOutput>?,
operationManifestFile: File?,
): SourceOutput {
check(irOperations is DefaultIrOperations)

@Suppress("NAME_SHADOWING")
val irOperations = irOperations.maybeTransform(irOperationsTransform) as DefaultIrOperations

val targetLanguage = defaultTargetLanguage(codegenOptions.targetLanguage, upstreamCodegenMetadata)
codegenOptions.validate()

Expand Down Expand Up @@ -417,7 +429,9 @@ object ApolloCompiler {
codegenOptions = codegenOptions,
schemaLayout = layout,
compilerKotlinHooks = compilerKotlinHooks,
compilerJavaHooks = compilerJavaHooks
compilerJavaHooks = compilerJavaHooks,
javaOutputTransform = javaOutputTransform,
kotlinOutputTransform = kotlinOutputTransform,
)
}
if (targetLanguage == TargetLanguage.JAVA) {
Expand All @@ -429,6 +443,7 @@ object ApolloCompiler {
codegenOptions = codegenOptions,
layout = layout,
compilerJavaHooks = compilerJavaHooks,
javaOutputTransform = javaOutputTransform,
).toSourceOutput()
} else {
sourceOutput = sourceOutput plus KotlinCodegen.buildOperationSources(
Expand All @@ -440,6 +455,7 @@ object ApolloCompiler {
codegenOptions = codegenOptions,
layout = layout,
compilerKotlinHooks = compilerKotlinHooks,
kotlinOutputTransform = kotlinOutputTransform
).toSourceOutput()
}

Expand All @@ -455,10 +471,13 @@ object ApolloCompiler {
codegenSchemaOptions: CodegenSchemaOptions,
irOptions: IrOptions,
codegenOptions: CodegenOptions,
layout: ((CodegenSchema) -> SchemaAndOperationsLayout)?,
layoutFactory: LayoutFactory?,
operationOutputGenerator: OperationOutputGenerator?,
compilerJavaHooks: List<ApolloCompilerJavaHooks>?,
compilerKotlinHooks: List<ApolloCompilerKotlinHooks>?,
irOperationsTransform: Transform<IrOperations>?,
javaOutputTransform: Transform<JavaOutput>?,
kotlinOutputTransform: Transform<KotlinOutput>?,
logger: Logger?,
operationManifestFile: File?,
): SourceOutput {
Expand All @@ -483,9 +502,12 @@ object ApolloCompiler {
downstreamUsedCoordinates = emptyMap(),
upstreamCodegenMetadata = emptyList(),
codegenOptions = codegenOptions,
layout = layout?.invoke(codegenSchema),
layout = layoutFactory?.create(codegenSchema),
compilerJavaHooks = compilerJavaHooks,
compilerKotlinHooks = compilerKotlinHooks,
irOperationsTransform = irOperationsTransform,
javaOutputTransform = javaOutputTransform,
kotlinOutputTransform = kotlinOutputTransform,
operationManifestFile = operationManifestFile,
operationOutputGenerator = operationOutputGenerator,
)
Expand Down Expand Up @@ -568,4 +590,10 @@ internal fun List<Issue>.group(
*/
class InputFile(val file: File, val normalizedPath: String)

fun Collection<File>.toInputFiles(): List<InputFile> = map { InputFile(it, "") }
fun Collection<File>.toInputFiles(): List<InputFile> = map { InputFile(it, "") }

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

interface LayoutFactory {
fun create(codegenSchema: CodegenSchema): SchemaAndOperationsLayout?
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import kotlinx.serialization.Serializable
*/
@Serializable
@ApolloInternal
class CodegenMetadata internal constructor(
class CodegenMetadata(
val targetLanguage: TargetLanguage,
internal val entries: List<ResolverEntry>
val entries: List<ResolverEntry>

) {
operator fun plus(other: CodegenMetadata): CodegenMetadata {
Expand All @@ -33,8 +33,6 @@ class CodegenMetadata internal constructor(
}
}

private val emptyTargetLanguage = "emptyTargetLanguage"

@ApolloInternal
fun CodegenMetadata.resolveSchemaType(name: String): ResolverClassName? {
return entries.firstOrNull { it.key.kind == ResolverKeyKind.SchemaType && it.key.id == name }?.className
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.apollographql.apollo3.compiler

import com.apollographql.apollo3.compiler.codegen.SchemaAndOperationsLayout
import com.apollographql.apollo3.compiler.codegen.java.JavaOutput
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinOutput
import com.apollographql.apollo3.compiler.ir.IrOperations

interface Transform<T> {
fun transform(input: T): T
}

interface Plugin {
fun layout(codegenSchema: CodegenSchema): SchemaAndOperationsLayout? {
return null
}

fun operationOutputGenerator(): OperationOutputGenerator? {
return null
}

fun irOperationsTransform(): Transform<IrOperations>? {
return null
}

fun javaOutputTransform(): Transform<JavaOutput>? {
return null
}

fun kotlinOutputTransform(): Transform<KotlinOutput>? {
return null
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.apollographql.apollo3.compiler.codegen

import com.apollographql.apollo3.annotations.ApolloInternal
import com.apollographql.apollo3.compiler.CodegenSchema
import com.apollographql.apollo3.compiler.PackageNameGenerator
import com.apollographql.apollo3.compiler.allTypes
Expand Down Expand Up @@ -156,8 +155,7 @@ internal fun String.selections(): String = "${this}Selections"
*/
internal fun String.variableName(): String = this.withUnderscorePrefix()

@ApolloInternal
fun Layout(
fun SchemaAndOperationsLayout(
codegenSchema: CodegenSchema,
packageNameGenerator: PackageNameGenerator,
useSemanticNaming: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ enum class ResolverKeyKind {
}

@Serializable
internal class ResolverEntry(
class ResolverEntry(
val key: ResolverKey,
val className: ResolverClassName
)