Skip to content

Commit d101fb2

Browse files
authored
Add possible Gradle options in the doc (#5124)
1 parent 170e7ff commit d101fb2

File tree

2 files changed

+173
-14
lines changed

2 files changed

+173
-14
lines changed

docs/source/advanced/plugin-configuration.mdx

Lines changed: 172 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
title: Gradle plugin configuration
33
---
44

5-
Apollo Kotlin'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.
5+
Apollo Kotlin's default configuration works for the majority of use cases. If you're getting started, see
6+
the [getting started guide](../) for an overview of the default Gradle configuration.
67

78
This article describes configuration options for advanced use cases when using Gradle.
89

910
## Using multiple GraphQL APIs
1011

11-
Apollo Kotlin supports communicating with multiple GraphQL endpoints with different schemas. To do so, create multiple services like so:
12+
Apollo Kotlin supports communicating with multiple GraphQL endpoints with different schemas. To do so, create multiple
13+
services like so:
1214

1315
```kotlin
1416
apollo {
@@ -39,9 +41,11 @@ apollo {
3941
4042
## Combining multiple schema files
4143

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

44-
If you expand your schema in a separate file, you can instruct Apollo Kotlin to construct its schema from a combination of multiple files, like so:
47+
If you expand your schema in a separate file, you can instruct Apollo Kotlin to construct its schema from a combination
48+
of multiple files, like so:
4549

4650
```kotlin
4751
apollo {
@@ -61,7 +65,8 @@ By default, Apollo Kotlin adds generated source:
6165
- to `commonMain` for multiplatform projects
6266
- to all non-test variants for Android projects
6367

64-
You can customize this behavior with the `outputDirConnection` property. For example, to wire a service to the test source set of a Kotlin
68+
You can customize this behavior with the `outputDirConnection` property. For example, to wire a service to the test
69+
source set of a Kotlin
6570
JVM project:
6671

6772
```kotlin
@@ -76,7 +81,8 @@ apollo {
7681

7782
## Android variants support
7883

79-
It is sometimes useful to have different operations or schemas depending on the [variant](https://developer.android.com/studio/build/build-variants) of your Android project.
84+
It is sometimes useful to have different operations or schemas depending on
85+
the [variant](https://developer.android.com/studio/build/build-variants) of your Android project.
8086

8187
To do this, you can instruct the Gradle plugin to automatically configure a Service per variant, like so:
8288

@@ -89,10 +95,13 @@ apollo {
8995
}
9096
```
9197

92-
- `sourceFolder` where to find the GraphQL relative to "src/$sourceSetName/graphql". Pass "." to look into "src/$sourceSetName/graphql".
98+
- `sourceFolder` where to find the GraphQL relative to "src/$sourceSetName/graphql". Pass "." to look into "
99+
src/$sourceSetName/graphql".
93100
- `nameSuffix` a suffix to use for the service name. Leave blank to use the variant name as is.
94101

95-
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. This means your project could look like this (e.g. when certain operations must only exist in debug builds):
102+
Similarly to what the Android variant system does with source code, the GraphQL files are handled additively, and files
103+
in `src/main/graphql` are included in all services. This means your project could look like this (e.g. when certain
104+
operations must only exist in debug builds):
96105

97106
```
98107
- main
@@ -118,7 +127,8 @@ Or for instance like this (specific backend per flavor):
118127
- operations.graphql // Operations specific to the 'full' flavor
119128
```
120129

121-
If you have many variants 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:
130+
If you have many variants and don't need to configure an Apollo Service for each one of them, it may be simpler to
131+
declare the Services manually, for instance:
122132

123133
```kotlin
124134
apollo {
@@ -139,7 +149,6 @@ apollo {
139149
}
140150
```
141151

142-
143152
## Downloading a schema
144153

145154
By default, the Gradle plugin registers a `downloadApolloSchema` task that you can use from the command line:
@@ -169,9 +178,11 @@ apollo {
169178
}
170179
```
171180

172-
This will create a task named `download<ServiceName>ApolloSchemaFromIntrospection` (`downloadServiceApolloSchemaFromIntrospection` by default).
181+
This will create a task
182+
named `download<ServiceName>ApolloSchemaFromIntrospection` (`downloadServiceApolloSchemaFromIntrospection` by default).
173183

174-
If you register your schema with [Apollo Studio](https://www.apollographql.com/docs/studio/), use the `registry` block instead:
184+
If you register your schema with [Apollo Studio](https://www.apollographql.com/docs/studio/), use the `registry` block
185+
instead:
175186

176187
```kotlin
177188
apollo {
@@ -189,4 +200,152 @@ apollo {
189200
}
190201
```
191202

192-
This will create a task named `download<ServiceName>ApolloSchemaFromRegistry` (`downloadServiceApolloSchemaFromRegistry` by default).
203+
This will create a task named `download<ServiceName>ApolloSchemaFromRegistry` (`downloadServiceApolloSchemaFromRegistry`
204+
by default).
205+
206+
## All options
207+
208+
Below is a summary of the Gradle options in a single code block. For more details, take a look at the [ApolloExtension KDoc](https://www.apollographql.com/docs/kotlin/kdoc/apollo-gradle-plugin-external/com.apollographql.apollo3.gradle.api/-apollo-extension/index.html)
209+
210+
```kotlin
211+
apollo {
212+
service("api") {
213+
// The package name for the generated models
214+
packageName.set("com.example")
215+
216+
// Adds the given directory as a GraphQL source root
217+
srcDir("src/main/graphql")
218+
// Operation files to include.
219+
includes.add("**/*.graphql")
220+
// Operation files to exclude.
221+
excludes.add("**/*.graphqls")
222+
223+
// explicitly set the schema
224+
schemaFile.set("src/main/graphql/schema.graphqls")
225+
// you can also merge different files together
226+
schemaFile.setFrom("shared/graphql/schema.graphqls", "shared/graphql/extra.graphqls")
227+
228+
// What codegen to use. One of "operationBased", "responseBased"
229+
codegenModels.set("operationBased")
230+
231+
// Warn if using a deprecated field
232+
warnOnDeprecatedUsages.set(true)
233+
// Fail on warnings
234+
failOnWarnings.set(true)
235+
236+
// Map the "Date" custom scalar to the com.example.Date Kotlin type
237+
mapScalar("Date", "com.example.Date")
238+
// Shorthands to map scalar to builtin types and configure their adapter at build time
239+
mapScalarToUpload("Upload")
240+
mapScalarToKotlinString("MyString")
241+
mapScalarToKotlinInt("MyInt")
242+
mapScalarToKotlinDouble("MyDouble")
243+
mapScalarToKotlinFloat("MyFloat")
244+
mapScalarToKotlinLong("MyLong")
245+
mapScalarToKotlinBoolean("MyBoolean")
246+
mapScalarToKotlinAny("MyAny")
247+
mapScalarToJavaString("MyString")
248+
mapScalarToJavaInteger("MyInteger")
249+
mapScalarToJavaDouble("MyDouble")
250+
mapScalarToJavaFloat("MyFloat")
251+
mapScalarToJavaLong("MyLong")
252+
mapScalarToJavaBoolean("MyBoolean")
253+
mapScalarToJavaObject("MyObject")
254+
255+
// Configure operation ids for persisted queries
256+
operationOutputGenerator = object : OperationOutputGenerator {
257+
override fun generate(operationDescriptorList: Collection<OperationDescriptor>): OperationOutput {
258+
return operationDescriptorList.map {
259+
it.source.sha256() to it
260+
}.toMap()
261+
}
262+
263+
override val version = "v1"
264+
}
265+
// The format to output for the operation manifest.
266+
operationManifestFormat.set("persistedQueryManifest")
267+
// The file where to write the operaiton manifest
268+
operationManifest.set("build/generated/persistedQueryManifest.json")
269+
270+
// Whether to generate Kotlin or Java models
271+
generateKotlinModels.set(true)
272+
// Target language version for the generated code.
273+
languageVersion.set("1.5")
274+
// Whether to suffix operation name with 'Query', 'Mutation' or 'Subscription'
275+
useSemanticNaming.set(true)
276+
// Whether to generate kotlin constructors with `@JvmOverloads` for more graceful Java interop experience when default values are present.
277+
addJvmOverloads.set(true)
278+
// Whether to generate Kotlin models with `internal` visibility modifier.
279+
generateAsInternal.set(true)
280+
// Whether to generate default implementation classes for GraphQL fragments.
281+
generateFragmentImplementations.set(true)
282+
// Whether to write the query document in models
283+
generateQueryDocument.set(true)
284+
// Whether to generate the Schema class.
285+
generateSchema.set(true)
286+
// Name for the generated schema
287+
generatedSchemaName.set("Schema")
288+
// Whether to generate operation variables as [com.apollographql.apollo3.api.Optional]
289+
generateOptionalOperationVariables.set(true)
290+
// Whether to generate the type safe Data builders.
291+
generateDataBuilders.set(true)
292+
// Whether to generate response model builders for Java.
293+
generateModelBuilders.set(true)
294+
// Whether to generate fields as primitive types (`int`, `double`, `boolean`) instead of their boxed types (`Integer`, `Double`, etc..)
295+
generatePrimitiveTypes.set(true)
296+
// The style to use for fields that are nullable in the Java generated code
297+
nullableFieldStyle.set("apolloOptional")
298+
// Whether to decapitalize field names in the generated models (for instance `FooBar` -> `fooBar`)
299+
decapitalizeFields.set(false)
300+
301+
// Whether to add the [JsExport] annotation to generated models.
302+
jsExport.set(true)
303+
// When to add __typename.
304+
addTypename.set("always")
305+
// Whether to flatten the models. File paths are limited on MacOSX to 256 chars and flattening can help keeping the path length manageable
306+
flattenModels.set(true)
307+
// A list of [Regex] patterns for GraphQL enums that should be generated as Kotlin sealed classes instead of the default Kotlin enums.
308+
sealedClassesForEnumsMatching.set(listOf(".*"))
309+
// A list of [Regex] patterns for GraphQL enums that should be generated as Java classes.
310+
classesForEnumsMatching.set(listOf(".*"))
311+
// Whether fields with different shape are disallowed to be merged in disjoint types.
312+
fieldsOnDisjointTypesMustMerge.set(false)
313+
314+
315+
// The directory where the generated models will be written.
316+
outputDir.set("build/generated/apollo")
317+
318+
// Whether to generate Apollo metadata. Apollo metadata is used for multi-module support.
319+
generateApolloMetadata.set(true)
320+
// 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.
321+
alwaysGenerateTypesMatching.set(listOf(".*"))
322+
// Reuse the schema from an upstream module
323+
dependsOn(project(":schema"))
324+
// Compute used types from a downstream module
325+
isADependencyOf(project(":feature"))
326+
327+
// configure introspection schema download
328+
introspection {
329+
endpointUrl.set("https://your.domain/graphql/endpoint")
330+
schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
331+
}
332+
// configure registry schema download
333+
registry {
334+
key.set(System.getenv("APOLLO_KEY"))
335+
graph.set(System.geten("APOLLO_GRAPH"))
336+
schemaFile.set(file("src/main/graphql/com/example/schema.graphqls"))
337+
}
338+
339+
// wire the generated models to the "test" source set
340+
outputDirConnection {
341+
connectToKotlinSourceSet("test")
342+
}
343+
}
344+
345+
// Make IDEA aware of codegen and will run it during your Gradle Sync, default: false
346+
generateSourcesDuringGradleSync.set(true)
347+
348+
// Link sqlite for Kotlin native projects
349+
linkSqlite.set(true)
350+
}
351+
```

docs/source/migration/3.0.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ The Gradle plugin has significantly changed in Apollo Kotlin 3.x to only generat
107107

108108
#### `generateKotlinModels`
109109

110-
Apollo Kotlin 3.0 generates Kotlin models by default. You can safely remove this behavior:
110+
If you are using the Kotlin Gradle Plugin, Apollo Kotlin 3.0 generates Kotlin models by default. You can safely remove this behavior:
111111

112112
```kotlin
113113
apollo {

0 commit comments

Comments
 (0)