Join us from October 8-10 in New York City to learn the latest tips, trends, and news about GraphQL Federation and API platform engineering.Join us for GraphQL Summit 2024 in NYC
Docs
Start for Free

Gradle plugin recipes


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 schemaFiles property:

apollo {
service("service") {
schemaFiles.from(file("shared/graphql/schema.graphqls"))
}
}

NOTE

If schemaFiles is not set, Apollo Kotlin combines all *.[graphqls|json|sdl] files in src/main/graphql/ (Android/JVM projects) or src/commonMain/graphql/ (multiplatform projects).

Combining multiple schema files

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

If you expand your schema in a separate file (usually named extra.graphqls), you can instruct Apollo Kotlin to construct its schema from a combination of multiple files, like so:

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

Wiring generated sources

By default, Apollo Kotlin adds the generated sources:

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

apollo {
createAllAndroidVariantServices(sourceFolder = ".", nameSuffix = "") {
// Configure the service here
packageName.set("...")
}
}
  • sourceFolder is where to find the GraphQL relative to "src/$sourceSetName/graphql". Pass "." to look into "src/$sourceSetName/graphql".
  • nameSuffix is the 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.

For instance, if certain operations should only exist in debug builds, your project structure could look like this:

- 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 if you have a specific backend per flavor, it could look like this:

- 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 a lot of variants and don't need to configure an Apollo Service for each, it may be simpler to declare the Services manually rather than using createAllAndroidVariantServices. 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

The Apollo Gradle plugin has APIs to download a schema from :

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

This creates a task named download${ServiceName}ApolloSchemaFromIntrospection.

If introspection is disabled and your team is using GraphOS, use the registry {} block instead:

apollo {
service("starwars") {
packageName.set("com.starwars")
// This creates 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 creates a task named download${ServiceName}ApolloSchemaFromRegistry.

With the Android Studio plugin, you can also go to Tools | Apollo | Download schema which acts as a shortcut to these tasks.

Alternatively, for one time downloads, you can also use the Apollo Kotlin cli.

Generated methods

By default, all Kotlin models, operations, , and input objects are generated as data classes. This means that the Kotlin compiler will auto-generate toString, equals hashCode, copy and componentN for most generated classes. If you don't think you need all of those auto-generated methods, and/or you are worried about the size of the generated code, you can configure the Apollo compiler to generate none or a subset of the data class methods. To do this, set generateMethods to a list of the methods you need. The available methods are:

  • "equalsHashCode" generates equals and hashCode methods that will compare generated class properties.
  • "toString" generates a method that will print a pretty string representing the data in the class.
  • "copy" (Kotlin only) generates a method that will copy the class with named parameters and default values.
  • "dataClass" (Kotlin only and redundant with all other methods) generates the class as a data class. which will automatically generate toString, copy, equals and hashCode.

Here are some possible configurations:

apollo {
service("service") {
// Generates equals and hashCode
generateMethods.set(listOf("equalsHashCode"))
// Generates toString, equals, and hashcode (the default for Java)
generateMethods.set(listOf("equalsHashCode", "toString"))
// Only generates copy
generateMethods.set(listOf("copy"))
// Generates data classes (the default for Kotlin)
generateMethods.set(listOf("dataClass"))
}
}
Previous
Gradle plugin configuration
Next
Multi Modules
Rate articleRateEdit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc., d/b/a Apollo GraphQL.

Privacy Policy

Company