Skip to content

Commit 0aedd8c

Browse files
authored
Add compile time possibleTypes (apollographql#5823)
1 parent 02dbe74 commit 0aedd8c

File tree

8 files changed

+80
-10
lines changed

8 files changed

+80
-10
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ internal object KotlinCodegen {
203203
builders.add(ObjectBuilder(context, irObject, generateDataBuilders))
204204
}
205205
if (generateSchema && context.resolver.resolve(ResolverKey(ResolverKeyKind.Schema, "")) == null) {
206-
builders.add(SchemaBuilder(context, irSchema.irObjects, irSchema.irInterfaces, irSchema.irUnions, irSchema.irEnums))
206+
builders.add(SchemaBuilder(context, codegenSchema.schema, irSchema.irObjects, irSchema.irInterfaces, irSchema.irUnions, irSchema.irEnums))
207207
builders.add(CustomScalarAdaptersBuilder(context, scalarMapping))
208208
}
209209

libraries/apollo-compiler/src/main/kotlin/com/apollographql/apollo3/compiler/codegen/kotlin/schema/SchemaBuilder.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.apollographql.apollo3.compiler.codegen.kotlin.schema
22

33

4+
import com.apollographql.apollo3.ast.Schema
45
import com.apollographql.apollo3.compiler.codegen.Identifier.type
56
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFile
67
import com.apollographql.apollo3.compiler.codegen.kotlin.CgFileBuilder
78
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSchemaContext
89
import com.apollographql.apollo3.compiler.codegen.kotlin.KotlinSymbols
910
import com.apollographql.apollo3.compiler.codegen.schemaSubPackageName
11+
import com.apollographql.apollo3.compiler.decapitalizeFirstLetter
1012
import com.apollographql.apollo3.compiler.ir.IrEnum
1113
import com.apollographql.apollo3.compiler.ir.IrInterface
1214
import com.apollographql.apollo3.compiler.ir.IrObject
@@ -18,9 +20,12 @@ import com.squareup.kotlinpoet.MemberName
1820
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
1921
import com.squareup.kotlinpoet.PropertySpec
2022
import com.squareup.kotlinpoet.TypeSpec
23+
import com.squareup.kotlinpoet.buildCodeBlock
24+
import com.squareup.kotlinpoet.joinToCode
2125

2226
internal class SchemaBuilder(
2327
private val context: KotlinSchemaContext,
28+
private val schema: Schema,
2429
private val objects: List<IrObject>,
2530
private val interfaces: List<IrInterface>,
2631
private val unions: List<IrUnion>,
@@ -65,6 +70,26 @@ internal class SchemaBuilder(
6570
.addKdoc("A __Schema object containing all the composite types and a possibleTypes helper function")
6671
.addProperty(typesPropertySpec())
6772
.addFunction(possibleTypesFunSpec())
73+
.apply {
74+
(interfaces + unions).forEach {
75+
addFunction(possibleTypesFunSpec(it.name, schema.possibleTypes(schema.typeDefinition(it.name))))
76+
}
77+
}
78+
.build()
79+
}
80+
81+
private fun possibleTypesFunSpec(name: String, possibleTypes: Set<String>): FunSpec {
82+
return FunSpec.builder("${name.decapitalizeFirstLetter()}PossibleTypes")
83+
.returns(KotlinSymbols.List.parameterizedBy(KotlinSymbols.ObjectType))
84+
.addCode(
85+
buildCodeBlock {
86+
add("return listOf(")
87+
add(possibleTypes.map {
88+
CodeBlock.of("%L.$type", context.resolver.resolveSchemaType(it))
89+
}.joinToCode(", "))
90+
add(")")
91+
}
92+
)
6893
.build()
6994
}
7095

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

Lines changed: 10 additions & 0 deletions
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/responseBased/data_builders/schema/__Schema.kt.expected

Lines changed: 10 additions & 0 deletions
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/measurements

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// If you updated the codegen and test fixtures, you should commit this file too.
33

44
Test: Total LOC:
5-
aggregate-all 202601
6-
aggregate-kotlin-responseBased 65109
7-
aggregate-kotlin-operationBased 41757
5+
aggregate-all 202649
6+
aggregate-kotlin-responseBased 65139
7+
aggregate-kotlin-operationBased 41775
88
aggregate-kotlin-compat 0
99
aggregate-java-operationBased 95735
1010

@@ -13,9 +13,9 @@ kotlin-operationBased-fragments_with_defer_and_include_directives
1313
java-operationBased-data_builders 3031
1414
java-operationBased-mutation_create_review 2435
1515
kotlin-responseBased-fragment_with_inline_fragment 2422
16-
kotlin-responseBased-data_builders 2355
16+
kotlin-responseBased-data_builders 2365
1717
java-operationBased-fragment_with_inline_fragment 2261
18-
kotlin-operationBased-data_builders 2004
18+
kotlin-operationBased-data_builders 2014
1919
java-operationBased-nested_named_fragments 1910
2020
java-operationBased-fragment_spread_with_include_directive 1667
2121
java-operationBased-union_inline_fragments 1666
@@ -27,7 +27,7 @@ java-operationBased-mutation_create_review_semantic_naming
2727
java-operationBased-root_query_fragment_with_nested_fragments 1607
2828
java-operationBased-simple_fragment 1505
2929
java-operationBased-named_fragment_delegate 1493
30-
kotlin-responseBased-mutation_create_review 1454
30+
kotlin-responseBased-mutation_create_review 1463
3131
java-operationBased-named_fragment_with_variables 1405
3232
kotlin-responseBased-named_fragment_delegate 1371
3333
kotlin-responseBased-unique_type_name 1354
@@ -42,7 +42,7 @@ kotlin-operationBased-nested_named_fragments
4242
kotlin-responseBased-inline_fragment_intersection 1245
4343
java-operationBased-two_heroes_with_friends 1241
4444
java-operationBased-inline_fragment_merge_fields 1229
45-
kotlin-responseBased-simple_fragment 1195
45+
kotlin-responseBased-simple_fragment 1203
4646
java-operationBased-named_fragment_inside_inline_fragment 1186
4747
java-operationBased-fragments_with_type_condition 1183
4848
java-operationBased-target_name 1168
@@ -69,7 +69,7 @@ kotlin-operationBased-root_query_fragment_with_nested_fragments
6969
java-operationBased-inline_fragments_with_friends 1056
7070
java-operationBased-fragment_spread_with_nested_fields 1054
7171
kotlin-responseBased-simple_fragment_with_inline_fragments 1052
72-
kotlin-operationBased-simple_fragment 1043
72+
kotlin-operationBased-simple_fragment 1051
7373
java-operationBased-operationbased2_ex8 1042
7474
kotlin-operationBased-named_fragment_delegate 1013
7575
java-operationBased-decapitalized_fields 993
@@ -233,7 +233,7 @@ kotlin-responseBased-starships
233233
kotlin-responseBased-java8annotation 403
234234
kotlin-responseBased-antlr_tokens 398
235235
kotlin-responseBased-subscriptions 391
236-
kotlin-responseBased-enums_as_sealed 361
236+
kotlin-responseBased-enums_as_sealed 364
237237
kotlin-responseBased-operation_id_generator 346
238238
kotlin-responseBased-big_query 344
239239
kotlin-responseBased-merged_include 344

libraries/apollo-compiler/src/test/graphql/com/example/mutation_create_review/kotlin/responseBased/mutation_create_review/schema/__Schema.kt.expected

Lines changed: 9 additions & 0 deletions
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/simple_fragment/kotlin/operationBased/simple_fragment/schema/__Schema.kt.expected

Lines changed: 8 additions & 0 deletions
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/simple_fragment/kotlin/responseBased/simple_fragment/schema/__Schema.kt.expected

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

0 commit comments

Comments
 (0)