Gradle plugin configuration


Minimal configuration

Apollo Kotlin's default configuration works for the majority of use cases. It requires a service name and a package name:

Kotlin
1apollo {
2  // Use "service" as a service name. 
3  // A service has a single schema and represent a single schema.
4  // You can have several services in a single app.
5  service("service") {
6    // The package name used for generated code.
7    packageName.set("com.example")
8  }
9}

All options

For more advanced usages, below are all Apollo Gradle Plugin options in a single code block. You can also take a look at the Gradle Plugin recipes.

Please refer to the ApolloExtension and Service API reference for details about a specific API.

Kotlin
1apollo {
2  service("service") {
3    // The package name for the generated models
4    packageName.set("com.example")
5
6    // Adds the given directory as a GraphQL source root
7    srcDir("src/main/graphql")
8    // Operation files to include.
9    includes.add("**/*.graphql")
10    // Operation files to exclude.
11    excludes.add("**/*.graphqls")
12
13    // Explicitly set the schema
14    schemaFiles.from("src/main/graphql/schema.graphqls")
15    // Extend your schema locally with type extensions
16    schemaFiles.from("shared/graphql/schema.graphqls", "shared/graphql/extra.graphqls")
17
18    // What codegen to use. One of "operationBased", "responseBased"
19    codegenModels.set("operationBased")
20
21    // Warn if using a deprecated field
22    warnOnDeprecatedUsages.set(true)
23    // Fail on warnings
24    failOnWarnings.set(true)
25
26    // Map the "Date" custom scalar to the com.example.Date Kotlin type
27    mapScalar("Date", "com.example.Date")
28    // Shorthands to map scalar to builtin types and configure their adapter at build time
29    mapScalarToUpload("Upload")
30    mapScalarToKotlinString("MyString")
31    mapScalarToKotlinInt("MyInt")
32    mapScalarToKotlinDouble("MyDouble")
33    mapScalarToKotlinFloat("MyFloat")
34    mapScalarToKotlinLong("MyLong")
35    mapScalarToKotlinBoolean("MyBoolean")
36    mapScalarToKotlinAny("MyAny")
37    mapScalarToJavaString("MyString")
38    mapScalarToJavaInteger("MyInteger")
39    mapScalarToJavaDouble("MyDouble")
40    mapScalarToJavaFloat("MyFloat")
41    mapScalarToJavaLong("MyLong")
42    mapScalarToJavaBoolean("MyBoolean")
43    mapScalarToJavaObject("MyObject")
44
45
46    // The format to output for the operation manifest. One of "operationOutput", "persistedQueryManifest"
47    operationManifestFormat.set("persistedQueryManifest")
48    // The file where to write the operation manifest
49    operationManifest.set(file("build/generated/persistedQueryManifest.json"))
50
51    // Whether to generate Kotlin or Java models
52    generateKotlinModels.set(true)
53    // Target language version for the generated code.
54    languageVersion.set("1.5")
55    // Whether to suffix operation name with 'Query', 'Mutation' or 'Subscription'
56    useSemanticNaming.set(true)
57    // Whether to generate kotlin constructors with `@JvmOverloads` for more graceful Java interop experience when default values are present.
58    addJvmOverloads.set(true)
59    // Whether to generate Kotlin models with `internal` visibility modifier.
60    generateAsInternal.set(true)
61    // Whether to generate default implementation classes for GraphQL fragments.
62    generateFragmentImplementations.set(true)
63    // Whether to write the query document in models
64    generateQueryDocument.set(true)
65    // Whether to generate the Schema class.
66    generateSchema.set(true)
67    // Name for the generated schema
68    generatedSchemaName.set("Schema")
69    // Whether to generate operation variables as [com.apollographql.apollo.api.Optional]
70    generateOptionalOperationVariables.set(true)
71    // Whether to generate the type safe Data builders.
72    generateDataBuilders.set(true)
73    // Whether to generate response model builders for Java.
74    generateModelBuilders.set(true)
75    // Which methods to auto generate (can include: `equalsHashCode`, `copy`, `toString`, or `dataClass`)
76    generateMethods.set(listOf("dataClass"))
77    // Whether to generate fields as primitive types (`int`, `double`, `boolean`) instead of their boxed types (`Integer`, `Double`, etc..)
78    generatePrimitiveTypes.set(true)
79    // Opt-in Builders for Operations, Fragments and Input types. Builders are more ergonomic than default arguments when there are a lot of
80    // optional arguments.
81    generateInputBuilders.set(true)
82    // The style to use for fields that are nullable in the Java generated code
83    nullableFieldStyle.set("apolloOptional")
84    // Whether to decapitalize field names in the generated models (for instance `FooBar` -> `fooBar`)
85    decapitalizeFields.set(false)
86
87    // Whether to add the [JsExport] annotation to generated models.
88    jsExport.set(true)
89    // When to add __typename.
90    addTypename.set("always")
91    // Whether to flatten the models. File paths are limited on MacOSX to 256 chars and flattening can help keeping the path length manageable
92    flattenModels.set(true)
93    // A list of [Regex] patterns for GraphQL enums that should be generated as Kotlin sealed classes instead of the default Kotlin enums.
94    sealedClassesForEnumsMatching.set(listOf(".*"))
95    // A list of [Regex] patterns for GraphQL enums that should be generated as Java classes.
96    classesForEnumsMatching.set(listOf(".*"))
97    // Whether fields with different shape are disallowed to be merged in disjoint types.
98    fieldsOnDisjointTypesMustMerge.set(false)
99
100
101    // The directory where the generated models are written.
102    outputDir.set(file("build/generated/apollo"))
103
104    // Whether to generate Apollo metadata. Apollo metadata is used for multi-module support.
105    generateApolloMetadata.set(true)
106    // list of [Regex] patterns matching for types and fields that should be generated whether they are used by queries/fragments in this module or not.
107    alwaysGenerateTypesMatching.set(listOf(".*"))
108    // Reuse the schema from an upstream module
109    dependsOn(project(":schema"))
110    // Compute used types from a downstream module
111    isADependencyOf(project(":feature"))
112
113    // configure introspection schema download
114    introspection {
115      endpointUrl.set("https://your.domain/graphql/endpoint")
116      schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
117    }
118    // configure registry schema download
119    registry {
120      key.set(System.getenv("APOLLO_KEY"))
121      graph.set(System.getenv("APOLLO_GRAPH"))
122      schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
123    }
124    // configure a compiler plugin
125    plugin(project(":apollo-compiler-plugin")) {
126      argument("myarg", "someValue")
127    }
128    // wire the generated models to the "test" source set
129    outputDirConnection {
130      connectToKotlinSourceSet("test")
131    }
132  }
133
134  // Make IDEA aware of codegen and will run it during your Gradle Sync, default: false
135  generateSourcesDuringGradleSync.set(true)
136
137  // Link sqlite for Kotlin native projects
138  linkSqlite.set(true)
139}
Feedback

Edit on GitHub

Forums