Skip to content

Commit 2bfcd99

Browse files
authored
Do not compute key fields and key args when the Cache compiler plugin is present (#6797)
1 parent 31dd8a6 commit 2bfcd99

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

libraries/apollo-ast/src/commonMain/kotlin/com/apollographql/apollo/ast/internal/SchemaValidationScope.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,34 @@ import com.apollographql.apollo.ast.withBuiltinDefinitions
5353
* @param addKotlinLabsDefinitions automatically import the kotlin_labs definitions, even if no `@link` is present. If [excludeCacheDirectives] is `true`, cache related directives are excluded.
5454
* @param foreignSchemas a list of known [ForeignSchema] that may or may not be imported depending on the `@link` directives
5555
* @param excludeCacheDirectives whether to exclude cache related directives when auto-importing the kotlin_labs definitions. Has no effect if [addKotlinLabsDefinitions] is `false`.
56+
* @param computeKeyFields whether to compute cache key fields. Can be false when using the Apollo Cache compiler plugin to avoid unneeded computation.
5657
*/
5758
@ApolloExperimental
5859
class SchemaValidationOptions(
5960
val addKotlinLabsDefinitions: Boolean,
6061
val foreignSchemas: List<ForeignSchema>,
6162
val excludeCacheDirectives: Boolean,
63+
val computeKeyFields: Boolean,
6264
) {
65+
constructor(
66+
addKotlinLabsDefinitions: Boolean,
67+
foreignSchemas: List<ForeignSchema>,
68+
excludeCacheDirectives: Boolean,
69+
) : this(
70+
addKotlinLabsDefinitions = addKotlinLabsDefinitions,
71+
foreignSchemas = foreignSchemas,
72+
excludeCacheDirectives = excludeCacheDirectives,
73+
computeKeyFields = true,
74+
)
75+
6376
constructor(
6477
addKotlinLabsDefinitions: Boolean,
6578
foreignSchemas: List<ForeignSchema>,
6679
) : this(
6780
addKotlinLabsDefinitions = addKotlinLabsDefinitions,
6881
foreignSchemas = foreignSchemas,
6982
excludeCacheDirectives = false,
83+
computeKeyFields = true,
7084
)
7185
}
7286

@@ -339,7 +353,11 @@ internal fun validateSchema(definitions: List<GQLDefinition>, options: SchemaVal
339353
mergedScope.validateScalars()
340354
mergedScope.validateDirectiveDefinitions()
341355

342-
val keyFields = mergedScope.validateAndComputeKeyFields()
356+
val keyFields = if (options.computeKeyFields) {
357+
mergedScope.validateAndComputeKeyFields()
358+
} else {
359+
emptyMap()
360+
}
343361

344362
return GQLResult(
345363
Schema(

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,14 @@ object ApolloCompiler {
7070
/**
7171
* The Cache plugin starts providing the cache directives in v1.0.0-alpha.7
7272
*/
73-
private fun String.hasCacheDirectives(): Boolean {
73+
private val cacheCompilerPluginHasCacheDirectives: Boolean = cacheCompilerPluginVersion?.isAtLeastAlpha(7) == true
74+
75+
/**
76+
* The Cache plugin starts computing key fields and key args in v1.0.0-alpha.8
77+
*/
78+
private val cacheCompilerPluginComputesKeys: Boolean = cacheCompilerPluginVersion?.isAtLeastAlpha(8) == true
79+
80+
private fun String.isAtLeastAlpha(alpha: Int): Boolean {
7481
val parts = this.split('.', '-')
7582
val major = parts[0].toInt()
7683
if (major < 1) return false
@@ -83,7 +90,7 @@ object ApolloCompiler {
8390
val preRelease = parts[3]
8491
if (preRelease != "alpha") return true
8592
val alphaVersion = parts[4].toInt()
86-
return alphaVersion >= 7
93+
return alphaVersion >= alpha
8794
}
8895

8996
fun buildCodegenSchema(
@@ -177,7 +184,11 @@ object ApolloCompiler {
177184
/**
178185
* If the cache compiler plugin is present and provides the cache directives, don't automatically import the cache related directives to avoid a conflict
179186
*/
180-
excludeCacheDirectives = cacheCompilerPluginVersion?.hasCacheDirectives() == true,
187+
excludeCacheDirectives = cacheCompilerPluginHasCacheDirectives,
188+
/**
189+
* If the cache compiler plugin is present and computes key fields, don't compute them to save redundant work
190+
*/
191+
computeKeyFields = !cacheCompilerPluginComputesKeys,
181192
)
182193
)
183194

@@ -379,6 +390,7 @@ object ApolloCompiler {
379390
val irSchema = IrSchemaBuilder.build(
380391
schema = codegenSchema.schema,
381392
usedCoordinates = usedCoordinates,
393+
computeKeyArgs = !cacheCompilerPluginComputesKeys,
382394
)
383395

384396
val targetLanguage = defaultTargetLanguage(codegenOptions.targetLanguage, emptyList())

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ internal fun GQLInputObjectTypeDefinition.toIr(schema: Schema): IrInputObject {
297297
)
298298
}
299299

300-
internal fun GQLInterfaceTypeDefinition.toIr(schema: Schema, usedCoordinates: UsedCoordinates): IrInterface {
300+
internal fun GQLInterfaceTypeDefinition.toIr(schema: Schema, usedCoordinates: UsedCoordinates, computeKeyArgs: Boolean): IrInterface {
301301
return IrInterface(
302302
name = name,
303303
implements = implementsInterfaces,
@@ -309,14 +309,14 @@ internal fun GQLInterfaceTypeDefinition.toIr(schema: Schema, usedCoordinates: Us
309309
fieldDefinitions = this.fieldDefinitions(schema).filter {
310310
usedCoordinates.hasField(type = name, field = it.name)
311311
}.mapNotNull {
312-
it.toIrFieldDefinition(schema, name, usedCoordinates)
312+
it.toIrFieldDefinition(schema, name, usedCoordinates, computeKeyArgs)
313313
// Only include fields that have arguments used in operations
314314
.takeIf { it.argumentDefinitions.isNotEmpty() }
315315
},
316316
)
317317
}
318318

319-
internal fun GQLObjectTypeDefinition.toIr(schema: Schema, usedCoordinates: UsedCoordinates): IrObject {
319+
internal fun GQLObjectTypeDefinition.toIr(schema: Schema, usedCoordinates: UsedCoordinates, computeKeyArgs: Boolean): IrObject {
320320
return IrObject(
321321
name = name,
322322
implements = implementsInterfaces,
@@ -327,7 +327,7 @@ internal fun GQLObjectTypeDefinition.toIr(schema: Schema, usedCoordinates: UsedC
327327
fieldDefinitions = this.fieldDefinitions(schema).filter {
328328
usedCoordinates.hasField(type = name, field = it.name)
329329
}.mapNotNull {
330-
it.toIrFieldDefinition(schema, name, usedCoordinates)
330+
it.toIrFieldDefinition(schema, name, usedCoordinates, computeKeyArgs)
331331
// Only include fields that have arguments used in operations
332332
.takeIf { it.argumentDefinitions.isNotEmpty() }
333333
},
@@ -338,9 +338,10 @@ private fun GQLFieldDefinition.toIrFieldDefinition(
338338
schema: Schema,
339339
parentType: String,
340340
usedCoordinates: UsedCoordinates,
341+
computeKeyArgs: Boolean,
341342
): IrFieldDefinition {
342343
val typeDefinition = schema.typeDefinition(parentType)
343-
val keyArgs = typeDefinition.keyArgs(name, schema)
344+
val keyArgs = if (computeKeyArgs) typeDefinition.keyArgs(name, schema) else emptyList()
344345
return IrFieldDefinition(
345346
argumentDefinitions = arguments.mapNotNull {
346347
// We only include arguments that are used in operations

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal object IrSchemaBuilder {
1313
fun build(
1414
schema: Schema,
1515
usedCoordinates: UsedCoordinates,
16+
computeKeyArgs: Boolean,
1617
): IrSchema {
1718

1819
val irEnums = mutableListOf<IrEnum>()
@@ -65,10 +66,10 @@ internal object IrSchemaBuilder {
6566
irUnions.add(typeDefinition.toIr())
6667
}
6768
typeDefinition is GQLInterfaceTypeDefinition -> {
68-
irInterfaces.add(typeDefinition.toIr(schema, mergedUsedCoordinates))
69+
irInterfaces.add(typeDefinition.toIr(schema, mergedUsedCoordinates, computeKeyArgs))
6970
}
7071
typeDefinition is GQLObjectTypeDefinition -> {
71-
irObjects.add(typeDefinition.toIr(schema, mergedUsedCoordinates))
72+
irObjects.add(typeDefinition.toIr(schema, mergedUsedCoordinates, computeKeyArgs))
7273
}
7374
}
7475
}

0 commit comments

Comments
 (0)