You are viewing documentation for a previous version of this software.

Switch to the latest stable version.

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 schemas. To do so, create multiple services like so:

Kotlin
1apollo {
2  service("starwars") {
3    srcDir("src/main/graphql/starwars")
4    packageName.set("com.starwars")
5  }
6  service("githunt") {
7    srcDir("src/main/graphql/githunt")
8    packageName.set("com.githunt")
9  }
10}

Specifying the schema location

Specify the location of your schema file using the schemaFile property:

Kotlin
1apollo {
2  service("service") {
3    schemaFile.set(file("shared/graphql/schema.graphqls"))
4  }
5}

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 directives, including @nonnull, @optional, and @typePolicy. These directives enable you to extend your server's base schema with client-specific types and fields.

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

Kotlin
1apollo {
2  service("service") {
3    schemaFiles.setFrom("shared/graphql/schema.graphqls", "shared/graphql/extra.graphqls")
4  }
5}

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 variants 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:

Kotlin
1apollo {
2  service("service") {
3    outputDirConnection {
4      connectToKotlinSourceSet("test")
5    }
6  }
7}

Android variants support

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

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

Kotlin
1apollo {
2  createAllAndroidVariantServices(sourceFolder = ".", nameSuffix = "") {
3    // Configure the service here
4    packageName.set("...")
5  }
6}
  • 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 variant name as is.

Similarly to what the Android variant 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 operations must only exist in debug builds):

Text
1- main
2    - graphql
3        - schema.graphqls // Schema for all variants
4        - operations.graphql // Operations shared by all variants
5- debug
6    - graphql
7        - operations.graphql // Operations specific to the 'debug' build type

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

Text
1- main
2- demo
3    - graphql
4        - schema.graphqls // Schema for the 'demo' flavor
5        - operations.graphql // Operations specific to the 'demo' flavor
6- full
7    - graphql
8        - schema.graphqls // Schema for the 'full' flavor
9        - operations.graphql // Operations specific to the 'full' flavor

If you have many variants 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:

Kotlin
1apollo {
2  service("debug") {
3    srcDir(file("src/debug/graphql/"))
4    packageName.set("com.example")
5    outputDirConnection {
6      connectToAndroidSourceSet("debug")
7    }
8  }
9  service("release") {
10    srcDir(file("src/release/graphql/"))
11    packageName.set("com.example")
12    outputDirConnection {
13      connectToAndroidSourceSet("release")
14    }
15  }
16}

Downloading a schema

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

Bash
1# --schema is interpreted relative to the project's root directory (can also be an absolute path). This example
2# assumes the root project directory and an Android app in `app`
3./gradlew downloadApolloSchema \
4  --endpoint="https://your.domain/graphql/endpoint" \
5  --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:

Kotlin
1apollo {
2  service("starwars") {
3    packageName.set("com.starwars")
4
5    // This will create a downloadStarwarsApolloSchemaFromIntrospection task
6    introspection {
7      endpointUrl.set("https://your.domain/graphql/endpoint")
8      // The path is interpreted relative to the current project here, no need to prepend 'app'
9      schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
10    }
11  }
12}

This creates a task named download<ServiceName>ApolloSchemaFromIntrospection.

[!NOTE]
It's a different task from the previous downloadApolloSchema one. Make sure to call download<ServiceName>ApolloSchemaFromIntrospection

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

Kotlin
1apollo {
2  service("starwars") {
3    packageName.set("com.starwars")
4
5    // This will create a downloadStarwarsApolloSchemaFromRegistry task
6    registry {
7      key.set(System.getenv("APOLLO_KEY"))
8      graph.set(System.getenv("APOLLO_GRAPH"))
9      // The path is interpreted relative to the current project here, no need to prepend 'app'
10      schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
11    }
12  }
13}

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

Feedback

Edit on GitHub

Forums