apollo-kotlin icon indicating copy to clipboard operation
apollo-kotlin copied to clipboard

⚙️ Low level compiler APIs

Open martinbonnin opened this issue 3 years ago • 4 comments

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.

martinbonnin avatar Apr 13 '22 10:04 martinbonnin

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.

BoD avatar Apr 19 '22 10:04 BoD

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

martinbonnin avatar Apr 19 '22 12:04 martinbonnin

Another potential use case: the ability to make the generated types internal or not depending on custom logic (see #4334)

BoD avatar Aug 11 '22 17:08 BoD

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"
}

AnkitPatanaik avatar Aug 15 '22 17:08 AnkitPatanaik

This can also be used to add getters/setters in Java models (#4210)

BoD avatar Nov 03 '22 18:11 BoD

Also, https://github.com/apollographql/apollo-kotlin/issues/4253

BoD avatar Nov 03 '22 18:11 BoD

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!

BoD avatar Nov 04 '22 09:11 BoD

Available in 3.7.0

BoD avatar Nov 09 '22 08:11 BoD