apollo-kotlin
apollo-kotlin copied to clipboard
Multi-module: better handling of duplicated types
When using multi-modules and the same GraphQL type is used in two sibling modules, the checkApolloDuplicates task fails with
duplicate Type '$Foo' generated in modules: feature1, feature2
Use 'alwaysGenerateTypesMatching' in a parent module to generate the type only once
This works but has 2 limitations:
- It's a manual process. One has to find the "good" parent module to add the type into and modify the Gradle file, invalidating the whole module build.
- because the check needs to be done across all modules, it configures and runs condegen in all modules who have the Apollo plugin which defeats module isolation.
One alternative is to generate all types in the schema module (alwaysGenerateTypesMatching.set(listOf(".*"))) but this increases codegen time and Kotlin compilation time too.
Proposed solution
Instead of using a Gradle optino, use a file to pass the types to generate. src/main/graphql/local.types would contain a list of types to generate locally:
Foo
SomeInput
SomeOtherType
...
Because this is not in Gradle scripts, this will only invalidate the GraphQL codegen when changed (which is expected).
And because this is in a file, we could have a global task that walks the whole module tree and generates these files:
# whenever a new used type is found, run this
./gradlew updateApolloLocalTypes
This way the cost of walking the module tree is only paid once during development and not on every build.
Questions:
- This doesn't prevent duplicate types to be generated. If one works on a single module and adds a new used type, the error only comes at the very end on Android when dexing (or never on the JVM and the classpath will have multiple versions of the same class). Maybe we should looks at it "the other way around" and have the schema module know about its "children" so it can update the local types accordingly...