Docs
Launch GraphOS Studio

Multi Modules codegen


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

Note: this page is for sharing a between different modules and defining your .graphql s in different modules. If all your .graphql files are in a single module, you can use ApolloClient like any other Kotlin dependency without any of this.

Setup

Multi-modules requires that one and only one module contains a . 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 module, opt-in multi-module by generating metadata for use by downstream feature modules:

// schema/build.gradle.kts
apollo {
service("service") {
packageName.set("schema")
// enable generation of metadata for use by downstream modules
generateApolloMetadata.set(true)
// If you need to specify the location of your schema
schemaFile.set(/*...*/)
// define options that must be shared between all modules using this schema
mapScalar(/*...*/)
generateDataBuilders.set(true)
// more options...
}
}

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

// feature/build.gradle.kts
apollo {
// service names must match
service("service") {
packageName.set("feature")
// The 'feature' depends on the 'schema' module
dependsOn(project(":schema"))
}
}

Auto-detection of used types

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

For large s, 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, you need to add the "opposite" link by using isADependencyOf():

// schema/build.gradle.kts
apollo {
service("service") {
packageName.set("schema")
generateApolloMetadata.set(true)
// Get the used types from the downstream module
isADependencyOf(project(":feature1"))
// you can have several feature modules
isADependencyOf(project(":feature2"))
// ...
}
}
// feature1/build.gradle.kts
apollo {
service("service") {
packageName.set("feature")
// Get the generated schema types (and fragments) from the upstream schema module
dependsOn(project(":schema"))
}
}

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 classes.

Previous
Client Awareness
Next
Connecting source sets
Edit on GitHubEditForumsDiscord