Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Docs
Launch GraphOS Studio

Gradle plugin configuration


Apollo Kotlin's default configuration works for the majority of use cases. If you're getting started, see the getting started guide for an overview of the default Gradle configuration.

This article describes configuration options for advanced use cases when using Gradle.

Using multiple GraphQL APIs

Apollo Kotlin supports communicating with multiple GraphQL endpoints with different s. To do so, create multiple services like so:

apollo {
service("starwars") {
srcDir("src/main/graphql/starwars")
packageName.set("com.starwars")
}
service("githunt") {
srcDir("src/main/graphql/githunt")
packageName.set("com.githunt")
}
}

Specifying the schema location

Specify the location of your file using the schemaFile property:

apollo {
service("service") {
schemaFile.set(file("shared/graphql/schema.graphqls"))
}
}

By default, Apollo Kotlin combines all files in your project that match the pattern schema.[graphqls|json|sdl].

Combining multiple schema files

Apollo Kotlin supports a collection of client s, including @nonnull, @optional, and @typePolicy. These s enable you to extend your server's base with client-specific types and s.

If you expand your in a separate file, you can instruct Apollo Kotlin to construct its schema from a combination of multiple files, like so:

apollo {
service("service") {
schemaFiles.setFrom("shared/graphql/schema.graphqls", "shared/graphql/extra.graphqls")
}
}

By default, Apollo Kotlin combines all files in your project that match the pattern schema.[graphqls|json|sdl].

Wiring generated source

By default, Apollo Kotlin adds generated source:

  • to the main sourceSet for JVM projects
  • to commonMain for multiplatform projects
  • to all non-test s for Android projects

You can customize this behavior with the outputDirConnection property. For example, to wire a service to the test source set of a Kotlin JVM project:

apollo {
service("service") {
outputDirConnection {
connectToKotlinSourceSet("test")
}
}
}

Android variants support

It is sometimes useful to have different s or s depending on the variant of your Android project.

To do this, you can instruct the Gradle plugin to automatically configure a Service per , like so:

apollo {
createAllAndroidVariantServices(sourceFolder = ".", nameSuffix = "") {
// Configure the service here
packageName.set("...")
}
}
  • sourceFolder where to find the GraphQL relative to "src/$sourceSetName/graphql". Pass "." to look into "src/$sourceSetName/graphql".
  • nameSuffix a suffix to use for the service name. Leave blank to use the name as is.

Similarly to what the Android system does with source code, the GraphQL files are handled additively, and files in src/main/graphql are included in all services. This means your project could look like this (e.g. when certain s must only exist in debug builds):

- main
- graphql
- schema.graphqls // Schema for all variants
- operations.graphql // Operations shared by all variants
- debug
- graphql
- operations.graphql // Operations specific to the 'debug' build type

Or for instance like this (specific backend per flavor):

- main
- demo
- graphql
- schema.graphqls // Schema for the 'demo' flavor
- operations.graphql // Operations specific to the 'demo' flavor
- full
- graphql
- schema.graphqls // Schema for the 'full' flavor
- operations.graphql // Operations specific to the 'full' flavor

If you have many s and don't need to configure an Apollo Service for each one of them, it may be simpler to declare the Services manually, for instance:

apollo {
service("debug") {
srcDir(file("src/debug/graphql/"))
packageName.set("com.example")
outputDirConnection {
connectToAndroidSourceSet("debug")
}
}
service("release") {
srcDir(file("src/release/graphql/"))
packageName.set("com.example")
outputDirConnection {
connectToAndroidSourceSet("release")
}
}
}

Downloading a schema

By default, the Gradle plugin registers a downloadApolloSchema task that you can use from the command line:

# --schema is interpreted relative to the project's root directory (can also be an absolute path). This example
# assumes the root project directory and an Android app in `app`
./gradlew downloadApolloSchema \
--endpoint="https://your.domain/graphql/endpoint" \
--schema="app/src/main/graphql/com/example/schema.graphqls"

If you're doing this often or want to automate the process from CI, you can configure an introspection {} block:

apollo {
service("starwars") {
packageName.set("com.starwars")
// This will create a downloadStarwarsApolloSchemaFromIntrospection task
introspection {
endpointUrl.set("https://your.domain/graphql/endpoint")
// The path is interpreted relative to the current project here, no need to prepend 'app'
schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
}
}
}

This will create a task named download<ServiceName>ApolloSchemaFromIntrospection (downloadServiceApolloSchemaFromIntrospection by default).

If you register your with Apollo Studio, use the registry block instead:

apollo {
service("starwars") {
packageName.set("com.starwars")
// This will create a downloadStarwarsApolloSchemaFromRegistry task
registry {
key.set(System.getenv("APOLLO_KEY"))
graph.set(System.geten("APOLLO_GRAPH"))
// The path is interpreted relative to the current project here, no need to prepend 'app'
schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
}
}
}

This will create a task named download<ServiceName>ApolloSchemaFromRegistry (downloadServiceApolloSchemaFromRegistry by default).

Previous
Connecting source sets
Next
Queries
Edit on GitHubEditForumsDiscord