Skip to content

Commit ae35a9b

Browse files
authored
Add schema-transform API (#6450)
* add schema-transform API * fix tests
1 parent a3c11be commit ae35a9b

File tree

15 files changed

+244
-8
lines changed

15 files changed

+244
-8
lines changed

libraries/apollo-compiler/api/apollo-compiler.api

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ public final class com/apollographql/apollo/compiler/AdapterInitializer$Companio
88

99
public final class com/apollographql/apollo/compiler/ApolloCompiler {
1010
public static final field INSTANCE Lcom/apollographql/apollo/compiler/ApolloCompiler;
11-
public final fun buildCodegenSchema (Ljava/util/List;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Lcom/apollographql/apollo/compiler/CodegenSchemaOptions;Ljava/util/List;)Lcom/apollographql/apollo/compiler/CodegenSchema;
11+
public final fun buildCodegenSchema (Ljava/util/List;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Lcom/apollographql/apollo/compiler/CodegenSchemaOptions;Ljava/util/List;Lcom/apollographql/apollo/compiler/SchemaTransform;)Lcom/apollographql/apollo/compiler/CodegenSchema;
1212
public final fun buildIrOperations (Lcom/apollographql/apollo/compiler/CodegenSchema;Ljava/util/List;Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/DocumentTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;)Lcom/apollographql/apollo/compiler/ir/IrOperations;
1313
public final fun buildSchemaAndOperationsSources (Lcom/apollographql/apollo/compiler/CodegenSchema;Ljava/util/List;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/LayoutFactory;Lcom/apollographql/apollo/compiler/OperationOutputGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/DocumentTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
14-
public final fun buildSchemaAndOperationsSources (Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo/compiler/CodegenSchemaOptions;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/LayoutFactory;Lcom/apollographql/apollo/compiler/OperationOutputGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/DocumentTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
14+
public final fun buildSchemaAndOperationsSources (Ljava/util/List;Ljava/util/List;Lcom/apollographql/apollo/compiler/CodegenSchemaOptions;Lcom/apollographql/apollo/compiler/IrOptions;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/LayoutFactory;Lcom/apollographql/apollo/compiler/OperationOutputGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/DocumentTransform;Lcom/apollographql/apollo/compiler/SchemaTransform;Lcom/apollographql/apollo/compiler/ApolloCompiler$Logger;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
1515
public final fun buildSchemaAndOperationsSourcesFromIr (Lcom/apollographql/apollo/compiler/CodegenSchema;Lcom/apollographql/apollo/compiler/ir/IrOperations;Lcom/apollographql/apollo/compiler/UsedCoordinates;Ljava/util/List;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout;Lcom/apollographql/apollo/compiler/OperationOutputGenerator;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;Ljava/io/File;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
1616
public final fun buildSchemaSources (Lcom/apollographql/apollo/compiler/CodegenSchema;Lcom/apollographql/apollo/compiler/UsedCoordinates;Lcom/apollographql/apollo/compiler/CodegenOptions;Lcom/apollographql/apollo/compiler/codegen/SchemaLayout;Lcom/apollographql/apollo/compiler/Transform;Lcom/apollographql/apollo/compiler/Transform;)Lcom/apollographql/apollo/compiler/codegen/SourceOutput;
1717
}
@@ -33,6 +33,7 @@ public abstract interface class com/apollographql/apollo/compiler/ApolloCompiler
3333
public fun layout (Lcom/apollographql/apollo/compiler/CodegenSchema;)Lcom/apollographql/apollo/compiler/codegen/SchemaAndOperationsLayout;
3434
public fun operationIds (Ljava/util/List;)Ljava/util/List;
3535
public fun schemaListener ()Lcom/apollographql/apollo/compiler/SchemaListener;
36+
public fun schemaTransform ()Lcom/apollographql/apollo/compiler/SchemaTransform;
3637
}
3738

3839
public final class com/apollographql/apollo/compiler/ApolloCompilerPluginEnvironment {
@@ -427,6 +428,10 @@ public abstract interface class com/apollographql/apollo/compiler/SchemaListener
427428
public abstract fun onSchema (Lcom/apollographql/apollo/ast/Schema;Ljava/io/File;)V
428429
}
429430

431+
public abstract interface class com/apollographql/apollo/compiler/SchemaTransform {
432+
public abstract fun transform (Lcom/apollographql/apollo/ast/GQLDocument;)Lcom/apollographql/apollo/ast/GQLDocument;
433+
}
434+
430435
public final class com/apollographql/apollo/compiler/StringsKt {
431436
public static final fun capitalizeFirstLetter (Ljava/lang/String;)Ljava/lang/String;
432437
public static final fun decapitalizeFirstLetter (Ljava/lang/String;)Ljava/lang/String;

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompiler.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ object ApolloCompiler {
6161
logger: Logger?,
6262
codegenSchemaOptions: CodegenSchemaOptions,
6363
foreignSchemas: List<ForeignSchema>,
64+
schemaTransform: SchemaTransform?,
6465
): CodegenSchema {
6566
val schemaDocuments = schemaFiles.map {
6667
it.normalizedPath to it.file.toGQLDocument(allowJson = true)
@@ -118,11 +119,15 @@ object ApolloCompiler {
118119
}
119120
val scalarExtensions = sdl.toGQLDocument().definitions
120121

121-
val schemaDocument = GQLDocument(
122+
var schemaDocument = GQLDocument(
122123
definitions = schemaDefinitions + scalarExtensions,
123124
sourceLocation = null
124125
)
125126

127+
if (schemaTransform != null) {
128+
schemaDocument = schemaTransform.transform(schemaDocument)
129+
}
130+
126131
val result = schemaDocument.validateAsSchema(
127132
validationOptions = SchemaValidationOptions(
128133
/**
@@ -475,14 +480,16 @@ object ApolloCompiler {
475480
javaOutputTransform: Transform<JavaOutput>?,
476481
kotlinOutputTransform: Transform<KotlinOutput>?,
477482
documentTransform: DocumentTransform?,
483+
schemaTransform: SchemaTransform?,
478484
logger: Logger?,
479485
operationManifestFile: File?,
480486
): SourceOutput {
481487
val codegenSchema = buildCodegenSchema(
482488
schemaFiles = schemaFiles,
483489
logger = logger,
484490
codegenSchemaOptions = codegenSchemaOptions,
485-
foreignSchemas = emptyList()
491+
foreignSchemas = emptyList(),
492+
schemaTransform = schemaTransform
486493
)
487494

488495
return buildSchemaAndOperationsSources(

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/ApolloCompilerPlugin.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ interface ApolloCompilerPlugin {
6262
return null
6363
}
6464

65+
/**
66+
* @return a [SchemaTransform] to transform the schema
67+
*/
68+
@ApolloExperimental
69+
fun schemaTransform(): SchemaTransform? {
70+
return null
71+
}
72+
73+
6574
/**
6675
* @return the [Transform] to be applied to [IrOperations] or null to use the default [Transform]
6776
*/
@@ -99,6 +108,15 @@ interface SchemaListener {
99108
fun onSchema(schema: Schema, outputDirectory: File)
100109
}
101110

111+
@ApolloExperimental
112+
interface SchemaTransform {
113+
/**
114+
* Transforms the given schema document.
115+
*
116+
* [transform] is called before validation of the schema.
117+
*/
118+
fun transform(schemaDocument: GQLDocument): GQLDocument
119+
}
102120

103121
/**
104122
* A [DocumentTransform] transforms operations and fragments at build time. [DocumentTransform] can add or remove fields automatically for an example.

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/entrypoints.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class EntryPoints {
4040
schemaFiles = normalizedSchemaFiles.toInputFiles(),
4141
logger = warning.toLogger(),
4242
codegenSchemaOptions = codegenSchemaOptionsFile.toCodegenSchemaOptions(),
43-
foreignSchemas = plugin?.foreignSchemas().orEmpty()
43+
foreignSchemas = plugin?.foreignSchemas().orEmpty(),
44+
schemaTransform = plugin?.schemaTransform()
4445
).writeTo(codegenSchemaFile)
4546
}
4647

@@ -133,7 +134,8 @@ class EntryPoints {
133134
schemaFiles = schemaFiles.toInputFiles(),
134135
codegenSchemaOptions = codegenSchemaOptions.toCodegenSchemaOptions(),
135136
foreignSchemas = plugin?.foreignSchemas().orEmpty(),
136-
logger = warning.toLogger()
137+
logger = warning.toLogger(),
138+
schemaTransform = plugin?.schemaTransform()
137139
)
138140

139141
ApolloCompiler.buildSchemaAndOperationsSources(

libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/CodegenTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ private fun ApolloCompiler.buildSchemaAndOperationsSourcesAndReturnIrOperations(
454454
schemaFiles = schemaFiles,
455455
logger = logger,
456456
codegenSchemaOptions = codegenSchemaOptions,
457-
foreignSchemas = emptyList()
457+
foreignSchemas = emptyList(),
458+
null
458459
)
459460

460461
val irOperations = buildIrOperations(

libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/MetadataTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class MetadataTest {
4545
logger = null,
4646
codegenSchemaOptions = codegenSchemaOptionsFile.toCodegenSchemaOptions(),
4747
foreignSchemas = emptyList(),
48+
null
4849
).writeTo(codegenSchemaFile)
4950

5051
ApolloCompiler.buildIrOperations(

libraries/apollo-compiler/src/test/kotlin/com/apollographql/apollo/compiler/conditionalFragments/ConditionalFragmentsTest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class ConditionalFragmentsTest {
3939
javaOutputTransform = null,
4040
kotlinOutputTransform = null,
4141
documentTransform = null,
42+
schemaTransform = null
4243
)
4344
}
4445

@@ -61,6 +62,7 @@ class ConditionalFragmentsTest {
6162
javaOutputTransform = null,
6263
kotlinOutputTransform = null,
6364
documentTransform = null,
65+
schemaTransform = null
6466
)
6567
}
6668
}

libraries/apollo-gradle-plugin-external/src/main/kotlin/com/apollographql/apollo/gradle/internal/ApolloGenerateSourcesTask.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ abstract class ApolloGenerateSourcesTask : ApolloGenerateSourcesBaseTask() {
5252
schemaFiles = schemaInputFiles,
5353
codegenSchemaOptions = codegenSchemaOptionsFile.get().asFile.toCodegenSchemaOptions(),
5454
foreignSchemas = emptyList(),
55-
logger = logger()
55+
logger = logger(),
56+
schemaTransform = null
5657
)
5758
ApolloCompiler.buildSchemaAndOperationsSources(
5859
codegenSchema = codegenSchema,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@file:OptIn(ApolloExperimental::class)
2+
3+
import com.apollographql.apollo.annotations.ApolloExperimental
4+
import com.apollographql.apollo.compiler.MODELS_RESPONSE_BASED
5+
6+
plugins {
7+
id("org.jetbrains.kotlin.jvm")
8+
id("com.apollographql.apollo")
9+
}
10+
11+
apolloTest()
12+
13+
dependencies {
14+
implementation(libs.apollo.runtime)
15+
testImplementation(libs.kotlin.test)
16+
testImplementation(libs.junit)
17+
}
18+
19+
apollo {
20+
service("service") {
21+
packageName.set("com.example")
22+
plugin(project(":schema-transform-plugin")) {}
23+
}
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
query GetAll {
2+
node {
3+
id
4+
}
5+
product {
6+
id
7+
price
8+
}
9+
review {
10+
id
11+
stars
12+
}
13+
}

0 commit comments

Comments
 (0)