Skip to content

Commit 04ba144

Browse files
authored
Inline classes, fix using inline classes in input positions (#6427)
* Add test * Fix using inline classes in input positions * upstate test fixtures * make the map overload experimental as well * update apiDump
1 parent d459af5 commit 04ba144

File tree

85 files changed

+294
-202
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+294
-202
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ public final class com/apollographql/apollo/api/OptionalAdapter : com/apollograp
857857

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

libraries/apollo-api/api/apollo-api.klib.api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,7 @@ final fun <#A: kotlin/Any> com.apollographql.apollo.api/and(kotlin/Array<out com
14421442
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]
14431443
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]
14441444
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]
1445+
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|map@com.apollographql.apollo.api.Optional.Present<0:0>(kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
14451446
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|map@com.apollographql.apollo.api.Optional<0:0>(kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
14461447
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|catchToNull@com.apollographql.apollo.api.Adapter<0:0>(){0§<kotlin.Any?>}[0]
14471448
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|catchToResult@com.apollographql.apollo.api.Adapter<0:0>(){0§<kotlin.Any?>}[0]

libraries/apollo-api/src/commonMain/kotlin/com/apollographql/apollo/api/Optional.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,8 @@ fun <V, R> Optional<V>.map(mapper: (V) -> R): Optional<R> {
5555
is Present -> Optional.present(mapper(value))
5656
}
5757
}
58+
59+
@ApolloExperimental
60+
fun <V, R> Present<V>.map(mapper: (V) -> R): Present<R> {
61+
return Optional.present(mapper(value))
62+
}

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -266,26 +266,28 @@ internal class KotlinResolver(
266266

267267
internal fun unwrapInlineClass(type: IrType): CodeBlock {
268268
val inlineClassProperty = resolveScalarInlineProperty(type.rawType().name)
269+
if (inlineClassProperty == null) {
270+
return CodeBlock.of("")
271+
}
272+
269273
return buildCodeBlock {
270-
when (type) {
271-
is IrListType -> {
272-
if (inlineClassProperty != null) {
273-
if (type.nullable) {
274-
add("?")
275-
}
276-
add(".map { it%L }", unwrapInlineClass(type.ofType))
274+
when {
275+
type.optional -> {
276+
add(".%M { it%L }", KotlinSymbols.PresentMap, unwrapInlineClass(type.copyWith(optional = false)))
277+
}
278+
type is IrListType -> {
279+
if (type.nullable) {
280+
add("?")
277281
}
282+
add(".map { it%L }", unwrapInlineClass(type.ofType))
278283
}
279284

280-
is IrScalarType -> {
281-
if (inlineClassProperty != null) {
282-
if (type.nullable) {
283-
add("?")
284-
}
285-
add(".%L", inlineClassProperty)
285+
type is IrScalarType -> {
286+
if (type.nullable) {
287+
add("?")
286288
}
289+
add(".%L", inlineClassProperty)
287290
}
288-
289291
else -> Unit
290292
}
291293
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ internal object KotlinSymbols {
3434
val Optional = ClassNames.Optional.toKotlinPoetClassName()
3535
val Absent = ClassNames.Absent.toKotlinPoetClassName()
3636
val Present = ClassNames.Present.toKotlinPoetClassName()
37+
val PresentMap = MemberName(apolloApiPackageName, "map")
3738
val Adapter = ClassNames.Adapter.toKotlinPoetClassName()
3839
val CompiledSelection = ClassNames.CompiledSelection.toKotlinPoetClassName()
3940
val CompiledNamedType = ClassNames.CompiledNamedType.toKotlinPoetClassName()

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/kotlin/helpers/AdapterCommon.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,10 @@ private fun IrProperty.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
221221
if (!isSynthetic) {
222222
val adapterInitializer = context.resolver.adapterInitializer(info.type, requiresBuffering, context.jsExport)
223223
builder.addStatement("${writer}.name(%S)", info.responseName)
224-
builder.addStatement(
225-
"%L.$toJson($writer, $customScalarAdapters, $value.%N%L)",
224+
builder.addSerializeStatement(
226225
adapterInitializer,
227226
propertyName,
228-
context.resolver.unwrapInlineClass(info.type),
227+
context.resolver.unwrapInlineClass(info.type)
229228
)
230229
} else {
231230
val adapterInitializer = context.resolver.resolveModelAdapter(info.type.modelPath())
@@ -237,7 +236,7 @@ private fun IrProperty.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
237236
builder.beginControlFlow("if ($value.%N != null)", propertyName)
238237
}
239238
builder.addStatement(
240-
"%L.$toJson($writer, $customScalarAdapters, $value.%N)",
239+
"%T.$toJson($writer, $customScalarAdapters, $value.%N)",
241240
adapterInitializer,
242241
propertyName,
243242
)
@@ -252,11 +251,13 @@ private fun IrProperty.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
252251
internal fun CodeBlock.Builder.addSerializeStatement(
253252
adapterInitializer: CodeBlock,
254253
propertyName: String,
254+
unwrapInlineClass: CodeBlock,
255255
) {
256256
addStatement(
257-
"%L.$toJson($writer, ${customScalarAdapters}, $value.%N)",
257+
"%L.$toJson($writer, $customScalarAdapters, $value.%N%L)",
258258
adapterInitializer,
259259
propertyName,
260+
unwrapInlineClass
260261
)
261262
}
262263

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/kotlin/helpers/InputAdapter.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ private fun NamedType.writeToResponseCodeBlock(context: KotlinContext): CodeBloc
8181
builder.beginControlFlow("if ($value.%N is %T)", propertyName, KotlinSymbols.Present)
8282
}
8383
builder.addStatement("$writer.name(%S)", graphQlName)
84-
builder.addSerializeStatement(adapterInitializer, propertyName)
84+
builder.addSerializeStatement(
85+
adapterInitializer,
86+
propertyName,
87+
context.resolver.unwrapInlineClass(this.type)
88+
)
8589
if (type.optional) {
8690
builder.endControlFlow()
8791
}

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/codegen/kotlin/operations/util/VariablesAdapter.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ private fun IrVariable.writeToResponseCodeBlock(context: KotlinContext): CodeBlo
5858
builder.beginControlFlow("if ($value.%N is %T)", propertyName, KotlinSymbols.Present)
5959
}
6060
builder.addStatement("$writer.name(%S)", name)
61-
builder.addSerializeStatement(adapterInitializer, propertyName)
61+
builder.addSerializeStatement(
62+
adapterInitializer,
63+
propertyName,
64+
context.resolver.unwrapInlineClass(type)
65+
)
6266
if (type.optional) {
6367
builder.endControlFlow()
6468
if (defaultValue != null) {

libraries/apollo-compiler/src/test/graphql/com/example/capitalized_fields/kotlin/operationBased/capitalized_fields/adapter/TestQuery_ResponseAdapter.kt.expected

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libraries/apollo-compiler/src/test/graphql/com/example/data_builders/kotlin/operationBased/data_builders/adapter/AnimalQuery_ResponseAdapter.kt.expected

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)