Gradle configuration options
Apollo Android comes with logical defaults that will work for the majority of use cases. If you're getting started, see the getting started guide for an overview of the most common options. This page describes the different options individually.
ApolloExtension, Services and CompilationUnit
The Gradle DSL exposes three types of objects:
- ApolloExtension is the global entry point of the plugin and will configure global options.
- A Service is the configuration of a single GraphQL endpoint. If your app uses multiple GraphQL endpoints, you will need to define several services.
- A CompilationUnit is an invocation of the Apollo compiler. It happens for each Service and Variant in your project.
These 3 objects inherit from CompilerParams
and compiler options can be set in all of them. Options set on the most reduced scopes will override those set on the more global scopes:
- Default compiler parameters are taken from
ApolloExtension
- Compiler parameters from
Service
override the ones fromApolloExtension
- Compiler parameters from
CompilerUnit
override the ones fromService
A complete configuration block with multiple services will look like this:
build.gradle
apollo {
// configure ApolloExtension here
generateKotlinModels.set(true) // Generate Kotlin models for all services
service("starwars") {
// Overwrite some options here for the starwars Service here if needed
sourceFolder = "starwars"
rootPackageName = "com.starwars"
}
service("githunt") {
// Overwrite some options here for the githunt Service here if needed
sourceFolder = "githunt"
rootPackageName = "com.starwars"
}
onCompilationUnit {
// Overwrite some options here for single CompilationUnit if needed
}
}
The up-to-date list of options can be found in CompilerParams:
/**
* Whether to generate Java or Kotlin models
*
* Default value: false
*/
val generateKotlinModels: Property<Boolean>
/**
* Whether to generate operationOutput.json. operationOutput.json contains information such as
* operation id, name and complete source sent to the server. This can be used to upload
* a query's exact content to a server that doesn't support automatic persisted queries.
*
* The operation output is written in [CompilationUnit.operationOutputFile]
*
* Default value: false
*/
val generateOperationOutput: Property<Boolean>
/**
* For custom scalar types like Date, map from the GraphQL type to the jvm/kotlin type.
*
* Default value: the empty map
*/
val customTypeMapping: MapProperty<String, String>
/**
* By default, Apollo uses `Sha256` hashing algorithm to generate an ID for the query.
* To provide a custom ID generation logic, pass an `instance` that implements the [OperationIdGenerator]. How the ID is generated is
* indifferent to the compiler. It can be an hashing algorithm or generated by a backend.
*
* Example Md5 hash generator:
*
* import com.apollographql.apollo.compiler.OperationIdGenerator
*
* apollo {
* operationIdGenerator = new OperationIdGenerator() {
* String apply(String operationDocument, String operationFilepath) {
* return operationDocument.md5()
* }
*
* /**
* * Use this version override to indicate an update to the implementation.
* * This invalidates the current cache.
* */
* String version = "v1"
* }
* }
*
* Default value: [OperationIdGenerator.Sha256]
*/
val operationIdGenerator: Property<OperationIdGenerator>
/**
* The custom types code generate some warnings that might make the build fail.
* suppressRawTypesWarning will add the appropriate SuppressWarning annotation
*
* Default value: false
*/
val suppressRawTypesWarning: Property<Boolean>
/**
* When true, Apollo Android will make sure all the generated classes end with 'Query' or 'Mutation'.
* If you write `query droid { ... }`, the generated class will be named 'DroidQuery'.
*
* Default value: true
*/
val useSemanticNaming: Property<Boolean>
/**
* The nullable value type to use. One of: "annotated", "apolloOptional", "guavaOptional", "javaOptional", "inputType"
*
* Default value: "annotated"
* Only valid for java models as kotlin has proper nullability support
*/
val nullableValueType: Property<String>
/**
* Whether to generate builders for java models
*
* Default value: false
* Only valid for java models as kotlin has data classes
*/
val generateModelBuilder: Property<Boolean>
/**
* When true, Apollo Android will use java beans getters in the models. If you request a property named 'user', the generated
* model will have a `getUser()` property instead of `user()`
*
* Default value: false
* Only valid for java as kotlin has properties
*/
val useJavaBeansSemanticNaming: Property<Boolean>
/**
* Apollo Gradle plugin supports generating visitors for compile-time safe handling of polymorphic datatypes.
* Enabling this requires source/target compatibility with Java 1.8.
*
* Default value: false
*/
val generateVisitorForPolymorphicDatatypes: Property<Boolean>
/**
* The package name of the models is computed from their folder hierarchy like for java sources.
*
* If you want, you can prepend a custom package name here to namespace your models.
*
* Default value: the empty string
*/
val rootPackageName: Property<String>
/**
* The graphql files containing the queries.
*
* This SourceDirectorySet includes .graphql and .gql files by default.
*
* By default, it will use [Service.sourceFolder] to populate the SourceDirectorySet.
* You can override it from [ApolloExtension.onCompilationUnit] for more advanced use cases
*/
val graphqlSourceDirectorySet: SourceDirectorySet
/**
* The schema file
*
* By default, it will use [Service.schemaPath] to set schemaFile.
* You can override it from [ApolloExtension.onCompilationUnit] for more advanced use cases
*/
val schemaFile: RegularFileProperty
/**
* Whether to generate Kotlin models with `internal` visibility modifier.
*
* Default value: false
*/
val generateAsInternal: Property<Boolean>
/**
* A list of [Regex] patterns for GraphQL enums that should be generated as Kotlin sealed classes instead of the default Kotlin enums.
*
* Use this if you want your client to have access to the rawValue of the enum. This can be useful if new GraphQL enums are added but
* the client was compiled against an older schema that doesn't have knowledge of the new enums.
*/
val sealedClassesForEnumsMatching: ListProperty<String>