Docs
Launch GraphOS Studio

Gradle plugin configuration


'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

supports communicating with multiple endpoints with different schemas. 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 schema file using the schemaFile property:

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

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

Combining multiple schema files

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

If you expand your schema in a separate file, you can instruct 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, combines all files in your project that match the pattern schema.[graphqls|json|sdl].

Wiring generated source

By default, adds generated source:

  • to the main sourceSet for JVM projects
  • to commonMain for multiplatform projects
  • to all non-test 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 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 , like so:

apollo {
createAllAndroidVariantServices(sourceFolder = ".", nameSuffix = "") {
// Configure the service here
packageName.set("...")
}
}
  • sourceFolder where to find the 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 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 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 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 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:

apollo {
service("starwars") {
packageName.set("com.starwars")
// This will create a downloadStarwarsApolloSchemaFromRegistry task
registry {
key.set(System.getenv("APOLLO_KEY"))
graph.set(System.getenv("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

© 2024 Apollo Graph Inc.

Privacy Policy

Company