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

[⚙️compiler] Remove OperationOutputGenerator from the Plugin API #5599

Merged
merged 5 commits into from
Feb 6, 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
8 changes: 5 additions & 3 deletions libraries/apollo-compiler/api/apollo-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public abstract interface class com/apollographql/apollo3/compiler/Plugin {
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 fun operationIds (Ljava/util/List;)Ljava/util/List;
}

public final class com/apollographql/apollo3/compiler/RuntimeAdapterInitializer : com/apollographql/apollo3/compiler/AdapterInitializer {
Expand Down Expand Up @@ -759,8 +759,10 @@ public final class com/apollographql/apollo3/compiler/operationoutput/OperationD
public final fun serializer ()Lkotlinx/serialization/KSerializer;
}

public final class com/apollographql/apollo3/compiler/operationoutput/OperationOutputKt {
public static final fun findOperationId (Ljava/util/Map;Ljava/lang/String;)Ljava/lang/String;
public final class com/apollographql/apollo3/compiler/operationoutput/OperationId {
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
public final fun getId ()Ljava/lang/String;
public final fun getName ()Ljava/lang/String;
}

public final class com/apollographql/apollo3/compiler/pqm/PersistedQueryManifest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ object ApolloCompiler {
upstreamCodegenMetadata: List<CodegenMetadata>,
codegenOptions: CodegenOptions,
layout: SchemaAndOperationsLayout?,
operationOutputGenerator: OperationOutputGenerator?,
@Suppress("DEPRECATION") operationOutputGenerator: OperationOutputGenerator?,
irOperationsTransform: Transform<IrOperations>?,
javaOutputTransform: Transform<JavaOutput>?,
kotlinOutputTransform: Transform<KotlinOutput>?,
Expand Down Expand Up @@ -464,7 +464,7 @@ object ApolloCompiler {
irOptions: IrOptions,
codegenOptions: CodegenOptions,
layoutFactory: LayoutFactory?,
operationOutputGenerator: OperationOutputGenerator?,
@Suppress("DEPRECATION") operationOutputGenerator: OperationOutputGenerator?,
irOperationsTransform: Transform<IrOperations>?,
javaOutputTransform: Transform<JavaOutput>?,
kotlinOutputTransform: Transform<KotlinOutput>?,
Expand Down Expand Up @@ -510,6 +510,7 @@ object ApolloCompiler {
packageName: String,
serviceName: String,
): SourceOutput {
@Suppress("DEPRECATION")
val layout = LayoutImpl(
codegenSchema = codegenSchema,
packageNameGenerator = PackageNameGenerator.Flat(packageName),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.apollographql.apollo3.compiler

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import com.apollographql.apollo3.compiler.operationoutput.OperationDescriptor
import com.apollographql.apollo3.compiler.operationoutput.OperationOutput

Expand All @@ -10,6 +11,9 @@ import com.apollographql.apollo3.compiler.operationoutput.OperationOutput
*
* If you don't need batch compute, use [OperationOutputGenerator.Default]
*/
@Suppress("DEPRECATION")
@Deprecated("Use Apollo compiler plugins instead")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
interface OperationOutputGenerator {
/**
* Generate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.apollographql.apollo3.compiler

import com.apollographql.apollo3.annotations.ApolloDeprecatedSince
import java.io.File

/**
* A [PackageNameGenerator] computes the package name for a given file. Files can be either
* - executable files containing operations and fragments
* - schema files containing type definitions or introspection json
*/
@Deprecated("Use Apollo compiler plugins instead")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
interface PackageNameGenerator {
/**
* This will be called with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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
import com.apollographql.apollo3.compiler.operationoutput.OperationDescriptor
import com.apollographql.apollo3.compiler.operationoutput.OperationId

/**
* Entry point for customizing the behaviour of the Apollo Compiler besides the
Expand All @@ -19,9 +21,11 @@ interface Plugin {
}

/**
* @return the [OperationOutputGenerator] or null to use the default [OperationOutputGenerator]
* Computes operation ids for persisted queries.
*
* @return a list of [OperationId] matching an operation name to its id or null to use the SHA256 default
*/
fun operationOutputGenerator(): OperationOutputGenerator? {
fun operationIds(descriptors: List<OperationDescriptor>): List<OperationId>? {
return null
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.apollographql.apollo3.compiler.codegen

import com.apollographql.apollo3.annotations.ApolloInternal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,35 @@ import kotlinx.serialization.Serializable
/**
* [OperationOutput] is a map where the operationId is the key and [OperationDescriptor] the value
*
* By default the operationId is a sha256 but it can be changed for custom whitelisting implementations
* By default, the operationId is a sha256. It can be changed for custom whitelisting/persisted queries implementations.
*/
typealias OperationOutput = Map<String, OperationDescriptor>

/**
* This structure is also generated by other tools (iOS, cli, ...), try to keep the field names if possible.
* A description of an operation to compute an id from
*/
@Serializable
class OperationDescriptor(
/**
* The name of the operation
*/
val name: String,
/**
* The source of the operation as it is sent over the wire, including fragments
*/
val source: String,
/**
* The type of the operation. One of "query", "mutation", "subscription"
*/
val type: String
)

fun OperationOutput.findOperationId(name: String): String {
/**
* The id of an operation associated with its name so that it can be looked up.
*/
class OperationId(val id: String, val name: String)

internal fun OperationOutput.findOperationId(name: String): String {
val id = entries.find { it.value.name == name }?.key
check(id != null) {
"cannot find operation ID for '$name', check your operationOutput.json"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.apollographql.apollo3.gradle.api

import com.android.build.gradle.api.BaseVariant
Expand Down Expand Up @@ -268,6 +270,9 @@ interface Service {
*
* Default value: [OperationIdGenerator.Sha256]
*/
@Suppress("DEPRECATION")
@Deprecated("Use Apollo compiler plugins instead")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
val operationOutputGenerator: Property<OperationOutputGenerator>

/**
Expand Down Expand Up @@ -296,6 +301,8 @@ interface Service {
*
* @see [packageName]
*/
@Deprecated("Use Apollo compiler plugins instead")
@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_0_0)
val packageNameGenerator: Property<PackageNameGenerator>

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private abstract class GenerateSourcesFromIr : WorkAction<GenerateSourcesFromIrP
javaOutputTransform = plugin?.javaOutputTransform(),
kotlinOutputTransform = plugin?.kotlinOutputTransform(),
operationManifestFile = operationManifestFile.orNull?.asFile,
operationOutputGenerator = plugin?.operationOutputGenerator()
operationOutputGenerator = plugin?.toOperationOutputGenerator()
).writeTo(outputDir.get().asFile, true, metadataOutputFile.orNull?.asFile)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private abstract class GenerateSources : WorkAction<GenerateSourcesParameters> {
return plugin?.layout(codegenSchema)
}
},
operationOutputGenerator = plugin?.operationOutputGenerator(),
operationOutputGenerator = plugin?.toOperationOutputGenerator(),
irOperationsTransform = plugin?.irOperationsTransform(),
javaOutputTransform = plugin?.javaOutputTransform(),
kotlinOutputTransform = plugin?.kotlinOutputTransform(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("DEPRECATION")

package com.apollographql.apollo3.gradle.internal

import com.apollographql.apollo3.compiler.APOLLO_VERSION
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
@file:Suppress("DEPRECATION")

package com.apollographql.apollo3.gradle.internal

import com.apollographql.apollo3.compiler.MANIFEST_NONE
import com.apollographql.apollo3.compiler.MANIFEST_OPERATION_OUTPUT
import com.apollographql.apollo3.compiler.MANIFEST_PERSISTED_QUERY
import com.apollographql.apollo3.compiler.OperationIdGenerator
import com.apollographql.apollo3.compiler.OperationOutputGenerator
import com.apollographql.apollo3.compiler.Plugin
import com.apollographql.apollo3.compiler.operationoutput.OperationDescriptor
import com.apollographql.apollo3.compiler.operationoutput.OperationId
import com.apollographql.apollo3.compiler.operationoutput.OperationOutput
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Provider
import java.io.File
Expand Down Expand Up @@ -79,3 +87,17 @@ internal fun DefaultService.operationManifestFormat(): Provider<String> {
}
}

internal fun Plugin.toOperationOutputGenerator(): OperationOutputGenerator {
return object : OperationOutputGenerator {
override fun generate(operationDescriptorList: Collection<OperationDescriptor>): OperationOutput {
var operationIds = operationIds(operationDescriptorList.toList())
if (operationIds == null) {
operationIds = operationDescriptorList.map { OperationId(OperationIdGenerator.Sha256.apply(it.source, it.name), it.name) }
}
return operationDescriptorList.associateBy { descriptor ->
val operationId = operationIds.firstOrNull { it.name == descriptor.name } ?: error("No id found for operation ${descriptor.name}")
operationId.id
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ package apollo.plugin
import com.apollographql.apollo3.compiler.OperationOutputGenerator
import com.apollographql.apollo3.compiler.Plugin
import com.apollographql.apollo3.compiler.operationoutput.OperationDescriptor
import com.apollographql.apollo3.compiler.operationoutput.OperationOutput
import com.apollographql.apollo3.compiler.operationoutput.OperationId

class MyPlugin: Plugin {
override fun operationOutputGenerator(): OperationOutputGenerator {
return object : OperationOutputGenerator {
override fun generate(operationDescriptorList: Collection<OperationDescriptor>): OperationOutput {
return operationDescriptorList.map {
"${it.name}CustomId" to it
}.toMap()
}
}
override fun operationIds(descriptors: List<OperationDescriptor>): List<OperationId>? {
return descriptors.map { OperationId("${it.name}CustomId", it.name) }
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ apollo {
val name = it.name.replace("-", "")
service(name) {
packageName.set("hooks.$name")
plugin(project(":compiler-hooks-${it.name}"))
plugin(project(":compiler-plugins-${it.name}"))
languageVersion.set("1.5")
if (name == "gettersandsetters") {
generateKotlinModels.set(false)
Expand Down