wasyl
10/03/2025, 1:58 PMExecutableDocumentTransform that would add `id`s to selections on types that have id field in the schema, but it's not in the current selection set? I saw https://github.com/apollographql/apollo-kotlin/blob/415255a8d4203b2714924584a4e80f2283274769/tests/compiler-plugins/add-field/src/main/kotlin/hooks/TestPlugin.kt#L44 and it got me somewhere, but I'm struggling to get and pass around the type I'm currently processing (so that I can look up in schema whether it has an id field defined)wasyl
10/03/2025, 2:00 PMschema.typeDefinition and definitionFromScope really do. I have a vague idea what I want them to be doing, but when I try to get current GQLTypeDefinition it seems that I need to understand what types I can encounter and how to map them to the type defbod
10/03/2025, 2:05 PMit.definitionFromScope(schema, parentType) gives you the schema definition of the field from a field in a selection. From that you can get the GQLType of the fieldbod
10/03/2025, 2:06 PMid one)mbonnin
10/03/2025, 2:06 PMmbonnin
10/03/2025, 2:07 PMmbonnin
10/03/2025, 2:07 PMit.definitionFromScope(schema, parentType), this is the way currentlywasyl
10/03/2025, 2:08 PMit.definitionFromScope(schema, parentType) properlywasyl
10/03/2025, 2:08 PMwasyl
10/03/2025, 2:09 PMschema.typeDefinition(type.rawType().name) seems to work πmbonnin
10/03/2025, 2:09 PMmbonnin
10/03/2025, 2:11 PM{
node {
# parentType is Node
... on Product {
# parentType is Product
price
}
}
}mbonnin
10/03/2025, 2:11 PMwasyl
10/03/2025, 2:11 PMwasyl
10/03/2025, 2:12 PMit.typeCondition?.name ?: parentType? I wrote
is GQLInlineFragment -> it.copy(
selections = it.selections.addIdIfNeeded(
schema = schema,
parentType = it.typeCondition?.name?.let { schema.typeDefinition(it) } ?: parentType,
),
)mbonnin
10/03/2025, 2:12 PMmbonnin
10/03/2025, 2:13 PMThe inline fragments thingy isFrom a cursory look, that looks ok?it.typeCondition?.name ?: parentType
mbonnin
10/03/2025, 2:13 PMmbonnin
10/03/2025, 2:14 PM{
node {
# parentType is Node
... {
# anonymous fragment here
id
}
}
}wasyl
10/03/2025, 2:15 PMwasyl
10/03/2025, 2:16 PMadd IDs to types that have them was a built-in utility, shouldn't that be ultra common use case?)mbonnin
10/03/2025, 2:18 PM@typePolicy(keyFields: "id")mbonnin
10/03/2025, 2:18 PMmbonnin
10/03/2025, 2:19 PMapollo-ast thing needs higher level APIswasyl
10/03/2025, 2:20 PMThat was the idea behind @typePolicy(keyFields: "id")I thought this is to define which field is the ID for normalized cache? blob thinking fast What I'm trying to do is making sure we fetch
id field if it's defined in the schema, even if people don't write it themselves in the queriesmbonnin
10/03/2025, 2:21 PMmbonnin
10/03/2025, 2:21 PMmbonnin
10/03/2025, 2:21 PMbod
10/03/2025, 2:22 PMmbonnin
10/03/2025, 2:22 PMwasyl
10/03/2025, 2:22 PMwhen using @typePolicy, these ids are automatically added to selectionsexactly that wasn't clear to me π
wasyl
10/03/2025, 2:23 PM@typePolicy needs to be written on every type right?mbonnin
10/03/2025, 2:24 PMNode interface you can add it to the interface but if not, then yea, it becomes hard to maintainwasyl
10/03/2025, 2:26 PMNode πwasyl
10/03/2025, 2:26 PMmbonnin
10/03/2025, 2:26 PM@typePolicy(keyFields: "id") and then leave @typePolicy add the field automatically down the road, this is probably easier to write than the addRequiredIds()wasyl
10/03/2025, 2:27 PMregisterSchemaCodeGenerator?mbonnin
10/03/2025, 2:27 PMregisterSchemaTransformmbonnin
10/03/2025, 2:28 PMSchemaCodeGenerator happens later down the road to give users an opportunity to write some KotlinPoet code based on the schemawasyl
10/03/2025, 2:29 PMApolloCompilerPlugin and don't see the registerSchemaTransform. Old version maybe?mbonnin
10/03/2025, 2:29 PMmbonnin
10/03/2025, 2:29 PMmbonnin
10/03/2025, 2:30 PMmbonnin
10/03/2025, 2:30 PMwasyl
10/03/2025, 2:30 PMmbonnin
10/03/2025, 2:31 PMapollo-ast in your buildscript classpath but yea, it starts to become a lotmbonnin
10/03/2025, 2:34 PM@typePolicy is that we could potentially add the ids post codegen.
I think this could be beneficial because the user would only see the properties that they explicitely requested. No more relying on the compiler to add fields for you and break when you change the addTypename valuembonnin
10/03/2025, 2:35 PMwasyl
10/03/2025, 2:37 PMmbonnin
10/03/2025, 2:37 PMmbonnin
10/03/2025, 2:38 PMmbonnin
10/03/2025, 2:39 PMmbonnin
10/03/2025, 2:39 PMwasyl
10/03/2025, 2:41 PMmbonnin
10/03/2025, 2:41 PMmbonnin
10/03/2025, 2:43 PMI want my code to do what I see, reallyThat can be understood both way actually. if the compiler adds ids post codegen, then what you see (the
.graphql document) is not what's in the .kt filewasyl
10/03/2025, 2:47 PMmbonnin
10/03/2025, 2:57 PMwasyl
10/03/2025, 3:00 PM@typePolicy, would my plugin get the transformed schema already? I don't think so, so can I use @typePolicy for most of the fields and a plugin for the remaining ones?mbonnin
10/03/2025, 3:04 PMmbonnin
10/03/2025, 3:05 PMExecutableDocumentTransform gets the 'final' schema, after any SchemaDocumentTransformwasyl
10/03/2025, 3:42 PMmbonnin
10/03/2025, 3:53 PM