Skip to content

Inline classes, fix using inline classes in input positions #6427

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

Merged
merged 5 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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-api/api/apollo-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,7 @@ public final class com/apollographql/apollo/api/OptionalAdapter : com/apollograp

public final class com/apollographql/apollo/api/OptionalKt {
public static final fun getOrElse (Lcom/apollographql/apollo/api/Optional;Ljava/lang/Object;)Ljava/lang/Object;
public static final fun map (Lcom/apollographql/apollo/api/Optional$Present;Lkotlin/jvm/functions/Function1;)Lcom/apollographql/apollo/api/Optional$Present;
public static final fun map (Lcom/apollographql/apollo/api/Optional;Lkotlin/jvm/functions/Function1;)Lcom/apollographql/apollo/api/Optional;
}

Expand Down
1 change: 1 addition & 0 deletions libraries/apollo-api/api/apollo-api.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,7 @@ final fun <#A: kotlin/Any> com.apollographql.apollo.api/and(kotlin/Array<out com
final fun <#A: kotlin/Any> com.apollographql.apollo.api/not(com.apollographql.apollo.api/BooleanExpression<#A>): com.apollographql.apollo.api/BooleanExpression<#A> // com.apollographql.apollo.api/not|not(com.apollographql.apollo.api.BooleanExpression<0:0>){0§<kotlin.Any>}[0]
final fun <#A: kotlin/Any> com.apollographql.apollo.api/or(kotlin/Array<out com.apollographql.apollo.api/BooleanExpression<#A>>...): com.apollographql.apollo.api/BooleanExpression<#A> // com.apollographql.apollo.api/or|or(kotlin.Array<out|com.apollographql.apollo.api.BooleanExpression<0:0>>...){0§<kotlin.Any>}[0]
final fun <#A: kotlin/Any?, #B: com.apollographql.apollo.api/ObjectBuilder<*>> com.apollographql.apollo.api/buildData(com.apollographql.apollo.api/BuilderFactory<#B>, kotlin/Function1<#B, kotlin/Unit>, com.apollographql.apollo.api/Adapter<#A>, kotlin.collections/List<com.apollographql.apollo.api/CompiledSelection>, kotlin/String, com.apollographql.apollo.api/FakeResolver, com.apollographql.apollo.api/CustomScalarAdapters): #A // com.apollographql.apollo.api/buildData|buildData(com.apollographql.apollo.api.BuilderFactory<0:1>;kotlin.Function1<0:1,kotlin.Unit>;com.apollographql.apollo.api.Adapter<0:0>;kotlin.collections.List<com.apollographql.apollo.api.CompiledSelection>;kotlin.String;com.apollographql.apollo.api.FakeResolver;com.apollographql.apollo.api.CustomScalarAdapters){0§<kotlin.Any?>;1§<com.apollographql.apollo.api.ObjectBuilder<*>>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (com.apollographql.apollo.api/Optional.Present<#A>).com.apollographql.apollo.api/map(kotlin/Function1<#A, #B>): com.apollographql.apollo.api/Optional.Present<#B> // com.apollographql.apollo.api/map|[email protected]<0:0>(kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (com.apollographql.apollo.api/Optional<#A>).com.apollographql.apollo.api/map(kotlin/Function1<#A, #B>): com.apollographql.apollo.api/Optional<#B> // com.apollographql.apollo.api/map|[email protected]<0:0>(kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/Adapter<#A>).com.apollographql.apollo.api/catchToNull(): com.apollographql.apollo.api/Adapter<#A?> // com.apollographql.apollo.api/catchToNull|[email protected]<0:0>(){0§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?> (com.apollographql.apollo.api/Adapter<#A>).com.apollographql.apollo.api/catchToResult(): com.apollographql.apollo.api/Adapter<com.apollographql.apollo.api/FieldResult<#A>> // com.apollographql.apollo.api/catchToResult|[email protected]<0:0>(){0§<kotlin.Any?>}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,8 @@ fun <V, R> Optional<V>.map(mapper: (V) -> R): Optional<R> {
is Present -> Optional.present(mapper(value))
}
}

@ApolloExperimental
fun <V, R> Present<V>.map(mapper: (V) -> R): Present<R> {
return Optional.present(mapper(value))
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,28 @@ internal class KotlinResolver(

internal fun unwrapInlineClass(type: IrType): CodeBlock {
val inlineClassProperty = resolveScalarInlineProperty(type.rawType().name)
if (inlineClassProperty == null) {
return CodeBlock.of("")
}

return buildCodeBlock {
when (type) {
is IrListType -> {
if (inlineClassProperty != null) {
if (type.nullable) {
add("?")
}
add(".map { it%L }", unwrapInlineClass(type.ofType))
when {
type.optional -> {
add(".%M { it%L }", KotlinSymbols.PresentMap, unwrapInlineClass(type.copyWith(optional = false)))
}
type is IrListType -> {
if (type.nullable) {
add("?")
}
add(".map { it%L }", unwrapInlineClass(type.ofType))
}

is IrScalarType -> {
if (inlineClassProperty != null) {
if (type.nullable) {
add("?")
}
add(".%L", inlineClassProperty)
type is IrScalarType -> {
if (type.nullable) {
add("?")
}
add(".%L", inlineClassProperty)
}

else -> Unit
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal object KotlinSymbols {
val Optional = ClassNames.Optional.toKotlinPoetClassName()
val Absent = ClassNames.Absent.toKotlinPoetClassName()
val Present = ClassNames.Present.toKotlinPoetClassName()
val PresentMap = MemberName(apolloApiPackageName, "map")
val Adapter = ClassNames.Adapter.toKotlinPoetClassName()
val CompiledSelection = ClassNames.CompiledSelection.toKotlinPoetClassName()
val CompiledNamedType = ClassNames.CompiledNamedType.toKotlinPoetClassName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,10 @@ private fun IrProperty.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
if (!isSynthetic) {
val adapterInitializer = context.resolver.adapterInitializer(info.type, requiresBuffering, context.jsExport)
builder.addStatement("${writer}.name(%S)", info.responseName)
builder.addStatement(
"%L.$toJson($writer, $customScalarAdapters, $value.%N%L)",
builder.addSerializeStatement(
adapterInitializer,
propertyName,
context.resolver.unwrapInlineClass(info.type),
context.resolver.unwrapInlineClass(info.type)
)
} else {
val adapterInitializer = context.resolver.resolveModelAdapter(info.type.modelPath())
Expand All @@ -237,7 +236,7 @@ private fun IrProperty.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
builder.beginControlFlow("if ($value.%N != null)", propertyName)
}
builder.addStatement(
"%L.$toJson($writer, $customScalarAdapters, $value.%N)",
"%T.$toJson($writer, $customScalarAdapters, $value.%N)",
adapterInitializer,
propertyName,
)
Expand All @@ -252,11 +251,13 @@ private fun IrProperty.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
internal fun CodeBlock.Builder.addSerializeStatement(
adapterInitializer: CodeBlock,
propertyName: String,
unwrapInlineClass: CodeBlock,
) {
addStatement(
"%L.$toJson($writer, ${customScalarAdapters}, $value.%N)",
"%L.$toJson($writer, $customScalarAdapters, $value.%N%L)",
adapterInitializer,
propertyName,
unwrapInlineClass
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ private fun NamedType.writeToResponseCodeBlock(context: KotlinContext): CodeBloc
builder.beginControlFlow("if ($value.%N is %T)", propertyName, KotlinSymbols.Present)
}
builder.addStatement("$writer.name(%S)", graphQlName)
builder.addSerializeStatement(adapterInitializer, propertyName)
builder.addSerializeStatement(
adapterInitializer,
propertyName,
context.resolver.unwrapInlineClass(this.type)
)
if (type.optional) {
builder.endControlFlow()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ private fun IrVariable.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
builder.beginControlFlow("if ($value.%N is %T)", propertyName, KotlinSymbols.Present)
}
builder.addStatement("$writer.name(%S)", name)
builder.addSerializeStatement(adapterInitializer, propertyName)
builder.addSerializeStatement(
adapterInitializer,
propertyName,
context.resolver.unwrapInlineClass(type)
)
if (type.optional) {
builder.endControlFlow()
if (defaultValue != null) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading