Skip to content

Commit a18a291

Browse files
martinbonninBoD
andauthored
Revisit docs 2/n: configuration + fetching (#6090)
* revisit Gradle docs * rework multi modules * revert unwanted changes * fix link * wording * mention generateInputBuilders * move the experimental websockets up * Fix broken polymorphic types link * typo * add not about new WebSocket API * fix syntax error * fix syntax * Update docs/source/advanced/plugin-recipes.mdx Co-authored-by: Benoit 'BoD' Lubek <[email protected]> --------- Co-authored-by: Benoit 'BoD' Lubek <[email protected]>
1 parent ea26b14 commit a18a291

File tree

8 files changed

+302
-275
lines changed

8 files changed

+302
-275
lines changed

docs/source/advanced/multi-modules.mdx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,30 @@ apollo {
6060

6161
By default, Apollo Kotlin generates all the types in your schema module. This is because there is no way to know in advance what types are going to be used by feature modules.
6262

63-
For large schemas, this can generate a lot of code and increase your build time significantly. In this case, you can opt in auto-detection of used types. This works by splitting the codegen task in different steps so that feature modules can let the schema module know what types are used before actually generating the models. To opt in, call `dependsOn()` with the `bidirectional` argument set to true:
63+
For large schemas, this can generate a lot of code and increase your build time significantly. In this case, you can opt in auto-detection of used types.
64+
65+
This works by splitting the codegen task in different steps so that feature modules can let the schema module know what types are used before actually generating the models. To opt in, call `isADependencyOf()` to establish the bidirectional dependency between your feature module and your schema module:
66+
67+
68+
```kotlin
69+
// schema/build.gradle.kts
70+
apollo {
71+
service("service") {
72+
packageName.set("schema")
73+
generateApolloMetadata.set(true)
74+
75+
isADependencyOf(project(":feature")) // highlight-line
76+
// list all your feature modules here
77+
isADependencyOf(project(":feature2"))
78+
// ...
79+
}
80+
}
81+
```
82+
Once you opt in auto-detection of used types, it's important that **all** modules are doubly linked like above. If not the feature modules will fail to compile due to some missing schema classes.
83+
84+
## bidirectional parameter (Experimental)
85+
86+
Using `isADependencyOf()` requires listing all your feature modules in your schema module which duplicates code. To keep things minimal and configure the dependency automatically, use `bidirectional = true`:
6487

6588
```kotlin
6689
// feature/build.gradle.kts
@@ -76,8 +99,7 @@ apollo {
7699

77100
<ExperimentalFeature>
78101

79-
The `bidirectional` parameter is experimental because it may break <a href="https://docs.gradle.org/current/userguide/build_environment.html">Gradle project isolation</a>. For a project-isolation compatible method, try <a href="https://www.apollographql.com/docs/kotlin/kdoc/apollo-gradle-plugin-external/com.apollographql.apollo.gradle.api/-service/is-a-dependency-of.html">isADependencyOf</a>
102+
The `bidirectional` parameter is experimental and not compatible with <a href="https://docs.gradle.org/current/userguide/build_environment.html">Gradle project isolation</a>.
80103

81104
</ExperimentalFeature>
82105

83-
Once you opt in auto-detection of used types, it's important that **all** modules are doubly linked like above. If not the feature modules will fail to compile due to some missing schema classes.

docs/source/advanced/operation-variables.mdx

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ title: Using GraphQL variables in Apollo Kotlin
44

55
GraphQL supports passing argument values to your operations with [variables](https://graphql.org/graphql-js/passing-arguments/). This enables you to write a single query that you can reuse with multiple variable values (this is a [recommended best practice](/react/data/operation-best-practices/#use-variables-to-provide-graphql-arguments)).
66

7-
In GraphQL, non-nullable variables are required, and nullable variables are optional. However, because variables are rarely omitted in practice, Apollo Kotlin provides a mechanism to make variables non-optional in generated code. This makes the structure of generated code more straightforward.
8-
9-
Because you might still need to omit variables, the default is to generate them as optional, but you can configure this (for specific variables or globally).
7+
In GraphQL, non-nullable variables are required, and nullable variables are always optional. Apollo Kotlin uses its own [`Optional`](https://apollographql.github.io/apollo-kotlin/kdoc/apollo-api/com.apollographql.apollo.api/-optional/index.html) type to distinguish between present (but maybe nullable) and absent types.
108

119
Consider the following GraphQL query with two nullable variables:
1210

@@ -39,45 +37,36 @@ val query = GetTodosQuery(Optional.Present(null), Optional.Present(null))
3937
val query = GetTodosQuery(Optional.Present(100), Optional.Present(0))
4038
```
4139

42-
## Making nullable variables non-optional
40+
## Using input builders
41+
42+
For both operations and input objects, having to wrap values in an [`Optional`](https://apollographql.github.io/apollo-kotlin/kdoc/apollo-api/com.apollographql.apollo.api/-optional/index.html) wrapper can be cumbersome.
4343

44-
To disable optional variables globally, update your Gradle config like so:
44+
For those cases, use `generateInputBuilders`:
4545

4646
```kotlin
4747
apollo {
4848
service("service") {
4949
// ...
50-
generateOptionalOperationVariables.set(false)
50+
generateInputBuilders.set(true)
5151
}
5252
}
5353
```
5454

55-
If you do, in the case of the `GetTodos` query shown above, Apollo Kotlin now generates the following code:
56-
57-
```kotlin
58-
class GetTodosQuery(val first: Int?, val offset: Int?)
59-
```
60-
61-
This makes the calling code less verbose to use:
55+
If you do, in the case of the `GetTodos` query shown above, Apollo Kotlin now generates a `Builder` for each operation:
6256

6357
```kotlin
58+
// Omit values for both variables
59+
val query = GetTodosQuery.Builder()
60+
.build()
6461
// Provide null for both variables
65-
val query = GetTodosQuery(null, null)
62+
val query = GetTodosQuery.Builder()
63+
.first(null)
64+
.offset(null)
65+
.build()
6666
// Send explicit values for both variables
67-
val query = GetTodosQuery(100, 0)
68-
```
69-
70-
If you pass `null` as the value for either of these variables, Apollo Kotlin sends `null` to the server _as the value of that variable_ instead of omitting it entirely.
71-
72-
Although doing this can simplify your code in most cases, you might still need to omit variables for certain operations. To achieve this, you can allow optional variables on a case-by-case basis with the `@optional` directive:
73-
74-
```graphql {1}
75-
query GetTodos($first: Int @optional, $offset: Int @optional) {
76-
todos(first: $first, offset: $offset) {
77-
id
78-
text
79-
}
80-
}
67+
val query = GetTodosQuery.Builder()
68+
.first(100)
69+
.offset(0)
70+
.build()
8171
```
8272

83-
In this case, Apollo Kotlin generates code with `Optional` for only these specific variables.

0 commit comments

Comments
 (0)