Multi Modules codegen


For multi-modules projects, Apollo Kotlin enables you to define queries in a feature module and reuse fragments and types from another module dependency. This helps with better separation of concerns and build times.

 note
This page is for sharing a schema between different modules and defining your .graphql operations in different modules. If all your .graphql files are in a single module, you can use apollo-runtime like any other Kotlin dependency without any of this.

Setup

Multi-modules requires that one and only one module contains a schema. This is the schema that all other modules can reuse. In this document, we'll refer to this module as the "schema module".

In your schema module, opt-in multi-module by generating metadata for use by downstream feature modules:

Kotlin
1// schema/build.gradle.kts
2apollo {
3  service("service") {
4    packageName.set("schema")
5    
6    // Enable generation of metadata for use by downstream modules 
7    generateApolloMetadata.set(true)
8    
9    // If you need to specify the location of your schema files
10    schemaFiles.from(/*...*/)
11    
12    // Scalar mappings and generateDataBuilders must be defined in the schema module
13    mapScalar(/*...*/)
14    generateDataBuilders.set(true)
15    
16    // Other options can be different between modules.
17    // If you want those options to be applied to all modules, use a convention plugin and shared build logic.
18    useSemanticNaming.set(true)
19    generateFragmentImplementations.set(true)
20  }
21}

In your feature module, declare your schema module as a dependency:

Kotlin
1// feature/build.gradle.kts
2apollo {
3  // Service names must match
4  service("service") {
5    packageName.set("feature")
6    
7    // The 'feature' depends on the 'schema' module
8    dependsOn(project(":schema"))
9  }
10}

Auto-detection of used types

By default, Apollo Kotlin generates all the types in your schema module. This is because there is no way to know in advance what types are going to be used by feature modules.

For large schemas, this can generate a lot of code and increase your build time significantly. In this case, you can opt in auto-detection of used types.

This works by splitting the codegen task in different steps so that feature modules can let the schema module know what types are used before actually generating the models. To opt in, call isADependencyOf() to establish the bidirectional dependency between your feature module and your schema module:

Kotlin
1// schema/build.gradle.kts
2apollo {
3  service("service") {
4    packageName.set("schema")
5    generateApolloMetadata.set(true)
6    
7    isADependencyOf(project(":feature"))
8    // list all your feature modules here
9    isADependencyOf(project(":feature2"))
10    // ...
11  }
12}

Once you opt in auto-detection of used types, it's important that all modules are doubly linked like above. If not the feature modules will fail to compile due to some missing schema classes.

bidirectional parameter (Experimental)

Using isADependencyOf() requires listing all your feature modules in your schema module which duplicates code. To keep things minimal and configure the dependency automatically, use bidirectional = true:

Kotlin
1// feature/build.gradle.kts
2apollo {
3  service("service") {
4    packageName.set("feature")
5
6    // Use `bidirectional` to have the schema module get the used types from this module
7    dependsOn(dependencyNotation = project(":schema"), bidirectional = true)
8  }
9}

Also disable generating types in the schema module:

Kotlin
1// schema/build.gradle.kts
2apollo {
3  service("service") {
4    packageName.set("schema")
5    generateApolloMetadata.set(true)
6    
7    // Disable generating all types 
8    alwaysGenerateTypesMatching.set(emptyList())
9  }
10}
The bidirectional parameter is experimental and not compatible with Gradle project isolation.
Feedback

Edit on GitHub

Forums