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

[Codegen] Add CompiledArgumentDefinition #5797

Merged
merged 18 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libraries/apollo-compiler/api/apollo-compiler.api
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ public final class com/apollographql/apollo3/compiler/codegen/ResolverKey$Compan
}

public final class com/apollographql/apollo3/compiler/codegen/ResolverKeyKind : java/lang/Enum {
public static final field ArgumentDefinition Lcom/apollographql/apollo3/compiler/codegen/ResolverKeyKind;
public static final field BuilderFun Lcom/apollographql/apollo3/compiler/codegen/ResolverKeyKind;
public static final field BuilderType Lcom/apollographql/apollo3/compiler/codegen/ResolverKeyKind;
public static final field CustomScalarAdapters Lcom/apollographql/apollo3/compiler/codegen/ResolverKeyKind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum class ResolverKeyKind {
Schema,
CustomScalarAdapters,
Pagination,
ArgumentDefinition,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: we can use the coordinate to lookup the matching kotlin instead of adding a new kind each time.

}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ internal class JavaResolver(

fun registerSchema(className: ClassName) = register(ResolverKeyKind.Schema, "", className)
fun resolveSchema(): ClassName = resolveAndAssert(ResolverKeyKind.Schema, "")

fun registerArgumentDefinition(id: String, className: ClassName) = register(ResolverKeyKind.ArgumentDefinition, id, className)
fun resolveArgumentDefinition(id: String): ClassName = resolveAndAssert(ResolverKeyKind.ArgumentDefinition, id)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class CompiledSelectionsBuilder(
builder.add(".condition($L)", condition.toCompiledConditionInitializer())
}
if (arguments.isNotEmpty()) {
builder.add(".arguments($L)", arguments.sortedBy { it.name }.map { it.codeBlock() }.toListInitializerCodeblock())
builder.add(".arguments($L)", arguments.sortedBy { it.definitionId }.map { it.codeBlock() }.toListInitializerCodeblock())
}
if (selectionSetName != null) {
builder.add(".selections($L)", "__$selectionSetName")
Expand Down Expand Up @@ -120,11 +120,10 @@ internal class CompiledSelectionsBuilder(
private fun IrArgument.codeBlock(): CodeBlock {
val argumentBuilder = CodeBlock.builder()
argumentBuilder.add(
"new $T($T.${L}__$L)",
"new $T($T.$L)",
JavaClassNames.CompiledArgument,
context.resolver.resolveSchemaType(parentType),
parentField,
name,
context.resolver.resolveArgumentDefinition(definitionId),
definitionPropertyName,
)
if (this.value != null) {
argumentBuilder.add(".value($L)", value.codeBlock())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ internal class ObjectBuilder(

override fun prepare() {
context.resolver.registerSchemaType(obj.name, ClassName.get(packageName, simpleName))
for (fieldDefinition in obj.fieldDefinitions) {
fieldDefinition.argumentDefinitions.forEach { argumentDefinition ->
context.resolver.registerArgumentDefinition(argumentDefinition.id, ClassName.get(packageName, simpleName))
}
}
}

override fun build(): CodegenJavaFile {
Expand All @@ -54,7 +59,7 @@ internal fun List<IrFieldDefinition>.fieldSpecs(): List<FieldSpec> {
fieldDefinition.argumentDefinitions.map { argumentDefinition ->
FieldSpec.builder(
JavaClassNames.CompiledArgumentDefinition,
"${fieldDefinition.name}__${argumentDefinition.name}",
argumentDefinition.propertyName,
Modifier.PUBLIC,
Modifier.STATIC,
Modifier.FINAL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,7 @@ internal class KotlinResolver(
}

fun resolveCustomScalarAdapters(): ClassName = resolveAndAssert(ResolverKeyKind.CustomScalarAdapters, "")

fun registerArgumentDefinition(id: String, className: ClassName) = register(ResolverKeyKind.ArgumentDefinition, id, className)
fun resolveArgumentDefinition(id: String) = resolveAndAssert(ResolverKeyKind.ArgumentDefinition, id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ internal class CompiledSelectionsBuilder(
builder.add(".condition(%L)\n", condition.toCompiledConditionInitializer())
}
if (arguments.isNotEmpty()) {
builder.add(".arguments(%L)\n", arguments.sortedBy { it.name }.map { it.codeBlock() }.toListInitializerCodeblock(true))
builder.add(".arguments(%L)\n", arguments.sortedBy { it.definitionId }.map { it.codeBlock() }.toListInitializerCodeblock(true))
}
if (selectionSetName != null) {
builder.add(".selections(%N)\n", "__$selectionSetName")
Expand Down Expand Up @@ -125,11 +125,10 @@ internal class CompiledSelectionsBuilder(
private fun IrArgument.codeBlock(): CodeBlock {
val argumentBuilder = CodeBlock.builder()
argumentBuilder.add(
"%T(%T.%L__%L)",
"%T(%T.%L)",
KotlinSymbols.CompiledArgument,
context.resolver.resolveSchemaType(parentType),
parentField,
name,
context.resolver.resolveArgumentDefinition(definitionId),
definitionPropertyName,
)

if (this.value != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ internal class ObjectBuilder(
context.resolver.registerMapType(obj.name, ClassName(packageName, mapName))
context.resolver.registerBuilderType(obj.name, ClassName(packageName, builderName))
context.resolver.registerBuilderFun(obj.name, MemberName(packageName, "build${obj.name.capitalizeFirstLetter()}"))
for (fieldDefinition in obj.fieldDefinitions) {
fieldDefinition.argumentDefinitions.forEach { argumentDefinition ->
context.resolver.registerArgumentDefinition(argumentDefinition.id, ClassName(packageName, simpleName))
}
}
}

override fun build(): CgFile {
Expand Down Expand Up @@ -98,7 +103,7 @@ internal fun List<IrFieldDefinition>.propertySpecs(): List<PropertySpec> {
return flatMap { fieldDefinition ->
fieldDefinition.argumentDefinitions.map { argumentDefinition ->
PropertySpec.builder(
name = "${fieldDefinition.name}__${argumentDefinition.name}",
name = argumentDefinition.propertyName,
type = KotlinSymbols.CompiledArgumentDefinition,
)
.initializer(argumentDefinition.codeBlock())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ data class IrField(
@Serializable
@ApolloExperimental
data class IrArgument(
val parentType: String,
val parentField: String,
val name: String,
val definitionId: String,
val definitionPropertyName: String,
/**
* The value for this argument. May be null if the argument is absent
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ internal class IrCompositeType2(val name: String) : IrType2

@Serializable
internal data class IrArgumentDefinition(
val id: String,
val name: String,
val propertyName: String,
val isKey: Boolean,
val isPagination: Boolean,
)
) {
companion object {
fun id(type: String, field: String, argument: String) = "$type.$field.$argument"
fun propertyName(fieldName: String, argumentName: String) = "__${fieldName}_${argumentName}"
Comment on lines +133 to +134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For later: we should come up with some rules about symbols. Are __ to escape nameclashes or a signal that it is not meant to be called directly. It's not 100% clear at the moment

}
}

@Serializable
internal data class IrMapProperty(
Expand Down Expand Up @@ -322,6 +329,8 @@ private fun GQLFieldDefinition.toIrFieldDefinition(
null
} else {
IrArgumentDefinition(
id = IrArgumentDefinition.id(parentType, name, it.name),
propertyName = IrArgumentDefinition.propertyName(name, it.name),
name = it.name,
isKey = keyArgs.contains(it.name),
isPagination = paginationArgs.contains(it.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ internal class SelectionSetsBuilder(
val schemaArgument = fieldDefinition.arguments.first { it.name == fieldArgument.name }
val userValue = fieldArgument.value.coerceInExecutableContextOrThrow(schemaArgument.type, schema)
IrArgument(
parentType = parentType,
parentField = fieldDefinition.name,
name = schemaArgument.name,
definitionId = IrArgumentDefinition.id(parentType, fieldDefinition.name, schemaArgument.name),
definitionPropertyName = IrArgumentDefinition.propertyName(fieldDefinition.name, schemaArgument.name),
value = userValue.toIrValue(),
)
}
Expand Down