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.
Refer to the ApolloExtension and Service API reference for details about each 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 "none" (default) or "persistedQueryManifest"
47 operationManifestFormat.set("persistedQueryManifest")
48
49 // Whether to generate Kotlin or Java models
50 generateKotlinModels.set(true)
51 // Target language version for the generated code.
52 languageVersion.set("1.5")
53 // Whether to suffix operation name with 'Query', 'Mutation' or 'Subscription'
54 useSemanticNaming.set(true)
55 // Whether to generate kotlin constructors with `@JvmOverloads` for more graceful Java interop experience when default values are present.
56 addJvmOverloads.set(true)
57 // Whether to generate Kotlin models with `internal` visibility modifier.
58 generateAsInternal.set(true)
59 // Whether to generate default implementation classes for GraphQL fragments.
60 generateFragmentImplementations.set(true)
61 // Whether to write the query document in models
62 generateQueryDocument.set(true)
63 // Whether to generate the Schema class.
64 generateSchema.set(true)
65 // Name for the generated schema
66 generatedSchemaName.set("Schema")
67 // Whether to generate operation variables as [com.apollographql.apollo.api.Optional]
68 generateOptionalOperationVariables.set(true)
69 // Whether to generate the type safe Data builders.
70 generateDataBuilders.set(true)
71 // Whether to generate response model builders for Java.
72 generateModelBuilders.set(true)
73 // Which methods to auto generate (can include: `equalsHashCode`, `copy`, `toString`, or `dataClass`)
74 generateMethods.set(listOf("dataClass"))
75 // Whether to generate fields as primitive types (`int`, `double`, `boolean`) instead of their boxed types (`Integer`, `Double`, etc..)
76 generatePrimitiveTypes.set(true)
77 // Opt-in Builders for Operations, Fragments and Input types. Builders are more ergonomic than default arguments when there are a lot of
78 // optional arguments.
79 generateInputBuilders.set(true)
80 // The style to use for fields that are nullable in the Java generated code
81 nullableFieldStyle.set("apolloOptional")
82 // Whether to decapitalize field names in the generated models (for instance `FooBar` -> `fooBar`)
83 decapitalizeFields.set(false)
84
85 // Whether to add the [JsExport] annotation to generated models.
86 jsExport.set(true)
87 // When to add __typename.
88 addTypename.set("always")
89 // Whether to flatten the models. File paths are limited on MacOSX to 256 chars and flattening can help keeping the path length manageable
90 flattenModels.set(true)
91 // A list of [Regex] patterns for GraphQL enums that should be generated as Kotlin sealed classes instead of the default Kotlin enums.
92 sealedClassesForEnumsMatching.set(listOf(".*"))
93 // A list of [Regex] patterns for GraphQL enums that should be generated as Java classes.
94 classesForEnumsMatching.set(listOf(".*"))
95 // Whether fields with different shape are disallowed to be merged in disjoint types.
96 fieldsOnDisjointTypesMustMerge.set(false)
97
98 // Whether to generate Apollo metadata. Apollo metadata is used for multi-module support.
99 generateApolloMetadata.set(true)
100 // 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.
101 alwaysGenerateTypesMatching.set(listOf(".*"))
102 // Reuse the schema from an upstream module
103 dependsOn(project(":schema"))
104 // Compute used types from a downstream module
105 isADependencyOf(project(":feature"))
106
107 // configure introspection schema download
108 introspection {
109 endpointUrl.set("https://your.domain/graphql/endpoint")
110 schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
111 }
112 // configure registry schema download
113 registry {
114 key.set(System.getenv("APOLLO_KEY"))
115 graph.set(System.getenv("APOLLO_GRAPH"))
116 schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
117 }
118 // configure a compiler plugin
119 plugin(project(":apollo-compiler-plugin")) {
120 argument("myarg", "someValue")
121 }
122 // wire the generated models to the "test" source set
123 outputDirConnection {
124 connectToKotlinSourceSet("test")
125 }
126 }
127
128 // Make IDEA aware of codegen and will run it during your Gradle Sync, default: false
129 generateSourcesDuringGradleSync.set(true)
130
131 // Link sqlite for Kotlin native projects
132 linkSqlite.set(true)
133}