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 c72cb295385..682139f57e3 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 @@ -1,5 +1,6 @@ package com.apollographql.apollo.compiler +import com.apollographql.apollo.annotations.ApolloExperimental import com.apollographql.apollo.ast.DeprecatedUsage import com.apollographql.apollo.ast.DifferentShape import com.apollographql.apollo.ast.DirectiveRedefinition @@ -605,6 +606,7 @@ fun Collection.toInputFiles(): List = map { InputFile(it, "") } internal fun T.maybeTransform(transform: Transform?) = transform?.transform(this) ?: this +@ApolloExperimental interface LayoutFactory { fun create(codegenSchema: CodegenSchema): SchemaAndOperationsLayout? } 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 a78b7af651a..6493d578751 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 @@ -160,6 +160,7 @@ interface DocumentTransform { * This is not a kotlin function type because this might be used in environment where those types are * relocated and might fail to load at runtime. For an example, in a Gradle plugin. */ +@ApolloExperimental interface Transform { /** * Transforms the given input into an output of the same type diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment.kt index 37361b0aaa9..67e939229f6 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment.kt @@ -1,11 +1,8 @@ package com.apollographql.apollo.compiler -import com.apollographql.apollo.annotations.ApolloExperimental - /** * [ApolloCompilerPluginEnvironment] contains the environment where the Apollo compiler is run. */ -@ApolloExperimental class ApolloCompilerPluginEnvironment( /** * @see [ApolloCompilerPluginValue] @@ -30,5 +27,4 @@ class ApolloCompilerPluginEnvironment( * - [List] * - [Map] */ -@ApolloExperimental typealias ApolloCompilerPluginValue = Any? \ No newline at end of file diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginLogger.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginLogger.kt index 5a5151e8cdb..c53bbbf06b4 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginLogger.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPluginLogger.kt @@ -1,13 +1,10 @@ package com.apollographql.apollo.compiler -import com.apollographql.apollo.annotations.ApolloExperimental - /** * [ApolloCompilerPluginLogger] allows logging from the context of the Apollo compiler. * * Typically, the Apollo compiler is run from an isolated classloader and cannot use the Gradle logging functions but can respect the logging level set by the user. */ -@ApolloExperimental interface ApolloCompilerPluginLogger { fun logging(message: String) fun info(message: String) diff --git a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout.kt b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout.kt index f1c5059ea60..d8e731d314a 100644 --- a/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout.kt +++ b/libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout.kt @@ -1,10 +1,14 @@ package com.apollographql.apollo.compiler.codegen +import com.apollographql.apollo.annotations.ApolloExperimental + +@ApolloExperimental interface CommonLayout { fun className(name: String): String fun propertyName(name: String): String } +@ApolloExperimental interface SchemaLayout : CommonLayout { fun schemaPackageName(): String fun schemaTypeName(schemaTypeName: String): String @@ -13,10 +17,12 @@ interface SchemaLayout : CommonLayout { fun paginationName(): String } +@ApolloExperimental interface OperationsLayout: CommonLayout { fun executableDocumentPackageName(filePath: String?): String fun operationName(name: String, capitalizedOperationType: String): String fun fragmentName(name: String): String } +@ApolloExperimental interface SchemaAndOperationsLayout : SchemaLayout, OperationsLayout 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 f2042f6b43c..4c279034daa 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 @@ -107,7 +107,7 @@ class EntryPoints { ).writeTo(outputDir, true, metadataOutputFile) if (upstreamCodegenMetadata.isEmpty()) { - registry.extraCodeGenerator().generate(codegenSchema.schema.toGQLDocument(), outputDir) + registry.schemaCodeGenerator().generate(codegenSchema.schema.toGQLDocument(), outputDir) } } @@ -156,7 +156,7 @@ class EntryPoints { operationManifestFile = operationManifestFile, ).writeTo(outputDir, true, null) - registry.extraCodeGenerator().generate(codegenSchema.schema.toGQLDocument(), outputDir) + registry.schemaCodeGenerator().generate(codegenSchema.schema.toGQLDocument(), outputDir) } } 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 140b88b66a7..982cc832b36 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 @@ -240,7 +240,7 @@ internal class DefaultApolloCompilerRegistry : ApolloCompilerRegistry { } } - fun extraCodeGenerator(): SchemaCodeGenerator { + fun schemaCodeGenerator(): SchemaCodeGenerator { return SchemaCodeGenerator { document, outputDirectory -> extraSchemaCodeGenerators.forEach { it.generate(document, outputDirectory) 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 1673a1fb0f6..02a945d76dc 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,16 +2,49 @@ 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.ApolloExecutableDocumentTransform +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 document = "src/test/kotlin/com/apollographql/apollo/compiler/keyfields/operations.graphql".toPath() + .toGQLDocument() + val definitions = document.definitions + + 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) + } + + // The document contains a single operation + val operationWithKeyFields = ApolloExecutableDocumentTransform("ifFragments", true).transform(schema, document, emptyList()).definitions.single() as GQLOperationDefinition + checkKeyFields(operationWithKeyFields, schema, emptyMap()) + } + @Test fun testExtendInterfaceTypePolicyDirective() { val schema = "src/test/kotlin/com/apollographql/apollo/compiler/keyfields/extendsSchema.graphqls"