diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
index 2aa1ef4e481..1bde7f7d451 100644
--- a/.idea/codeStyles/Project.xml
+++ b/.idea/codeStyles/Project.xml
@@ -23,6 +23,9 @@
+
+
+
diff --git a/libraries/apollo-compiler/api/apollo-compiler.api b/libraries/apollo-compiler/api/apollo-compiler.api
index 12ca5b4bb97..9d3a26b2a88 100644
--- a/libraries/apollo-compiler/api/apollo-compiler.api
+++ b/libraries/apollo-compiler/api/apollo-compiler.api
@@ -666,8 +666,9 @@ public final class com/apollographql/apollo3/compiler/ir/IrTargetKt {
}
public final class com/apollographql/apollo3/compiler/ir/IrTargetObject {
- public fun (Ljava/lang/String;Lcom/apollographql/apollo3/compiler/ir/IrClassName;ZLjava/lang/String;Ljava/util/List;)V
+ public fun (Ljava/lang/String;Lcom/apollographql/apollo3/compiler/ir/IrClassName;ZZLjava/lang/String;Ljava/util/List;)V
public final fun getFields ()Ljava/util/List;
+ public final fun getHasNoArgsConstructor ()Z
public final fun getName ()Ljava/lang/String;
public final fun getOperationType ()Ljava/lang/String;
public final fun getTargetClassName ()Lcom/apollographql/apollo3/compiler/ir/IrClassName;
diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/ExecutableSchemaBuilderBuilder.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/ExecutableSchemaBuilderBuilder.kt
new file mode 100644
index 00000000000..a67f12294ce
--- /dev/null
+++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/ExecutableSchemaBuilderBuilder.kt
@@ -0,0 +1,81 @@
+package com.apollographql.apollo3.compiler.codegen.kotlin
+
+import com.apollographql.apollo3.compiler.capitalizeFirstLetter
+import com.apollographql.apollo3.compiler.ir.IrTargetObject
+import com.apollographql.apollo3.compiler.ir.asKotlinPoet
+import com.squareup.kotlinpoet.ClassName
+import com.squareup.kotlinpoet.CodeBlock
+import com.squareup.kotlinpoet.FunSpec
+import com.squareup.kotlinpoet.LambdaTypeName
+import com.squareup.kotlinpoet.MemberName
+import com.squareup.kotlinpoet.ParameterSpec
+import com.squareup.kotlinpoet.joinToCode
+
+internal class ExecutableSchemaBuilderBuilder(
+ private val context: KotlinContext,
+ private val serviceName: String,
+ private val mainResolver: ClassName,
+ private val adapterRegistry: MemberName,
+ private val irTargetObjects: List,
+) : CgFileBuilder {
+ val simpleName = context.layout.capitalizedIdentifier("${serviceName}ExecutableSchemaBuilder")
+ override fun prepare() {
+
+ }
+
+ override fun build(): CgFile {
+ return CgFile(packageName = context.layout.executionPackageName(), fileName = simpleName, funSpecs = listOf(funSpec())
+ )
+ }
+
+ private fun funSpec(): FunSpec {
+ val rootIrTargetObjects = listOf("query", "mutation", "subscription").map { operationType ->
+ irTargetObjects.find { it.operationType == operationType }
+ }
+
+ return FunSpec.builder(simpleName)
+ .returns(KotlinSymbols.ExecutableSchemaBuilder)
+ .apply {
+ addParameter(ParameterSpec.builder("schema", KotlinSymbols.Schema).build())
+ rootIrTargetObjects.forEach { irTargetObject ->
+ if (irTargetObject != null) {
+ addParameter(
+ ParameterSpec.builder(
+ name = "root${irTargetObject.operationType?.capitalizeFirstLetter()}Object",
+ type = LambdaTypeName.get(parameters = emptyList(), returnType = irTargetObject.targetClassName.asKotlinPoet())
+ ).apply {
+ if (irTargetObject.isSingleton) {
+ defaultValue(CodeBlock.of("{ %L }", irTargetObject.targetClassName.asKotlinPoet()))
+ } else if (irTargetObject.hasNoArgsConstructor) {
+ defaultValue(CodeBlock.of("{ %L() }", irTargetObject.targetClassName.asKotlinPoet()))
+ }
+ }.build())
+ }
+ }
+ }
+ .addCode(
+ CodeBlock.builder()
+ .add("return %L()\n", KotlinSymbols.ExecutableSchemaBuilder)
+ .add(".schema(schema)\n")
+ .add(".resolver(%L)\n", mainResolver)
+ .add(".adapterRegistry(%L)\n", adapterRegistry)
+ .add(".roots(%L.create(", KotlinSymbols.Roots)
+ .apply {
+ rootIrTargetObjects.map { irTargetObject ->
+ if (irTargetObject == null) {
+ CodeBlock.of("null")
+ } else {
+ CodeBlock.of("root${irTargetObject.operationType?.capitalizeFirstLetter()}Object")
+ }
+ }.joinToCode(", ")
+ .let {
+ add(it)
+ }
+ }
+ .add("))\n")
+ .build()
+ )
+ .build()
+ }
+
+}
diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinCodeGen.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinCodeGen.kt
index ec9055dd223..05902ae6784 100644
--- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinCodeGen.kt
+++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinCodeGen.kt
@@ -389,18 +389,27 @@ internal object KotlinCodeGen {
val builders = mutableListOf()
- builders.add(
- MainResolverBuilder(
- context = context,
- serviceName = serviceName,
- irTargetObjects = irTargetObjects
- )
+ val mainResolverBuilder = MainResolverBuilder(
+ context = context,
+ serviceName = serviceName,
+ irTargetObjects = irTargetObjects
)
+ builders.add(mainResolverBuilder)
+
+ val adapterRegistryBuilder = AdapterRegistryBuilder(
+ context = context,
+ serviceName = serviceName,
+ codegenSchema = codegenSchema
+ )
+ builders.add(adapterRegistryBuilder)
+
builders.add(
- AdapterRegistryBuilder(
+ ExecutableSchemaBuilderBuilder(
context = context,
serviceName = serviceName,
- codegenSchema = codegenSchema
+ mainResolver = mainResolverBuilder.className,
+ adapterRegistry = adapterRegistryBuilder.memberName,
+ irTargetObjects = irTargetObjects
)
)
diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinSymbols.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinSymbols.kt
index 64e7799e7f4..d9c8409fbfe 100644
--- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinSymbols.kt
+++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/KotlinSymbols.kt
@@ -15,8 +15,11 @@ import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
* Symbols can be [ClassName] or [MemberName]
*/
internal object KotlinSymbols {
+ val ExecutableSchemaBuilder = ClassName("com.apollographql.apollo3.execution", "ExecutableSchema", "Builder")
val Resolver = ClassName("com.apollographql.apollo3.execution", "Resolver")
val ResolveInfo = ClassName("com.apollographql.apollo3.execution", "ResolveInfo")
+ val Roots = ClassName("com.apollographql.apollo3.execution", "Roots")
+ val Schema = ClassName("com.apollographql.apollo3.ast", "Schema")
val ObjectType = ClassNames.ObjectType.toKotlinPoetClassName()
val ObjectTypeBuilder = ClassNames.ObjectTypeBuilder.toKotlinPoetClassName()
val InterfaceType = ClassNames.InterfaceType.toKotlinPoetClassName()
diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/AdapterRegistryBuilder.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/AdapterRegistryBuilder.kt
index f16dc01eee5..6a0bdb44e78 100644
--- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/AdapterRegistryBuilder.kt
+++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/AdapterRegistryBuilder.kt
@@ -16,6 +16,7 @@ import com.apollographql.apollo3.compiler.ir.IrInputObjectType
import com.apollographql.apollo3.compiler.ir.IrNonNullType
import com.apollographql.apollo3.compiler.ir.IrScalarType
import com.squareup.kotlinpoet.CodeBlock
+import com.squareup.kotlinpoet.MemberName
import com.squareup.kotlinpoet.PropertySpec
internal class AdapterRegistryBuilder(
@@ -23,8 +24,10 @@ internal class AdapterRegistryBuilder(
val serviceName: String,
val codegenSchema: CodegenSchema
) : CgFileBuilder {
- val packageName = context.layout.executionPackageName()
- val simpleName = context.layout.capitalizedIdentifier("${serviceName}AdapterRegistry")
+ private val packageName = context.layout.executionPackageName()
+ private val simpleName = context.layout.capitalizedIdentifier("${serviceName}AdapterRegistry")
+
+ val memberName = MemberName(packageName, simpleName)
override fun prepare() {
}
diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/MainResolverBuilder.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/MainResolverBuilder.kt
index 24eaec17a25..ff4e9cf7778 100644
--- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/MainResolverBuilder.kt
+++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/file/MainResolverBuilder.kt
@@ -1,7 +1,6 @@
package com.apollographql.apollo3.compiler.codegen.kotlin.file
import com.apollographql.apollo3.compiler.applyIf
-import com.apollographql.apollo3.compiler.capitalizeFirstLetter
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFile
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFileBuilder
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinContext
@@ -30,8 +29,8 @@ internal class MainResolverBuilder(
val serviceName: String,
val irTargetObjects: List,
) : CgFileBuilder {
- val packageName = context.layout.executionPackageName()
- val simpleName = context.layout.capitalizedIdentifier("${serviceName}Resolver")
+ private val packageName = context.layout.executionPackageName()
+ private val simpleName = context.layout.capitalizedIdentifier("${serviceName}Resolver")
val className = ClassName(packageName, simpleName)
@@ -45,47 +44,15 @@ internal class MainResolverBuilder(
)
}
- private fun primaryConstructor(): FunSpec {
- return FunSpec.constructorBuilder()
- .build()
- }
private fun typeSpec(): TypeSpec {
- return TypeSpec.classBuilder(simpleName)
- .primaryConstructor(primaryConstructor())
+ return TypeSpec.objectBuilder(simpleName)
.addSuperinterface(ClassName("com.apollographql.apollo3.execution", "MainResolver"))
.addProperty(typenamesPropertySpec())
.addProperty(resolversPropertySpec())
.addFunction(typenameFunSpec())
.addFunction(resolveFunSpec())
.addAnnotation(suppressDeprecationAnnotationSpec)
- .apply {
- listOf("query", "mutation", "subscription").forEach { operationType ->
- val rootTargetObject = irTargetObjects.firstOrNull { it.operationType == operationType }
-
- addFunction(rootObjectFunSpec(operationType, rootTargetObject))
- }
- }
- .build()
- }
-
- private fun rootObjectFunSpec(operationType: String, irTargetObject: IrTargetObject?): FunSpec {
-
- return FunSpec.builder("root${operationType.capitalizeFirstLetter()}Object")
- .addModifiers(KModifier.OVERRIDE)
- .returns(KotlinSymbols.Any.copy(nullable = true))
- .apply {
- if (irTargetObject == null) {
- addCode("return null")
- } else {
- val maybeParenthesis = if (irTargetObject.isSingleton) {
- ""
- } else {
- "()"
- }
- addCode("return %T$maybeParenthesis", irTargetObject.targetClassName.asKotlinPoet())
- }
- }
.build()
}
@@ -232,7 +199,7 @@ internal class MainResolverBuilder(
builder.add(
"%L·=·it.getArgument<%T>(%S)",
irTargetArgument.targetName,
- context.resolver.resolveIrType(type, false, false),
+ context.resolver.resolveIrType(type = type, jsExport = false, isInterface = false),
irTargetArgument.name,
)
if (irTargetArgument.type !is IrOptionalType) {
diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ir/IrTarget.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ir/IrTarget.kt
index 66c983588d3..e84a88f47ba 100644
--- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ir/IrTarget.kt
+++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/ir/IrTarget.kt
@@ -33,6 +33,7 @@ class IrTargetObject(
val name: String,
val targetClassName: IrClassName,
val isSingleton: Boolean,
+ val hasNoArgsConstructor: Boolean,
val operationType: String?,
val fields: List,
)
\ No newline at end of file
diff --git a/libraries/apollo-debug-server/build.gradle.kts b/libraries/apollo-debug-server/build.gradle.kts
index c87556ca83f..e0d3f598002 100644
--- a/libraries/apollo-debug-server/build.gradle.kts
+++ b/libraries/apollo-debug-server/build.gradle.kts
@@ -23,9 +23,8 @@ kotlin {
dependencies {
implementation(project(":apollo-normalized-cache"))
- // apollo-execution is not published: we bundle it into the aar artifact
- compileOnly(project(":apollo-execution"))
api(project(":apollo-ast"))
+ api(project(":apollo-api"))
}
}
@@ -44,13 +43,21 @@ val shadow = configurations.create("shadow") {
dependencies {
add("kspCommonMainMetadata", project(":apollo-ksp"))
- add("kspCommonMainMetadata", apollo.apolloKspProcessor(file("src/androidMain/resources/schema.graphqls"), "apolloDebugServer", "com.apollographql.apollo3.debugserver.internal.graphql"))
+ add(
+ "kspCommonMainMetadata",
+ apollo.apolloKspProcessor(
+ schema = file(path = "src/androidMain/resources/schema.graphqls"),
+ service = "apolloDebugServer",
+ packageName = "com.apollographql.apollo3.debugserver.internal.graphql"
+ )
+ )
+ // apollo-execution is not published: we bundle it into the aar artifact
add(shadow.name, project(":apollo-execution")) {
isTransitive = false
}
}
-configurations.getByName("compileOnly").extendsFrom(shadow)
+configurations.getByName(kotlin.sourceSets.getByName("commonMain").compileOnlyConfigurationName).extendsFrom(shadow)
android {
compileSdk = libs.versions.android.sdkversion.compile.get().toInt()
diff --git a/libraries/apollo-debug-server/src/androidMain/kotlin/com/apollographql/apollo3/debugserver/internal/server/Server.android.kt b/libraries/apollo-debug-server/src/androidMain/kotlin/com/apollographql/apollo3/debugserver/internal/server/Server.android.kt
index 09c4c8d3d4e..23cd85f2c89 100644
--- a/libraries/apollo-debug-server/src/androidMain/kotlin/com/apollographql/apollo3/debugserver/internal/server/Server.android.kt
+++ b/libraries/apollo-debug-server/src/androidMain/kotlin/com/apollographql/apollo3/debugserver/internal/server/Server.android.kt
@@ -14,13 +14,14 @@ import kotlinx.coroutines.launch
import java.io.BufferedReader
import java.io.PrintStream
import java.util.concurrent.Executors
+import java.util.concurrent.atomic.AtomicReference
internal actual fun createServer(
- apolloClients: Map,
+ apolloClients: AtomicReference