apollo-kotlin
apollo-kotlin copied to clipboard
⚙️ Low level compiler APIs
It has come up a few times that tweaking the compiler output is desired. This issue is to brainstorm about possible extension points for the compiler and collect use cases.
- Allowing to set import aliases on
FileSpec.Builder: https://github.com/apollographql/apollo-kotlin/issues/4023 - Add prefixes to the generated classes: https://kotlinlang.slack.com/archives/C01A6KM1SBZ/p1649789764514569
- More?
One of the challenges is going to be how to expose that to Gradle. Because every Task input must be serializable, providing code is not easy. OperationOutputGenerator for an example uses a small workaround that the user has to manage a version by hand, which is not ideal for code that might change more often.
Another one is that this will certainly require keeping the KotlinPoet classes in the R8 configuration, which means it's more likely to clash with other buildscript dependencies.
Another potential use-case: have the generated models implement Serializable or Parcelable so they can easily be passed around in Intents, or in a navigation graph.
I don't think the Parcelable use case holds. Serializing a model usually requires some context (variables, scalar adapters, defer labels, etc...) so it's better addressed through serialization using fromJson/toJson instead of implementing Parcelable/Serializable
Another potential use case: the ability to make the generated types internal or not depending on custom logic (see #4334)
Another potential use case: Adding default values to Apollo Models w/o writing a custom directive
i.e.
query MyQuery {
name
nullable_name // this is a String? type
}
// response
{
name: "ankit",
nullable_name: null
}
// ideal response
{
name: "ankit",
nullable_name: "my_default_name"
}
This can also be used to add getters/setters in Java models (#4210)
Also, https://github.com/apollographql/apollo-kotlin/issues/4253
With #4210, hooks can now be set to intercept and manipulate KotlinPoet / JavaPoet builders before they are written to the disk. A few examples of using it are available here. This will be in the next release, in the meantime it can be tried out with the snapshots. The API is marked as experimental for now, feedback is appreciated!
Available in 3.7.0