Skip to content

Commit

Permalink
Introduce SchemaLayout/OperationsLayout/ExecutableSchemaLayout (#5588)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbonnin authored Feb 1, 2024
1 parent 2b517d1 commit 5568815
Show file tree
Hide file tree
Showing 91 changed files with 412 additions and 383 deletions.
30 changes: 26 additions & 4 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;Lcom/apollographql/apollo3/compiler/PackageNameGenerator;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/PackageNameGenerator;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/PackageNameGenerator;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/codegen/SchemaAndOperationsLayout;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 abstract interface class com/apollographql/apollo3/compiler/ApolloCompiler$Logger {
Expand Down Expand Up @@ -409,6 +409,20 @@ public final class com/apollographql/apollo3/compiler/VersionKt {
public static final field APOLLO_VERSION Ljava/lang/String;
}

public abstract interface class com/apollographql/apollo3/compiler/codegen/CommonLayout {
public abstract fun propertyName (Ljava/lang/String;)Ljava/lang/String;
public abstract fun topLevelName (Ljava/lang/String;)Ljava/lang/String;
}

public abstract interface class com/apollographql/apollo3/compiler/codegen/ExecutableSchemaLayout : com/apollographql/apollo3/compiler/codegen/SchemaLayout {
}

public abstract interface class com/apollographql/apollo3/compiler/codegen/OperationsLayout : com/apollographql/apollo3/compiler/codegen/CommonLayout {
public abstract fun executableDocumentPackageName (Ljava/lang/String;)Ljava/lang/String;
public abstract fun fragmentName (Ljava/lang/String;)Ljava/lang/String;
public abstract fun operationName (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
}

public final class com/apollographql/apollo3/compiler/codegen/ResolverClassName$$serializer : kotlinx/serialization/internal/GeneratedSerializer {
public static final field INSTANCE Lcom/apollographql/apollo3/compiler/codegen/ResolverClassName$$serializer;
public fun childSerializers ()[Lkotlinx/serialization/KSerializer;
Expand Down Expand Up @@ -475,6 +489,14 @@ public final class com/apollographql/apollo3/compiler/codegen/ResolverKeyKind :
public static fun values ()[Lcom/apollographql/apollo3/compiler/codegen/ResolverKeyKind;
}

public abstract interface class com/apollographql/apollo3/compiler/codegen/SchemaAndOperationsLayout : com/apollographql/apollo3/compiler/codegen/OperationsLayout, com/apollographql/apollo3/compiler/codegen/SchemaLayout {
}

public abstract interface class com/apollographql/apollo3/compiler/codegen/SchemaLayout : com/apollographql/apollo3/compiler/codegen/CommonLayout {
public abstract fun schemaPackageName ()Ljava/lang/String;
public abstract fun schemaTypeName (Ljava/lang/String;)Ljava/lang/String;
}

public abstract interface class com/apollographql/apollo3/compiler/codegen/SourceFile {
public abstract fun getName ()Ljava/lang/String;
public abstract fun getPackageName ()Ljava/lang/String;
Expand Down Expand Up @@ -521,7 +543,7 @@ public final class com/apollographql/apollo3/compiler/codegen/java/JavaOutputKt
public static final fun toSourceOutput (Lcom/apollographql/apollo3/compiler/codegen/java/JavaOutput;)Lcom/apollographql/apollo3/compiler/codegen/SourceOutput;
}

public final class com/apollographql/apollo3/compiler/codegen/java/adapter/AdapterCommonKt {
public final class com/apollographql/apollo3/compiler/codegen/java/helpers/AdapterCommonKt {
public static final fun singletonAdapterInitializer (Lcom/squareup/javapoet/TypeName;Lcom/squareup/javapoet/TypeName;Z)Lcom/squareup/javapoet/CodeBlock;
public static synthetic fun singletonAdapterInitializer$default (Lcom/squareup/javapoet/TypeName;Lcom/squareup/javapoet/TypeName;ZILjava/lang/Object;)Lcom/squareup/javapoet/CodeBlock;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import com.apollographql.apollo3.ast.pretty
import com.apollographql.apollo3.ast.toGQLDocument
import com.apollographql.apollo3.ast.validateAsExecutable
import com.apollographql.apollo3.ast.validateAsSchemaAndAddApolloDefinition
import com.apollographql.apollo3.compiler.codegen.LayoutImpl
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.toSourceOutput
Expand Down Expand Up @@ -313,12 +316,11 @@ object ApolloCompiler {
}
}


fun buildSchemaSources(
codegenSchema: CodegenSchema,
usedCoordinates: UsedCoordinates?,
codegenOptions: CodegenOptions,
packageNameGenerator: PackageNameGenerator,
schemaLayout: SchemaLayout?,
compilerKotlinHooks: List<ApolloCompilerKotlinHooks>?,
compilerJavaHooks: List<ApolloCompilerJavaHooks>?,
): SourceOutput {
Expand All @@ -327,12 +329,19 @@ object ApolloCompiler {
val targetLanguage = defaultTargetLanguage(codegenOptions.targetLanguage, emptyList())
codegenOptions.validate()

val layout = schemaLayout ?: LayoutImpl(
codegenSchema = codegenSchema,
packageNameGenerator = packageNameGenerator(codegenOptions.packageName, codegenOptions.rootPackageName),
useSemanticNaming = codegenOptions.useSemanticNaming ?: defaultUseSemanticNaming,
decapitalizeFields = codegenOptions.decapitalizeFields ?: defaultDecapitalizeFields
)

return if (targetLanguage == TargetLanguage.JAVA) {
JavaCodegen.buildSchemaSources(
codegenSchema = codegenSchema,
irSchema = irSchema,
codegenOptions = codegenOptions,
packageNameGenerator = packageNameGenerator,
layout = layout,
compilerJavaHooks = compilerJavaHooks ?: defaultCompilerJavaHooks,
).toSourceOutput()
} else {
Expand All @@ -341,7 +350,7 @@ object ApolloCompiler {
targetLanguage = targetLanguage,
irSchema = irSchema,
codegenOptions = codegenOptions,
packageNameGenerator = packageNameGenerator,
layout = layout,
compilerKotlinHooks = compilerKotlinHooks ?: defaultCompilerKotlinHooks,
).toSourceOutput()
}
Expand All @@ -353,7 +362,7 @@ object ApolloCompiler {
downstreamUsedCoordinates: UsedCoordinates?,
upstreamCodegenMetadata: List<CodegenMetadata>,
codegenOptions: CodegenOptions,
packageNameGenerator: PackageNameGenerator?,
layout: SchemaAndOperationsLayout?,
operationOutputGenerator: OperationOutputGenerator?,
compilerKotlinHooks: List<ApolloCompilerKotlinHooks>?,
compilerJavaHooks: List<ApolloCompilerJavaHooks>?,
Expand Down Expand Up @@ -391,20 +400,22 @@ object ApolloCompiler {
}
}

val realizedPackageNameGenerator = when {
packageNameGenerator != null -> packageNameGenerator
codegenOptions.packageName != null -> PackageNameGenerator.Flat(codegenOptions.packageName)
codegenOptions.rootPackageName != null -> PackageNameGenerator.NormalizedPathAware(codegenOptions.rootPackageName)
else -> error("Apollo: no packageName found")
}

@Suppress("NAME_SHADOWING")
val layout = layout ?: LayoutImpl(
codegenSchema = codegenSchema,
packageNameGenerator = packageNameGenerator(codegenOptions.packageName, codegenOptions.rootPackageName),
useSemanticNaming = codegenOptions.useSemanticNaming ?: defaultUseSemanticNaming,
decapitalizeFields = codegenOptions.decapitalizeFields ?: defaultDecapitalizeFields
)

var sourceOutput: SourceOutput? = null
if (upstreamCodegenMetadata.isEmpty()) {
sourceOutput = sourceOutput plus buildSchemaSources(
codegenSchema = codegenSchema,
usedCoordinates = downstreamUsedCoordinates?.mergeWith(irOperations.usedFields),
codegenOptions = codegenOptions,
packageNameGenerator = realizedPackageNameGenerator,
schemaLayout = layout,
compilerKotlinHooks = compilerKotlinHooks,
compilerJavaHooks = compilerJavaHooks
)
Expand All @@ -416,7 +427,7 @@ object ApolloCompiler {
operationOutput = operationOutput,
upstreamCodegenMetadata = upstreamCodegenMetadata + listOfNotNull(sourceOutput?.codegenMetadata),
codegenOptions = codegenOptions,
packageNameGenerator = realizedPackageNameGenerator,
layout = layout,
compilerJavaHooks = compilerJavaHooks,
).toSourceOutput()
} else {
Expand All @@ -427,7 +438,7 @@ object ApolloCompiler {
operationOutput = operationOutput,
upstreamCodegenMetadata = upstreamCodegenMetadata + listOfNotNull(sourceOutput?.codegenMetadata),
codegenOptions = codegenOptions,
packageNameGenerator = realizedPackageNameGenerator,
layout = layout,
compilerKotlinHooks = compilerKotlinHooks,
).toSourceOutput()
}
Expand All @@ -444,7 +455,7 @@ object ApolloCompiler {
codegenSchemaOptions: CodegenSchemaOptions,
irOptions: IrOptions,
codegenOptions: CodegenOptions,
packageNameGenerator: PackageNameGenerator?,
layout: SchemaAndOperationsLayout?,
operationOutputGenerator: OperationOutputGenerator?,
compilerJavaHooks: List<ApolloCompilerJavaHooks>?,
compilerKotlinHooks: List<ApolloCompilerKotlinHooks>?,
Expand Down Expand Up @@ -472,7 +483,7 @@ object ApolloCompiler {
downstreamUsedCoordinates = emptyMap(),
upstreamCodegenMetadata = emptyList(),
codegenOptions = codegenOptions,
packageNameGenerator = packageNameGenerator,
layout = layout,
compilerJavaHooks = compilerJavaHooks,
compilerKotlinHooks = compilerKotlinHooks,
operationManifestFile = operationManifestFile,
Expand All @@ -489,11 +500,17 @@ object ApolloCompiler {
packageName: String,
serviceName: String,
): SourceOutput {
val layout = LayoutImpl(
codegenSchema,
PackageNameGenerator.Flat(packageName),
false,
false
)
return KotlinCodegen.buildExecutableSchema(
codegenSchema = codegenSchema,
codegenMetadata = codegenMetadata,
irTargetObjects = irTargetObjects,
packageName = packageName,
layout = layout,
serviceName = serviceName
).toSourceOutput()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,12 @@ internal fun compilerKotlinHooks(compilerKotlinHooks: List<ApolloCompilerKotlinH

internal fun compilerJavaHooks(compilerJavaHooks: List<ApolloCompilerJavaHooks>?): List<ApolloCompilerJavaHooks> {
return compilerJavaHooks.orEmpty()
}

internal fun packageNameGenerator(packageName: String?, rootPackageName: String?): PackageNameGenerator {
return when {
packageName != null -> PackageNameGenerator.Flat(packageName)
rootPackageName != null -> PackageNameGenerator.NormalizedPathAware(rootPackageName)
else -> error("Apollo: missing 'packageName'")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import com.apollographql.apollo3.compiler.withUnderscorePrefix
*
* Inputs should always be GraphQL identifiers and outputs are valid Kotlin/Java identifiers.
*/
internal class SchemaAndOperationsLayoutImpl(
internal class LayoutImpl(
codegenSchema: CodegenSchema,
private val packageNameGenerator: PackageNameGenerator,
private val useSemanticNaming: Boolean,
private val decapitalizeFields: Boolean
) : SchemaAndOperationsLayout {
private val schemaPackageName = executableDocumentPackageName(codegenSchema.normalizedPath ?: "")
) : SchemaAndOperationsLayout, ExecutableSchemaLayout {
private val schemaPackageName = executableDocumentPackageName(codegenSchema.normalizedPath)
private val schemaTypeToClassName: Map<String, String> = mutableMapOf<String, String>().apply {
val usedNames = mutableSetOf<String>()
val allTypes = codegenSchema.allTypes()
Expand Down Expand Up @@ -127,23 +127,23 @@ internal fun modelName(info: IrFieldInfo): String {
}


internal fun SchemaAndOperationsLayoutImpl.typePackageName() = "${schemaPackageName()}.type"
internal fun SchemaAndOperationsLayoutImpl.typeBuilderPackageName() = "${schemaPackageName()}.type.builder"
internal fun SchemaAndOperationsLayoutImpl.typeAdapterPackageName() = "${schemaPackageName()}.type.adapter"
internal fun SchemaAndOperationsLayoutImpl.typeUtilPackageName() = "${schemaPackageName()}.type.util"
internal fun SchemaLayout.typePackageName() = "${schemaPackageName()}.type"
internal fun SchemaLayout.typeBuilderPackageName() = "${schemaPackageName()}.type.builder"
internal fun SchemaLayout.typeAdapterPackageName() = "${schemaPackageName()}.type.adapter"
internal fun SchemaLayout.typeUtilPackageName() = "${schemaPackageName()}.type.util"

internal fun SchemaAndOperationsLayoutImpl.paginationPackageName() = "${schemaPackageName()}.pagination"
internal fun SchemaAndOperationsLayoutImpl.schemaSubPackageName() = "${schemaPackageName()}.schema"
internal fun SchemaAndOperationsLayoutImpl.executionPackageName() = "${schemaPackageName()}.execution"
internal fun SchemaLayout.paginationPackageName() = "${schemaPackageName()}.pagination"
internal fun SchemaLayout.schemaSubPackageName() = "${schemaPackageName()}.schema"
internal fun ExecutableSchemaLayout.executionPackageName() = "${schemaPackageName()}.execution"

internal fun SchemaAndOperationsLayoutImpl.operationAdapterPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.adapter"
internal fun SchemaAndOperationsLayoutImpl.operationResponseFieldsPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.selections"
internal fun OperationsLayout.operationAdapterPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.adapter"
internal fun OperationsLayout.operationResponseFieldsPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.selections"

internal fun SchemaAndOperationsLayoutImpl.fragmentPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.fragment"
internal fun SchemaAndOperationsLayoutImpl.fragmentAdapterPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.fragment.adapter"
internal fun SchemaAndOperationsLayoutImpl.fragmentResponseFieldsPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.fragment.selections"
internal fun OperationsLayout.fragmentPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.fragment"
internal fun OperationsLayout.fragmentAdapterPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.fragment.adapter"
internal fun OperationsLayout.fragmentResponseFieldsPackageName(filePath: String) = "${executableDocumentPackageName(filePath)}.fragment.selections"

internal fun SchemaAndOperationsLayoutImpl.operationName(operation: IrOperation) = operationName(operation.name, operation.operationType.name)
internal fun OperationsLayout.operationName(operation: IrOperation) = operationName(operation.name, operation.operationType.name)

internal fun String.responseAdapter(): String = "${this}_ResponseAdapter"
internal fun String.inputAdapter(): String = "${this}_InputAdapter"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.apollographql.apollo3.compiler.codegen

internal interface CommonLayout {
interface CommonLayout {
fun topLevelName(name: String): String
fun propertyName(name: String): String
}

internal interface SchemaLayout : CommonLayout {
interface SchemaLayout : CommonLayout {
fun schemaPackageName(): String
fun schemaTypeName(schemaTypeName: String): String
}

internal interface OperationsLayout {
interface OperationsLayout: CommonLayout {
fun executableDocumentPackageName(filePath: String?): String
fun operationName(name: String, capitalizedOperationType: String): String
fun fragmentName(name: String): String
}

internal interface SchemaAndOperationsLayout : SchemaLayout, OperationsLayout
interface SchemaAndOperationsLayout : SchemaLayout, OperationsLayout
interface ExecutableSchemaLayout : SchemaLayout
Loading

0 comments on commit 5568815

Please sign in to comment.