diff --git a/libraries/apollo-compiler/api/apollo-compiler.api b/libraries/apollo-compiler/api/apollo-compiler.api index 453640cc83a..b5774d601f2 100644 --- a/libraries/apollo-compiler/api/apollo-compiler.api +++ b/libraries/apollo-compiler/api/apollo-compiler.api @@ -60,13 +60,13 @@ public abstract interface class com/apollographql/apollo/compiler/ApolloCompiler public abstract interface class com/apollographql/apollo/compiler/ApolloCompilerRegistry { public abstract fun registerExecutableDocumentTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/ExecutableDocumentTransform;)V - public abstract fun registerExtraCodeGenerator (Lcom/apollographql/apollo/compiler/CodeGenerator;)V public abstract fun registerForeignSchemas (Ljava/util/List;)V public abstract fun registerIrTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/Transform;)V public abstract fun registerJavaOutputTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/Transform;)V public abstract fun registerKotlinOutputTransform (Ljava/lang/String;[Lcom/apollographql/apollo/compiler/Order;Lcom/apollographql/apollo/compiler/Transform;)V public abstract fun registerLayout (Lcom/apollographql/apollo/compiler/LayoutFactory;)V public abstract fun registerOperationIdsGenerator (Lcom/apollographql/apollo/compiler/OperationIdsGenerator;)V + public abstract fun registerSchemaCodeGenerator (Lcom/apollographql/apollo/compiler/SchemaCodeGenerator;)V } public final class com/apollographql/apollo/compiler/Before : com/apollographql/apollo/compiler/Order { @@ -74,10 +74,6 @@ public final class com/apollographql/apollo/compiler/Before : com/apollographql/ public final fun getId ()Ljava/lang/String; } -public abstract interface class com/apollographql/apollo/compiler/CodeGenerator { - public abstract fun generate (Lcom/apollographql/apollo/ast/GQLDocument;Ljava/io/File;)V -} - public final class com/apollographql/apollo/compiler/CodegenMetadata { public static final field Companion Lcom/apollographql/apollo/compiler/CodegenMetadata$Companion; public fun (Ljava/lang/String;Lcom/apollographql/apollo/compiler/TargetLanguage;Ljava/util/List;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;Ljava/util/Map;)V @@ -389,7 +385,7 @@ public final class com/apollographql/apollo/compiler/OperationIdGenerator$Sha256 } public abstract interface class com/apollographql/apollo/compiler/OperationIdsGenerator { - public abstract fun generate (Ljava/util/Collection;)Ljava/util/List; + public abstract fun generate (Ljava/util/List;)Ljava/util/List; } public abstract interface class com/apollographql/apollo/compiler/OperationOutputGenerator { @@ -452,6 +448,10 @@ public final class com/apollographql/apollo/compiler/RuntimeAdapterInitializer : public final fun serializer ()Lkotlinx/serialization/KSerializer; } +public abstract interface class com/apollographql/apollo/compiler/SchemaCodeGenerator { + public abstract fun generate (Lcom/apollographql/apollo/ast/GQLDocument;Ljava/io/File;)V +} + public abstract interface class com/apollographql/apollo/compiler/SchemaCodegenOpt { public abstract fun getGenerateSchema ()Ljava/lang/Boolean; public abstract fun getGeneratedSchemaName ()Ljava/lang/String; diff --git a/libraries/apollo-compiler/build.gradle.kts b/libraries/apollo-compiler/build.gradle.kts index cf2c44e2887..8521520e889 100644 --- a/libraries/apollo-compiler/build.gradle.kts +++ b/libraries/apollo-compiler/build.gradle.kts @@ -55,7 +55,8 @@ abstract class GeneratePluginVersion : DefaultTask() { versionFile.parentFile.mkdirs() versionFile.writeText("""// Generated file. Do not edit! package com.apollographql.apollo.compiler -const val APOLLO_VERSION = "${version.get()}" +@JvmField +val APOLLO_VERSION = "${version.get()}" """) } } diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt index 291b89d5c26..c72cb295385 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt @@ -5,7 +5,6 @@ import com.apollographql.apollo.ast.DifferentShape import com.apollographql.apollo.ast.DirectiveRedefinition import com.apollographql.apollo.ast.ForeignSchema import com.apollographql.apollo.ast.GQLDefinition -import com.apollographql.apollo.ast.GQLDirective import com.apollographql.apollo.ast.GQLDocument import com.apollographql.apollo.ast.GQLFragmentDefinition import com.apollographql.apollo.ast.GQLOperationDefinition @@ -15,7 +14,6 @@ import com.apollographql.apollo.ast.IncompatibleDefinition import com.apollographql.apollo.ast.Issue import com.apollographql.apollo.ast.ParserOptions import com.apollographql.apollo.ast.QueryDocumentMinifier -import com.apollographql.apollo.ast.Schema import com.apollographql.apollo.ast.UnknownDirective import com.apollographql.apollo.ast.UnusedFragment import com.apollographql.apollo.ast.UnusedVariable @@ -37,7 +35,7 @@ import com.apollographql.apollo.compiler.codegen.kotlin.KotlinCodegen import com.apollographql.apollo.compiler.codegen.kotlin.KotlinOutput import com.apollographql.apollo.compiler.codegen.kotlin.toSourceOutput import com.apollographql.apollo.compiler.codegen.plus -import com.apollographql.apollo.compiler.internal.addRequiredFields +import com.apollographql.apollo.compiler.internal.ApolloExecutableDocumentTransform import com.apollographql.apollo.compiler.internal.checkApolloInlineFragmentsHaveTypeCondition import com.apollographql.apollo.compiler.internal.checkApolloReservedEnumValueNames import com.apollographql.apollo.compiler.internal.checkApolloTargetNameClashes @@ -236,7 +234,6 @@ object ApolloCompiler { val warnOnDeprecatedUsages = options.warnOnDeprecatedUsages ?: defaultWarnOnDeprecatedUsages val failOnWarnings = options.failOnWarnings ?: defaultFailOnWarnings val fieldsOnDisjointTypesMustMerge = options.fieldsOnDisjointTypesMustMerge ?: defaultFieldsOnDisjointTypesMustMerge - val addTypename = options.addTypename ?: defaultAddTypename val generateOptionalOperationVariables = options.generateOptionalOperationVariables ?: defaultGenerateOptionalOperationVariables val alwaysGenerateTypesMatching = options.alwaysGenerateTypesMatching ?: defaultAlwaysGenerateTypesMatching @@ -264,45 +261,54 @@ object ApolloCompiler { /** * Step 3, Modify the AST to add typename and key fields */ - val fragmentDefinitions = (definitions.filterIsInstance() + upstreamFragmentDefinitions).associateBy { it.name } - var fragments = definitions.filterIsInstance().map { - addRequiredFields(it, addTypename, schema, fragmentDefinitions) + val hasCacheCompilerPlugin = try { + /** + * We have the cache compiler plugin in the class path. Do not do the work twice + */ + Class.forName("com.apollographql.cache.apollocompilerplugin.ApolloCacheCompilerPlugin") + true + } catch (_: ClassNotFoundException) { + false } - var operations = definitions.filterIsInstance().map { - var operation = addRequiredFields(it, addTypename, schema, fragmentDefinitions) - if (schema.directiveDefinitions.containsKey(Schema.DISABLE_ERROR_PROPAGATION) - && schema.schemaDefinition?.directives?.any { schema.originalDirectiveName(it.name) == Schema.CATCH_BY_DEFAULT } == true) { - operation = operation.copy( - directives = operation.directives + GQLDirective(null, Schema.DISABLE_ERROR_PROPAGATION, emptyList()) - ) - } - operation - } + var document = ApolloExecutableDocumentTransform(options.addTypename ?: defaultAddTypename, !hasCacheCompilerPlugin).transform( + schema = schema, + document = GQLDocument(definitions, sourceLocation = null), + upstreamFragmentDefinitions + ) if (documentTransform != null) { - val transformedDocument = documentTransform.transform(schema, GQLDocument(sourceLocation = null, definitions = fragments + operations), upstreamFragmentDefinitions) - fragments = transformedDocument.definitions.filterIsInstance() - operations = transformedDocument.definitions.filterIsInstance() + document = documentTransform.transform(schema, document, upstreamFragmentDefinitions) } // Remember the fragments with the possibly updated fragments - val allFragmentDefinitions = (fragments + upstreamFragmentDefinitions).associateBy { it.name } + val allFragmentDefinitions = (document.definitions.filterIsInstance() + upstreamFragmentDefinitions).associateBy { it.name } // Check if all the key fields are present in operations and fragments // (do this only if there are key fields as it may be costly) if (schema.hasTypeWithTypePolicy()) { - operations.forEach { - checkKeyFields(it, schema, allFragmentDefinitions) - } - fragments.forEach { - checkKeyFields(it, schema, allFragmentDefinitions) + document.definitions.forEach { + when(it) { + is GQLOperationDefinition -> checkKeyFields(it, schema, allFragmentDefinitions) + is GQLFragmentDefinition -> checkKeyFields(it, schema, allFragmentDefinitions) + else -> Unit + } } } /** * Build the IR */ + val operations = mutableListOf() + val fragments = mutableListOf() + document.definitions.forEach { + when(it) { + is GQLOperationDefinition -> operations.add(it) + is GQLFragmentDefinition -> fragments.add(it) + else -> Unit + } + } + return IrOperationsBuilder( schema = schema, operationDefinitions = operations, diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPlugin.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPlugin.kt index 718ad588899..a78b7af651a 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPlugin.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPlugin.kt @@ -14,14 +14,16 @@ import com.apollographql.apollo.compiler.ir.IrOperations import com.apollographql.apollo.compiler.operationoutput.OperationDescriptor import com.apollographql.apollo.compiler.operationoutput.OperationId import java.io.File +import kotlin.jvm.Throws /** * [ApolloCompilerPlugin] allows to customize the behavior of the Apollo Compiler. * - * [ApolloCompilerPlugin] may be instantiated several times in a codegen run. Each instance is create in a - * separate classloader. - * The classloaders contains `apollo-compiler` classes and the runtime classpath of the [ApolloCompilerPlugin]. - * You may throw from [ApolloCompilerPlugin] methods to fail the build. + * [ApolloCompilerPlugin] instances are created by [java.util.ServiceLoader] and may be instantiated several times in a codegen run. + * Each instance is created in a separate classloader and contains the `apollo-compiler` version loaded by your build tool. + * + * You may check [com.apollographql.apollo.compiler.APOLLO_VERSION] to check for compatibility of your + * plugin with the version of `apollo-compiler` available at runtime. */ interface ApolloCompilerPlugin { /** @@ -34,7 +36,6 @@ interface ApolloCompilerPlugin { * @param registry the registry where to register transformations. */ fun beforeCompilationStep(environment: ApolloCompilerPluginEnvironment, registry: ApolloCompilerRegistry) { - } /** @@ -61,6 +62,8 @@ interface ApolloCompilerPlugin { /** * @return the [Transform] to be applied to [JavaOutput] or null to use the default [Transform] */ + @Deprecated("Call `registry.registerJavaOutputTransform()` from beforeCompilationStep() instead.") + @ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) fun javaOutputTransform(): Transform? { return null } @@ -68,6 +71,8 @@ interface ApolloCompilerPlugin { /** * @return the [Transform] to be applied to [KotlinOutput] or null to use the default [Transform] */ + @Deprecated("Call `registry.registerKotlinOutputTransform()` from beforeCompilationStep() instead.") + @ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) fun kotlinOutputTransform(): Transform? { return null } @@ -75,7 +80,9 @@ interface ApolloCompilerPlugin { /** * @return a [DocumentTransform] to transform operations and/or fragments */ - @ApolloExperimental + @Suppress("DEPRECATION") + @Deprecated("Call `registry.registerExecutableDocumentTransform()` from beforeCompilationStep() instead.") + @ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) fun documentTransform(): DocumentTransform? { return null } @@ -83,7 +90,11 @@ interface ApolloCompilerPlugin { /** * @return the [Transform] to be applied to [IrOperations] or null to use the default [Transform] */ - @ApolloExperimental + /** + * @return a [DocumentTransform] to transform operations and/or fragments + */ + @Deprecated("Call `registry.registerIrTransform()` from beforeCompilationStep() instead.") + @ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) fun irOperationsTransform(): Transform? { return null } @@ -91,7 +102,8 @@ interface ApolloCompilerPlugin { /** * @return A list of [ForeignSchema] supported by this plugin */ - @ApolloExperimental + @Deprecated("Call `registry.registerForeignSchemas()` from beforeCompilationStep() instead.") + @ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) fun foreignSchemas(): List { return emptyList() } @@ -99,13 +111,16 @@ interface ApolloCompilerPlugin { /** * @return A [SchemaListener] called whenever the schema changed */ - @ApolloExperimental + @Suppress("DEPRECATION") + @Deprecated("Call `registry.registerSchemaCodeGenerator()` from beforeCompilationStep() instead.") + @ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) fun schemaListener(): SchemaListener? { return null } } -@ApolloExperimental +@Deprecated("Use CodeGenerator instead.") +@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) interface SchemaListener { /** * Called when the schema changed and codegen needs to be updated @@ -121,7 +136,8 @@ interface SchemaListener { /** * A [DocumentTransform] transforms operations and fragments at build time. [DocumentTransform] can add or remove fields automatically for an example. */ -@ApolloExperimental +@Deprecated("Use ExecutableDocumentTransform instead.") +@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) interface DocumentTransform { /** * Transforms the given operation. @@ -154,6 +170,7 @@ interface Transform { /** * A [ExecutableDocumentTransform] transforms operations and fragments at build time. [ExecutableDocumentTransform] can add or remove fields automatically, for an example. */ +@ApolloExperimental fun interface ExecutableDocumentTransform { /** * Transforms the given document. @@ -168,43 +185,68 @@ fun interface ExecutableDocumentTransform { fun transform(schema: Schema, document: GQLDocument, extraFragmentDefinitions: List): GQLDocument } +/** + * An [OperationIdsGenerator] is responsible for computing [OperationId] from [OperationDescriptor]. + * + * This is used for [persisted queries](https://www.apollographql.com/docs/kotlin/advanced/persisted-queries). + */ +@ApolloExperimental fun interface OperationIdsGenerator { - fun generate(operationDescriptorList: Collection): List + /** + * Generate the [OperationId]s from [operationDescriptors]. + * Implementations my throw to fail the build. + */ + @Throws + fun generate(operationDescriptors: List): List } +@ApolloExperimental sealed interface Order +@ApolloExperimental class Before(val id: String): Order + +@ApolloExperimental class After(val id: String): Order +/** + * A code generator that may write extra schema code in addition to what the Apollo Kotlin compiler is generating. + */ +@ApolloExperimental +fun interface SchemaCodeGenerator { + /** + * @param schema a [GQLDocument] representing the current schema. + * @param outputDirectory the directory where to write source files. + */ + fun generate(schema: GQLDocument, outputDirectory: File) +} + interface ApolloCompilerRegistry { + /** + * Registers an [OperationIdsGenerator]. + * + * Use this function implement [persisted queries](https://www.apollographql.com/docs/kotlin/advanced/persisted-queries). + */ + fun registerOperationIdsGenerator(generator: OperationIdsGenerator) + + @ApolloExperimental fun registerForeignSchemas(schemas: List) @ApolloExperimental fun registerExecutableDocumentTransform(id: String, vararg orders: Order, transform: ExecutableDocumentTransform) + @ApolloExperimental fun registerIrTransform(id: String, vararg orders: Order, transform: Transform) @ApolloExperimental fun registerLayout(factory: LayoutFactory) - fun registerOperationIdsGenerator(generator: OperationIdsGenerator) + @ApolloExperimental fun registerJavaOutputTransform(id: String, vararg orders: Order, transform: Transform) + @ApolloExperimental fun registerKotlinOutputTransform(id: String, vararg orders: Order, transform: Transform) - @ApolloExperimental - fun registerExtraCodeGenerator(codeGenerator: CodeGenerator) -} -/** - * A code generator that may write code in [ApolloCompilerPluginEnvironment.outputDirectory] - * - * This is not a kotlin function type because this might be used in environments where those types are - * relocated and might fail to load at runtime. For an example, in a Gradle plugin. - */ -fun interface CodeGenerator { - /** - * Transforms the given input into an output of the same type - */ - fun generate(schema: GQLDocument, outputDirectory: File) + @ApolloExperimental + fun registerSchemaCodeGenerator(schemaCodeGenerator: SchemaCodeGenerator) } \ No newline at end of file diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginProvider.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginProvider.kt index 1150c40a919..4608a5d6eb9 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginProvider.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginProvider.kt @@ -1,13 +1,14 @@ package com.apollographql.apollo.compiler -import com.apollographql.apollo.annotations.ApolloExperimental +import com.apollographql.apollo.annotations.ApolloDeprecatedSince /** * [ApolloCompilerPluginProvider] is entry point for creating [ApolloCompilerPlugin]. * * [ApolloCompilerPluginProvider] is created by [java.util.ServiceLoader], make sure to include a matching `META-INF/services` resource. */ -@ApolloExperimental +@Deprecated("Use ApolloCompilerPlugin directly.") +@ApolloDeprecatedSince(ApolloDeprecatedSince.Version.v4_2_1) fun interface ApolloCompilerPluginProvider { /** * Creates the [ApolloCompilerPlugin] diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/entrypoints.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/entrypoints.kt index 7b50f246873..f2042f6b43c 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/entrypoints.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/entrypoints.kt @@ -5,7 +5,6 @@ import com.apollographql.apollo.compiler.codegen.SchemaAndOperationsLayout import com.apollographql.apollo.compiler.codegen.writeTo import com.apollographql.apollo.compiler.internal.DefaultApolloCompilerRegistry import com.apollographql.apollo.compiler.internal.GradleCompilerPluginLogger -import com.apollographql.apollo.compiler.internal.LegacyOperationIdsGenerator import com.apollographql.apollo.compiler.operationoutput.OperationDescriptor import com.apollographql.apollo.compiler.operationoutput.OperationId import com.apollographql.apollo.compiler.operationoutput.OperationOutput @@ -215,9 +214,13 @@ internal fun apolloCompilerRegistry( registry.registerPlugin(it) } + @Suppress("DEPRECATION") val pluginProviders = ServiceLoader.load(ApolloCompilerPluginProvider::class.java, ApolloCompilerPluginProvider::class.java.classLoader).toList() pluginProviders.forEach { - println("Apollo: using ApolloCompilerPluginProvider is deprecated. Please use ApolloCompilerPlugin directly.") + // we make an exception for our own cache plugin because we want to display a nice error message to users before 4.3 + if (it.javaClass.name != "com.apollographql.cache.apollocompilerplugin.ApolloCacheCompilerPluginProvider") { + println("Apollo: using ApolloCompilerPluginProvider is deprecated. Please use ApolloCompilerPlugin directly.") + } hasPlugin = true val plugin = it.create(environment) plugin.beforeCompilationStep(environment, registry) diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/ApolloExecutableDocumentTransform.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/ApolloExecutableDocumentTransform.kt new file mode 100644 index 00000000000..3487d57dc06 --- /dev/null +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/ApolloExecutableDocumentTransform.kt @@ -0,0 +1,241 @@ +package com.apollographql.apollo.compiler.internal + +import com.apollographql.apollo.ast.GQLDirective +import com.apollographql.apollo.ast.GQLDocument +import com.apollographql.apollo.ast.GQLField +import com.apollographql.apollo.ast.GQLFragmentDefinition +import com.apollographql.apollo.ast.GQLFragmentSpread +import com.apollographql.apollo.ast.GQLInlineFragment +import com.apollographql.apollo.ast.GQLInterfaceTypeDefinition +import com.apollographql.apollo.ast.GQLNamedType +import com.apollographql.apollo.ast.GQLOperationDefinition +import com.apollographql.apollo.ast.GQLSelection +import com.apollographql.apollo.ast.GQLUnionTypeDefinition +import com.apollographql.apollo.ast.Schema +import com.apollographql.apollo.ast.definitionFromScope +import com.apollographql.apollo.ast.isAbstract +import com.apollographql.apollo.ast.rawType +import com.apollographql.apollo.ast.responseName +import com.apollographql.apollo.ast.rootTypeDefinition +import com.apollographql.apollo.compiler.ADD_TYPENAME_ALWAYS +import com.apollographql.apollo.compiler.ADD_TYPENAME_IF_ABSTRACT +import com.apollographql.apollo.compiler.ADD_TYPENAME_IF_FRAGMENTS +import com.apollographql.apollo.compiler.ADD_TYPENAME_IF_POLYMORPHIC +import com.apollographql.apollo.compiler.ExecutableDocumentTransform +import kotlin.collections.plus + +internal class ApolloExecutableDocumentTransform(private val addTypename: String, private val addKeyFields: Boolean) : + ExecutableDocumentTransform { + override fun transform( + schema: Schema, + document: GQLDocument, + extraFragmentDefinitions: List, + ): GQLDocument { + val userFragments = mutableListOf() + val userOperations = mutableListOf() + + document.definitions.forEach { + when (it) { + is GQLFragmentDefinition -> userFragments.add(it) + is GQLOperationDefinition -> userOperations.add(it) + else -> Unit + } + } + val fragmentDefinitions = (userFragments + extraFragmentDefinitions).associateBy { it.name } + val fragments = userFragments.map { + addRequiredFields(it, schema, fragmentDefinitions) + } + + val usesDisableErrorPropagation = schema.directiveDefinitions.containsKey(Schema.DISABLE_ERROR_PROPAGATION) + && schema.schemaDefinition?.directives?.any { schema.originalDirectiveName(it.name) == Schema.CATCH_BY_DEFAULT } == true + val operations = userOperations.map { + var operation = addRequiredFields(it, schema, fragmentDefinitions) + if (usesDisableErrorPropagation) { + operation = operation.copy( + directives = operation.directives + GQLDirective(null, Schema.DISABLE_ERROR_PROPAGATION, emptyList()) + ) + } + operation + } + + return document.copy( + definitions = operations + fragments + ) + } + + internal fun addRequiredFields( + operation: GQLOperationDefinition, + schema: Schema, + fragments: Map, + ): GQLOperationDefinition { + val parentType = operation.rootTypeDefinition(schema)!!.name + return operation.copy( + selections = operation.selections.addRequiredFields( + schema = schema, + fragments = fragments, + parentType = parentType, + parentFields = emptySet(), + isRoot = false, + ) + ) + } + + internal fun addRequiredFields( + fragmentDefinition: GQLFragmentDefinition, + schema: Schema, + fragments: Map, + ): GQLFragmentDefinition { + val newSelectionSet = fragmentDefinition.selections.addRequiredFields( + schema = schema, + fragments = fragments, + parentType = fragmentDefinition.typeCondition.name, + parentFields = emptySet(), + isRoot = true, + ) + + return fragmentDefinition.copy( + selections = newSelectionSet, + ) + } + + private fun List.isPolymorphic(schema: Schema, fragments: Map, rootType: String): Boolean { + return any { + when (it) { + is GQLField -> false + is GQLInlineFragment -> { + val tc = it.typeCondition?.name ?: rootType + !schema.isTypeASuperTypeOf(tc, rootType) || it.selections.isPolymorphic(schema, fragments, rootType) + } + + is GQLFragmentSpread -> { + val fragmentDefinition = fragments[it.name] ?: error("cannot find fragment ${it.name}") + /** + * If we were only looking at operationBased codegen, we wouldn't need to look inside fragment definitions but responseBased requires + * the __typename at the root of the field to determine the shape + */ + !schema.isTypeASuperTypeOf(fragmentDefinition.typeCondition.name, rootType) || fragmentDefinition.selections.isPolymorphic(schema, fragments, rootType) + } + } + } + } + + /** + * @param isRoot: whether this selection set is considered a valid root for adding __typename + * This is the case for field selection sets but also fragments since fragments can be executed from the cache + */ + private fun List.addRequiredFields( + schema: Schema, + fragments: Map, + parentType: String, + parentFields: Set, + isRoot: Boolean, + ): List { + if (isEmpty()) { + return this + } + + val selectionSet = this + + val requiresTypename = when (addTypename) { + ADD_TYPENAME_IF_POLYMORPHIC -> isRoot && isPolymorphic(schema, fragments, parentType) + ADD_TYPENAME_IF_FRAGMENTS -> { + selectionSet.any { it is GQLFragmentSpread || it is GQLInlineFragment } + } + + ADD_TYPENAME_IF_ABSTRACT -> isRoot && schema.typeDefinition(parentType).isAbstract() + ADD_TYPENAME_ALWAYS -> isRoot + else -> error("Unknown addTypename option: $addTypename") + } + val requiredFieldNames = if (addKeyFields) { + schema.keyFields(parentType).toMutableSet() + } else { + mutableSetOf() + } + + if (requiredFieldNames.isNotEmpty() || requiresTypename) { + requiredFieldNames.add("__typename") + } + + val fieldNames = parentFields + selectionSet.filterIsInstance().map { it.responseName() }.toSet() + + var newSelections = selectionSet.map { + when (it) { + is GQLInlineFragment -> { + it.copy( + selections = it.selections.addRequiredFields( + schema = schema, + fragments = fragments, + parentType = it.typeCondition?.name ?: parentType, + parentFields = fieldNames + requiredFieldNames, + isRoot = false + ) + ) + } + + is GQLFragmentSpread -> it + is GQLField -> it.addRequiredFields(schema, fragments, parentType) + } + } + + val fieldNamesToAdd = (requiredFieldNames - fieldNames).toMutableList() + if (requiresTypename) { + /** + * For "ifFragments", we add the typename always, even if it's already in parentFields + */ + fieldNamesToAdd.add("__typename") + } + + newSelections.filterIsInstance().forEach { + /** + * Verify that the fields we add won't overwrite an existing alias + * This is not 100% correct as this validation should be made more globally + */ + check(!fieldNamesToAdd.contains(it.alias)) { + "Field ${it.alias}: ${it.name} in $parentType conflicts with key fields" + } + } + + newSelections = newSelections + fieldNamesToAdd.map { buildField(it) } + + newSelections = if (requiresTypename) { + // remove the __typename if it exists + // and add it again at the top, so we're guaranteed to have it at the beginning of json parsing + // also remove any @include/@skip directive on __typename + listOf(buildField("__typename")) + newSelections.filter { (it as? GQLField)?.name != "__typename" } + } else { + newSelections + } + + return newSelections + } + + private fun GQLField.addRequiredFields( + schema: Schema, + fragments: Map, + parentType: String, + ): GQLField { + val typeDefinition = definitionFromScope(schema, parentType)!! + val newSelectionSet = selections.addRequiredFields( + schema = schema, + fragments = fragments, + parentType = typeDefinition.type.rawType().name, + parentFields = emptySet(), + isRoot = true + ) + + return copy( + selections = newSelectionSet, + ) + } + + private fun buildField(name: String): GQLField { + return GQLField( + name = name, + arguments = emptyList(), + selections = emptyList(), + sourceLocation = null, + directives = emptyList(), + alias = null, + ) + } +} \ No newline at end of file diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/DefaultApolloCompilerRegistry.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/DefaultApolloCompilerRegistry.kt index e45f43bd1cb..140b88b66a7 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/DefaultApolloCompilerRegistry.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/DefaultApolloCompilerRegistry.kt @@ -7,7 +7,7 @@ import com.apollographql.apollo.compiler.After import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerRegistry import com.apollographql.apollo.compiler.Before -import com.apollographql.apollo.compiler.CodeGenerator +import com.apollographql.apollo.compiler.SchemaCodeGenerator import com.apollographql.apollo.compiler.CodegenSchema import com.apollographql.apollo.compiler.LayoutFactory import com.apollographql.apollo.compiler.OperationIdsGenerator @@ -74,7 +74,7 @@ internal class DefaultApolloCompilerRegistry : ApolloCompilerRegistry { private val operationIdsGenerators = mutableListOf() private val javaOutputTransforms = mutableListOf>>() private val kotlinOutputTransforms = mutableListOf>>() - private val extraCodeGenerators = mutableListOf() + private val extraSchemaCodeGenerators = mutableListOf() @Suppress("DEPRECATION") fun registerPlugin(plugin: ApolloCompilerPlugin) { @@ -111,7 +111,7 @@ internal class DefaultApolloCompilerRegistry : ApolloCompilerRegistry { val schemaListener = plugin.schemaListener() if (schemaListener != null) { - error("Apollo: using ApolloCompilerPlugin.schemaListener() is deprecated. Please use registry.registerExtraCodeGenerator() from beforeCompilationStep() instead.") + error("Apollo: using ApolloCompilerPlugin.schemaListener() is deprecated. Please use registry.registerSchemaCodeGenerator() from beforeCompilationStep() instead.") } } @@ -159,8 +159,8 @@ internal class DefaultApolloCompilerRegistry : ApolloCompilerRegistry { kotlinOutputTransforms.add(Registration(id, transform, orders)) } - override fun registerExtraCodeGenerator(codeGenerator: CodeGenerator) { - extraCodeGenerators.add(codeGenerator) + override fun registerSchemaCodeGenerator(schemaCodeGenerator: SchemaCodeGenerator) { + extraSchemaCodeGenerators.add(schemaCodeGenerator) } fun foreignSchemas() = foreignSchemas @@ -220,7 +220,7 @@ internal class DefaultApolloCompilerRegistry : ApolloCompilerRegistry { return object : OperationOutputGenerator { override fun generate(operationDescriptorList: Collection): OperationOutput { val candidates = operationIdsGenerators.mapNotNull { - when (val operationIds = it.generate(operationDescriptorList)) { + when (val operationIds = it.generate(operationDescriptorList.toList())) { LegacyOperationIdsGenerator.NoList -> null else -> operationIds } @@ -240,9 +240,9 @@ internal class DefaultApolloCompilerRegistry : ApolloCompilerRegistry { } } - fun extraCodeGenerator(): CodeGenerator { - return CodeGenerator { document, outputDirectory -> - extraCodeGenerators.forEach { + fun extraCodeGenerator(): SchemaCodeGenerator { + return SchemaCodeGenerator { document, outputDirectory -> + extraSchemaCodeGenerators.forEach { it.generate(document, outputDirectory) } } diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/addRequiredFields.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/addRequiredFields.kt deleted file mode 100644 index d1c05a23a4b..00000000000 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/addRequiredFields.kt +++ /dev/null @@ -1,175 +0,0 @@ -package com.apollographql.apollo.compiler.internal - -import com.apollographql.apollo.ast.GQLField -import com.apollographql.apollo.ast.GQLFragmentDefinition -import com.apollographql.apollo.ast.GQLFragmentSpread -import com.apollographql.apollo.ast.GQLInlineFragment -import com.apollographql.apollo.ast.GQLOperationDefinition -import com.apollographql.apollo.ast.GQLSelection -import com.apollographql.apollo.ast.Schema -import com.apollographql.apollo.ast.definitionFromScope -import com.apollographql.apollo.ast.isAbstract -import com.apollographql.apollo.ast.rawType -import com.apollographql.apollo.ast.responseName -import com.apollographql.apollo.ast.rootTypeDefinition -import com.apollographql.apollo.compiler.ADD_TYPENAME_ALWAYS -import com.apollographql.apollo.compiler.ADD_TYPENAME_IF_ABSTRACT -import com.apollographql.apollo.compiler.ADD_TYPENAME_IF_FRAGMENTS -import com.apollographql.apollo.compiler.ADD_TYPENAME_IF_POLYMORPHIC - -internal fun addRequiredFields( - operation: GQLOperationDefinition, - addTypename: String, - schema: Schema, - fragments: Map, -): GQLOperationDefinition { - val parentType = operation.rootTypeDefinition(schema)!!.name - return operation.copy( - selections = operation.selections.addRequiredFields(schema, addTypename, fragments, parentType, emptySet(), false) - ) -} - -internal fun addRequiredFields( - fragmentDefinition: GQLFragmentDefinition, - addTypename: String, - schema: Schema, - fragments: Map, -): GQLFragmentDefinition { - val newSelectionSet = fragmentDefinition.selections.addRequiredFields(schema, addTypename, fragments, fragmentDefinition.typeCondition.name, emptySet(), true) - - return fragmentDefinition.copy( - selections = newSelectionSet, - ) -} - -private fun List.isPolymorphic(schema: Schema, fragments: Map, rootType: String): Boolean { - return any { - when (it) { - is GQLField -> false - is GQLInlineFragment -> { - val tc = it.typeCondition?.name ?: rootType - !schema.isTypeASuperTypeOf(tc, rootType) || it.selections.isPolymorphic(schema, fragments, rootType) - } - is GQLFragmentSpread -> { - val fragmentDefinition = fragments[it.name] ?: error("cannot find fragment ${it.name}") - /** - * If we were only looking at operationBased codegen, we wouldn't need to look inside fragment definitions but responseBased requires - * the __typename at the root of the field to determine the shape - */ - !schema.isTypeASuperTypeOf(fragmentDefinition.typeCondition.name, rootType) || fragmentDefinition.selections.isPolymorphic(schema, fragments, rootType) - } - } - } -} - -/** - * @param isRoot: whether this selection set is considered a valid root for adding __typename - * This is the case for field selection sets but also fragments since fragments can be executed from the cache - */ -private fun List.addRequiredFields( - schema: Schema, - addTypename: String, - fragments: Map, - parentType: String, - parentFields: Set, - isRoot: Boolean, -): List { - if (isEmpty()) { - return this - } - - val selectionSet = this - - val requiresTypename = when(addTypename) { - ADD_TYPENAME_IF_POLYMORPHIC -> isRoot && isPolymorphic(schema, fragments, parentType) - ADD_TYPENAME_IF_FRAGMENTS -> { - selectionSet.any { it is GQLFragmentSpread || it is GQLInlineFragment } - } - ADD_TYPENAME_IF_ABSTRACT -> isRoot && schema.typeDefinition(parentType).isAbstract() - ADD_TYPENAME_ALWAYS -> isRoot - else -> error("Unknown addTypename option: $addTypename") - } - val requiredFieldNames = schema.keyFields(parentType).toMutableSet() - - if (requiredFieldNames.isNotEmpty() || requiresTypename) { - requiredFieldNames.add("__typename") - } - - val fieldNames = parentFields + selectionSet.filterIsInstance().map { it.responseName() }.toSet() - - var newSelections = selectionSet.map { - when (it) { - is GQLInlineFragment -> { - it.copy( - selections = it.selections.addRequiredFields( - schema, - addTypename, - fragments, - it.typeCondition?.name ?: parentType, - fieldNames + requiredFieldNames, - false - ) - ) - } - is GQLFragmentSpread -> it - is GQLField -> it.addRequiredFields(schema, addTypename, fragments, parentType) - } - } - - val fieldNamesToAdd = (requiredFieldNames - fieldNames).toMutableList() - if (requiresTypename) { - /** - * For "ifFragments", we add the typename always, even if it's already in parentFields - */ - fieldNamesToAdd.add("__typename") - } - - newSelections.filterIsInstance().forEach { - /** - * Verify that the fields we add won't overwrite an existing alias - * This is not 100% correct as this validation should be made more globally - */ - check(!fieldNamesToAdd.contains(it.alias)) { - "Field ${it.alias}: ${it.name} in $parentType conflicts with key fields" - } - } - newSelections = newSelections + fieldNamesToAdd.map { buildField(it) } - - newSelections = if (requiresTypename) { - // remove the __typename if it exists - // and add it again at the top, so we're guaranteed to have it at the beginning of json parsing - // also remove any @include/@skip directive on __typename - listOf(buildField("__typename")) + newSelections.filter { (it as? GQLField)?.name != "__typename" } - } else { - newSelections - } - - return newSelections -} - -private fun GQLField.addRequiredFields(schema: Schema, addTypename: String, fragments: Map, parentType: String): GQLField { - val typeDefinition = definitionFromScope(schema, parentType)!! - val newSelectionSet = selections.addRequiredFields( - schema, - addTypename, - fragments, - typeDefinition.type.rawType().name, - emptySet(), - true - ) - - return copy( - selections = newSelectionSet, - ) -} - -private fun buildField(name: String): GQLField { - return GQLField( - name = name, - arguments = emptyList(), - selections = emptyList(), - sourceLocation = null, - directives = emptyList(), - alias = null, - ) -} diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/legacy.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/legacy.kt index e786ebca1aa..8875c1c88c1 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/legacy.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/internal/legacy.kt @@ -11,9 +11,9 @@ import com.apollographql.apollo.compiler.operationoutput.OperationDescriptor import com.apollographql.apollo.compiler.operationoutput.OperationId internal class LegacyOperationIdsGenerator(private val plugin: ApolloCompilerPlugin) : OperationIdsGenerator { - override fun generate(operationDescriptorList: Collection): List { + override fun generate(operationDescriptors: List): List { @Suppress("DEPRECATION") - val operationIds = plugin.operationIds(operationDescriptorList.toList()) + val operationIds = plugin.operationIds(operationDescriptors.toList()) if (operationIds != null) { println("Apollo: using ApolloCompiler.operationIds() is deprecated. Please use registry.registerOperationIdsGenerator() from beforeCompilationStep() instead.") return operationIds diff --git a/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/TypenameTest.kt b/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/TypenameTest.kt index ddc29ac962d..c1324f428be 100644 --- a/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/TypenameTest.kt +++ b/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/TypenameTest.kt @@ -1,14 +1,10 @@ package com.apollographql.apollo.compiler -import com.apollographql.apollo.ast.GQLDocument -import com.apollographql.apollo.ast.GQLFragmentDefinition -import com.apollographql.apollo.ast.GQLOperationDefinition import com.apollographql.apollo.ast.toExecutableDocument import com.apollographql.apollo.ast.toGQLDocument import com.apollographql.apollo.ast.toSchema import com.apollographql.apollo.ast.toUtf8 -import com.apollographql.apollo.ast.validateAsSchema -import com.apollographql.apollo.compiler.internal.addRequiredFields +import com.apollographql.apollo.compiler.internal.ApolloExecutableDocumentTransform import com.google.common.truth.Truth.assertThat import com.google.testing.junit.testparameterinjector.TestParameter import com.google.testing.junit.testparameterinjector.TestParameterInjector @@ -28,19 +24,9 @@ class TypenameTest( val schemaFile = File("src/test/graphql/schema.graphqls") val schema = schemaFile.toGQLDocument().toSchema() - val definitions = graphQLFile.source().buffer().toExecutableDocument(schema).definitions + val document = graphQLFile.source().buffer().toExecutableDocument(schema) - val fragments = definitions.filterIsInstance().associateBy { it.name } - val documentWithTypename = GQLDocument( - definitions = definitions.map { - when (it) { - is GQLOperationDefinition -> addRequiredFields(it, addTypename, schema, fragments) - is GQLFragmentDefinition -> addRequiredFields(it, addTypename, schema, fragments) - else -> it - } - }, - sourceLocation = null - ).toUtf8() + val documentWithTypename = ApolloExecutableDocumentTransform(addTypename, false).transform(schema, document, emptyList()).toUtf8() val extra = when(addTypename) { "ifFragments" -> "" // for backward compat diff --git a/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/keyfields/KeyFieldsTest.kt b/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/keyfields/KeyFieldsTest.kt index a3a08339a89..1673a1fb0f6 100644 --- a/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/keyfields/KeyFieldsTest.kt +++ b/libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/keyfields/KeyFieldsTest.kt @@ -2,50 +2,16 @@ package com.apollographql.apollo.compiler.keyfields import com.apollographql.apollo.ast.GQLDocument import com.apollographql.apollo.ast.GQLExecutableDefinition -import com.apollographql.apollo.ast.GQLFragmentDefinition -import com.apollographql.apollo.ast.GQLOperationDefinition import com.apollographql.apollo.ast.parseAsGQLDocument import com.apollographql.apollo.ast.toGQLDocument import com.apollographql.apollo.ast.validateAsSchemaAndAddApolloDefinition -import com.apollographql.apollo.compiler.internal.addRequiredFields -import com.apollographql.apollo.compiler.internal.checkKeyFields import okio.Path.Companion.toPath import kotlin.test.Test import kotlin.test.assertContains import kotlin.test.assertEquals import kotlin.test.assertTrue -import kotlin.test.fail class KeyFieldsTest { - @Test - fun testAddRequiredFields() { - val schema = "src/test/kotlin/com/apollographql/apollo/compiler/keyfields/schema.graphqls" - .toPath() - .toGQLDocument() - .validateAsSchemaAndAddApolloDefinition() - .getOrThrow() - - val definitions = "src/test/kotlin/com/apollographql/apollo/compiler/keyfields/operations.graphql".toPath() - .toGQLDocument() - .definitions - - val fragments = definitions.filterIsInstance().associateBy { it.name } - - val operation = definitions - .filterIsInstance() - .first() - - try { - checkKeyFields(operation, schema, emptyMap()) - fail("an exception was expected") - } catch (e: Exception) { - assertTrue(e.message?.contains("are not queried") == true) - } - - val operationWithKeyFields = addRequiredFields(operation, "ifFragments", schema, fragments) - checkKeyFields(operationWithKeyFields, schema, emptyMap()) - } - @Test fun testExtendInterfaceTypePolicyDirective() { val schema = "src/test/kotlin/com/apollographql/apollo/compiler/keyfields/extendsSchema.graphqls" diff --git a/tests/compiler-plugins/add-field/src/main/kotlin/hooks/TestPlugin.kt b/tests/compiler-plugins/add-field/src/main/kotlin/hooks/TestPlugin.kt index d88ff942166..7c6cfe3d84a 100644 --- a/tests/compiler-plugins/add-field/src/main/kotlin/hooks/TestPlugin.kt +++ b/tests/compiler-plugins/add-field/src/main/kotlin/hooks/TestPlugin.kt @@ -1,3 +1,5 @@ +@file:Suppress("DEPRECATION") + package hooks import com.apollographql.apollo.ast.GQLField diff --git a/tests/compiler-plugins/capitalize-enum-values/src/main/kotlin/hooks/TestPlugin.kt b/tests/compiler-plugins/capitalize-enum-values/src/main/kotlin/hooks/TestPlugin.kt index fbf5597f0d3..c74229cde0c 100644 --- a/tests/compiler-plugins/capitalize-enum-values/src/main/kotlin/hooks/TestPlugin.kt +++ b/tests/compiler-plugins/capitalize-enum-values/src/main/kotlin/hooks/TestPlugin.kt @@ -1,3 +1,5 @@ +@file:Suppress("DEPRECATION") + package hooks import com.apollographql.apollo.compiler.ApolloCompilerPlugin diff --git a/tests/compiler-plugins/custom-flatten/src/main/kotlin/hooks/TestPlugin.kt b/tests/compiler-plugins/custom-flatten/src/main/kotlin/hooks/TestPlugin.kt index b13ffa76180..bc301ab458e 100644 --- a/tests/compiler-plugins/custom-flatten/src/main/kotlin/hooks/TestPlugin.kt +++ b/tests/compiler-plugins/custom-flatten/src/main/kotlin/hooks/TestPlugin.kt @@ -1,3 +1,5 @@ +@file:Suppress("DEPRECATION") + package hooks import com.apollographql.apollo.compiler.ApolloCompilerPlugin diff --git a/tests/compiler-plugins/default-null-values/src/main/kotlin/hooks/TestPlugin.kt b/tests/compiler-plugins/default-null-values/src/main/kotlin/hooks/TestPlugin.kt index 4a2d5615dc4..e20ec2cffab 100644 --- a/tests/compiler-plugins/default-null-values/src/main/kotlin/hooks/TestPlugin.kt +++ b/tests/compiler-plugins/default-null-values/src/main/kotlin/hooks/TestPlugin.kt @@ -1,3 +1,5 @@ +@file:Suppress("DEPRECATION") + package hooks import com.apollographql.apollo.compiler.ApolloCompilerPlugin diff --git a/tests/compiler-plugins/getters-and-setters/src/main/kotlin/hooks/TestPlugin.kt b/tests/compiler-plugins/getters-and-setters/src/main/kotlin/hooks/TestPlugin.kt index 472a6cd9535..1f5924e5c8a 100644 --- a/tests/compiler-plugins/getters-and-setters/src/main/kotlin/hooks/TestPlugin.kt +++ b/tests/compiler-plugins/getters-and-setters/src/main/kotlin/hooks/TestPlugin.kt @@ -1,3 +1,5 @@ +@file:Suppress("DEPRECATION") + package hooks import com.apollographql.apollo.compiler.ApolloCompilerPlugin diff --git a/tests/compiler-plugins/prefix-names/src/main/kotlin/hooks/TestPlugin.kt b/tests/compiler-plugins/prefix-names/src/main/kotlin/hooks/TestPlugin.kt index 1999ad03255..b2bb7f0d33b 100644 --- a/tests/compiler-plugins/prefix-names/src/main/kotlin/hooks/TestPlugin.kt +++ b/tests/compiler-plugins/prefix-names/src/main/kotlin/hooks/TestPlugin.kt @@ -1,8 +1,9 @@ +@file:Suppress("DEPRECATION") + package hooks import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment -import com.apollographql.apollo.compiler.ApolloCompilerPluginLogger import com.apollographql.apollo.compiler.ApolloCompilerPluginProvider import com.apollographql.apollo.compiler.ApolloCompilerRegistry import com.apollographql.apollo.compiler.CodegenSchema diff --git a/tests/compiler-plugins/schema-codegen/src/main/kotlin/schema/TestPlugin.kt b/tests/compiler-plugins/schema-codegen/src/main/kotlin/schema/TestPlugin.kt index 67606f60e0f..1ff33120567 100644 --- a/tests/compiler-plugins/schema-codegen/src/main/kotlin/schema/TestPlugin.kt +++ b/tests/compiler-plugins/schema-codegen/src/main/kotlin/schema/TestPlugin.kt @@ -1,22 +1,21 @@ +@file:Suppress("DEPRECATION") + package schema import com.apollographql.apollo.ast.ForeignSchema import com.apollographql.apollo.ast.GQLIntValue import com.apollographql.apollo.ast.GQLTypeDefinition -import com.apollographql.apollo.ast.Schema import com.apollographql.apollo.ast.parseAsGQLDocument import com.apollographql.apollo.compiler.ApolloCompilerPlugin import com.apollographql.apollo.compiler.ApolloCompilerPluginEnvironment import com.apollographql.apollo.compiler.ApolloCompilerPluginLogger import com.apollographql.apollo.compiler.ApolloCompilerPluginProvider import com.apollographql.apollo.compiler.ApolloCompilerRegistry -import com.apollographql.apollo.compiler.SchemaListener import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.PropertySpec import okio.buffer import okio.source -import java.io.File class TestPlugin( logger: ApolloCompilerPluginLogger, @@ -39,7 +38,7 @@ class TestPlugin( ) )) - registry.registerExtraCodeGenerator { schema, outputDirectory -> + registry.registerSchemaCodeGenerator { schema, outputDirectory -> val maxAge = schema.definitions.filterIsInstance() .first { it.name == "Menu" } .directives diff --git a/tests/compiler-plugins/typename-interface/src/main/kotlin/hooks/TestPlugin.kt b/tests/compiler-plugins/typename-interface/src/main/kotlin/hooks/TestPlugin.kt index 1a622e6a2b2..73a773242b6 100644 --- a/tests/compiler-plugins/typename-interface/src/main/kotlin/hooks/TestPlugin.kt +++ b/tests/compiler-plugins/typename-interface/src/main/kotlin/hooks/TestPlugin.kt @@ -1,3 +1,5 @@ +@file:Suppress("DEPRECATION") + package hooks import com.apollographql.apollo.compiler.ApolloCompilerPlugin