Here's your chance to speak at GraphQL Summit in New York City, October 8 - 10, 2024! 🏙️ Submit your proposal by May 31.
Launch GraphOS Studio
You're viewing documentation for an upcoming version of this software. Switch to the latest stable version.

Multi Modules codegen

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


This page is for sharing a schema between different modules and defining your `.` 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.


Multi-modules requires that one and only one module contains a schema. This is the schema that all other modules can reuse. In this , 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:

// schema/build.gradle.kts
apollo {
service("service") {
// enable generation of metadata for use by downstream modules
// If you need to specify the location of your schema files
// scalar mappings and generateDataBuilders must be defined in the schema module
// other options can be different between modules.
// if you want those options to be applied to all modules, use a convention plugin and shared build logic.

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

// feature/build.gradle.kts
apollo {
// service names must match
service("service") {
// The 'feature' depends on the 'schema' module

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 dependsOn() with the bidirectional set to true:

// feature/build.gradle.kts
apollo {
service("service") {
// Use `bidirectional` to have the schema module get the used types from this module
dependsOn(dependencyNotation = project(":schema"), bidirectional = true)

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.

Client Awareness
Connecting source sets
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy