You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Migration guide tweaks
* More consistency and address feedback
* move package name to next_docs
---------
Co-authored-by: Martin Bonnin <[email protected]>
Copy file name to clipboardExpand all lines: docs/source/migration/4.0.mdx
+27-32Lines changed: 27 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
-
title: Migrating to Apollo Kotlin 4.0
3
-
description: From 3.x
2
+
title: Migrating to Apollo Kotlin 4
3
+
description: From version 3
4
4
---
5
5
6
6
<Note>
@@ -9,27 +9,29 @@ description: From 3.x
9
9
10
10
</Note>
11
11
12
-
Apollo Kotlin 3.0 was a major rewrite of Apollo in Kotlin multiplatform.
12
+
Apollo Kotlin 3 was a major rewrite of Apollo in Kotlin multiplatform.
13
13
14
-
Apollo Kotlin 4.0 focuses on tooling, stability and fixing some API regrets that came with 3.x.
14
+
Apollo Kotlin 4 focuses on tooling, stability and fixing some API regrets that came with 3.
15
15
16
-
Because most of the common APIs stayed the same, we [kept the package name unchanged](https://github.com/apollographql/apollo-kotlin/issues/4710). Apollo Kotlin 4.0 removes some deprecated symbols. We strongly recommend removing deprecated usages before migrating to 4.0.
16
+
While most of the common APIs stayed the same, Apollo Kotlin 4 contains a few binary breaking changes. To account for that, and in order to be more future proof, we [changed the package name](https://github.com/apollographql/apollo-kotlin/issues/4710)to `com.apollographql.apollo`.
17
17
18
-
If you are using a library that depends on Apollo Kotlin transitively, you need it to update to 4.x before you can update your own application to 4.0.
18
+
Apollo Kotlin 4 removes some deprecated symbols. We strongly recommend removing deprecated usages before migrating to version 4.
19
+
20
+
If you are using a library that depends on Apollo Kotlin transitively, you need it to update to version 4 before you can update your own application to version 4.
19
21
20
22
## Automatic migration using the Android Studio plugin
21
23
22
24
Apollo Kotlin 4 ships with a companion [Android Studio plugin](../testing/android-studio-plugin#migration-helpers) that automates most of the migration.
23
25
24
-
It automates most of the API changes but cannot deal with behaviour changes like error handling.
26
+
It automates most of the API changes but cannot deal with behavior changes like error handling.
25
27
26
-
We recommend using the plugin to automate the repetitive tasks but still go through this document for the details.
28
+
We recommend using the plugin to automate the repetitive tasks but still go through this document for the details.
27
29
28
30
## Error handling
29
31
30
32
<Caution>
31
33
32
-
Error handling changes are a behaviour change that is not detected at compile time. Usages of `execute`, `toFlow` and `watch` must be updated to the new error handling or changed to their v3 compat equivalent.
34
+
Error handling changes are a behavior change that is not detected at compile time. Usages of `execute`, `toFlow` and `watch` must be updated to the new error handling or changed to their version 3 compat equivalent.
33
35
34
36
See [`executeV3` and `toFlowV3`](#migration-helpers) for temporary methods to help with the migration.
35
37
@@ -39,9 +41,9 @@ See [nullability](../advanced/nullability) for a quick peek at the future of Gra
39
41
40
42
### Fetch errors do not throw
41
43
42
-
In Apollo Kotlin 3.x, non-GraphQL errors like network errors, cache misses, and parsing errors were surfaced by throwing exceptions in `ApolloCall.execute()` and in Flows (`ApolloCall.toFlow()`, `ApolloCall.watch()`). This was problematic because it was a difference in how to handle GraphQL errors vs other errors. Moreover, throwing terminates a Flow and consumers would have to handle re-collection.
44
+
In Apollo Kotlin 3, non-GraphQL errors like network errors, cache misses, and parsing errors were surfaced by throwing exceptions in `ApolloCall.execute()` and in Flows (`ApolloCall.toFlow()`, `ApolloCall.watch()`). This was problematic because it was a difference in how to handle GraphQL errors vs other errors. Moreover, throwing terminates a Flow and consumers would have to handle re-collection.
43
45
44
-
In Apollo Kotlin 4.0, a new field `ApolloResponse.exception` has been added and these errors are now surfaced by returning (for `execute()`) or emitting (for Flows) an `ApolloResponse` with a non-null `exception` instead of throwing it.
46
+
In Apollo Kotlin 4, a new field `ApolloResponse.exception` has been added and these errors are now surfaced by returning (for `execute()`) or emitting (for Flows) an `ApolloResponse` with a non-null `exception` instead of throwing it.
45
47
46
48
This allows consumers to handle different kinds of errors at the same place, and it prevents Flows from being terminated.
### ApolloCompositeException is not thrown anymore
118
120
119
-
When using the cache, Apollo Kotlin 3.x threw `ApolloCompositeException` if no response could be found. For an example, a `CacheFirst` fetch policy would throw `ApolloCompositeException(cacheMissException, apolloNetworkException)` if both cache and network failed.
121
+
When using the cache, Apollo Kotlin 3 threw `ApolloCompositeException` if no response could be found. For an example, a `CacheFirst` fetch policy would throw `ApolloCompositeException(cacheMissException, apolloNetworkException)` if both cache and network failed.
120
122
121
-
In those cases, Apollo Kotlin 4.0 throws the first exception and adds the second as a suppressed exception:
123
+
In those cases, Apollo Kotlin 4 throws the first exception and adds the second as a suppressed exception:
122
124
123
125
```kotlin
124
126
// Replace
@@ -134,15 +136,15 @@ val networkException = exception.suppressedExceptions.firstOrNull()
134
136
135
137
### emitCacheMisses(Boolean) is removed
136
138
137
-
In Apollo Kotlin 3.x, when using the normalized cache, you could set `emitCacheMisses` to `true` to emit cache misses instead of throwing.
139
+
In Apollo Kotlin 3, when using the normalized cache, you could set `emitCacheMisses` to `true` to emit cache misses instead of throwing.
138
140
139
-
In Apollo Kotlin 4.0, this is now the default behavior and `emitCacheMisses` has been removed.
141
+
In Apollo Kotlin 4, this is now the default behavior and `emitCacheMisses` has been removed.
140
142
141
143
With the `CacheFirst`, `NetworkFirst` and `CacheAndNetwork` policies, cache misses and network errors are now emitted in `ApolloResponse.exception`.
142
144
143
145
### Migration helpers
144
146
145
-
To ease the migration from 3.x, drop-in helpers functions are provided that restore the V3 behaviour:
147
+
To ease the migration from Apollo Kotlin 3, drop-in helpers functions are provided that restore the version 3 behavior:
146
148
147
149
*`ApolloCall.executeV3()`
148
150
*`ApolloCall.toFlowV3()`
@@ -152,7 +154,7 @@ Those helper functions:
152
154
* make `CacheFirst`, `NetworkFirst` and `CacheAndNetwork` policies ignore fetch errors.
153
155
* throw ApolloComposite exception if needed.
154
156
155
-
Because of the number of different options in 3.x and the complexity of error handling, these functions do not pretend to match 1:1 the V3 behaviour, especially in the advanced cases involving watchers. If you are in one of those cases, we strongly recommend using the 4.0 functions that are easier to reason about.
157
+
Because of the number of different options in version 3 and the complexity of error handling, these functions may not 100% match the 3 behavior, especially in the advanced cases involving watchers. If you are in one of those cases, we strongly recommend using the 4 functions that are easier to reason about.
156
158
157
159
## Other Apollo Runtime changes
158
160
@@ -177,7 +179,7 @@ val apolloClient = ApolloClient.Builder()
177
179
178
180
### `ApolloCall.Builder.httpHeaders` is additive
179
181
180
-
In v3, if HTTP headers were set on an `ApolloCall`, they would replace the ones set on `ApolloClient`. In v4 they are added instead by default. To replace them, call `ApolloCall.Builder.ignoreApolloClientHttpHeaders(true)`.
182
+
In Apollo Kotlin 3, if HTTP headers were set on an `ApolloCall`, they would replace the ones set on `ApolloClient`. In Apollo Kotlin 4 they are added instead by default. To replace them, call `ApolloCall.Builder.ignoreApolloClientHttpHeaders(true)`.
181
183
182
184
```kotlin
183
185
// Replace
@@ -203,7 +205,7 @@ Over the years, a lot of support functionality was added alongside `apollo-api`
203
205
204
206
Moving forward, some artifacts are released separately and have new Maven coordinates, GitHub repositories and package names. Others are deprecated.
205
207
206
-
For the initial v4 release, most artifacts are kept with deprecation warnings and will be removed in the future.
208
+
For the initial version 4 release, most artifacts are kept with deprecation warnings and will be removed in the future.
207
209
208
210
The artifacts are:
209
211
@@ -276,7 +278,7 @@ apollo {
276
278
277
279
In multi-module projects, by default, all the types of an upstream module are generated because there is no way to know in advance what types are going to be used by downstream modules. For large projects this can lead to a lot of unused code and an increased build time.
278
280
279
-
To avoid this, in v3 you could manually specify which types to generate by using `alwaysGenerateTypesMatching`. In v4 this can now be computed automatically by detecting which types are used by the downstream modules.
281
+
To avoid this, in Apollo Kotlin 3 you could manually specify which types to generate by using `alwaysGenerateTypesMatching`. In Apollo Kotlin 4 this can now be computed automatically by detecting which types are used by the downstream modules.
280
282
281
283
To enable this, add the "opposite" link of dependencies with `isADependencyOf()`.
282
284
@@ -379,7 +381,7 @@ Apollo Kotlin 3 was using the operation root directories to compute the schema n
379
381
380
382
### Migrating to ApolloCompilerPlugin
381
383
382
-
4.0 introduces `ApolloCompilerPlugin` as a way to customize code generation. `ApolloCompilerPlugin` are loaded using the [ServiceLoader](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html) API and run in a separate classloader from your Gradle build. As a result, using `Service.operationIdGenerator`/`Service.operationOutputGenerator` together with `ApolloCompilerPlugin` is not possible.
384
+
Apollo Kotlin 4 introduces `ApolloCompilerPlugin` as a way to customize code generation. `ApolloCompilerPlugin` are loaded using the [ServiceLoader](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html) API and run in a separate classloader from your Gradle build. As a result, using `Service.operationIdGenerator`/`Service.operationOutputGenerator` together with `ApolloCompilerPlugin` is not possible.
383
385
384
386
`Service.operationIdGenerator`/`Service.operationOutputGenerator` are deprecated and will be removed in a future release. You can migrate to `ApolloCompilerPlugin` using the instructions [from the dedicated page](https://www.apollographql.com/docs/kotlin/v4/advanced/compiler-plugins) and the `ApolloCompilerPlugin.operationIds()` method:
385
387
@@ -501,11 +503,11 @@ The [Android Studio plugin](../testing/android-studio-plugin#migration-helpers)
501
503
502
504
Test builders were an experimental feature that have been superseded by [data builders](../testing/data-builders), a simpler version that also plays nicer with custom scalars.
503
505
504
-
In Apollo Kotlin 4.0, test builders are no longer available - please refer to the data builders documentation for more information.
506
+
In Apollo Kotlin 4, test builders are no longer available - please refer to the data builders documentation for more information.
505
507
506
508
### Enum class names now have their first letter capitalized
507
509
508
-
For consistency with other types, GraphQL enums are now capitalized in Kotlin. You can restore the previous behaviour using `@targetName`:
510
+
For consistency with other types, GraphQL enums are now capitalized in Kotlin. You can restore the previous behavior using `@targetName`:
509
511
510
512
```graphql
511
513
# Make sure `someEnum` isn't renamed to `SomeEnum`
@@ -545,7 +547,7 @@ This is a backward compatible change.
545
547
546
548
### Directive usages are validated and require a matching directive definition
547
549
548
-
In Apollo 4, all directive usages are validated against their definition. If you have queries using client side directives:
550
+
In Apollo Kotlin 4, all directive usages are validated against their definition. If you have queries using client side directives:
549
551
550
552
```graphql
551
553
query HeroName {
@@ -580,13 +582,6 @@ val myEnum = MyEnum.safeValueOf("foo")
580
582
581
583
## Cache
582
584
583
-
### ApolloStore
584
-
585
-
In Apollo Kotlin 3.x, most `ApolloStore` functions were marked as `suspend` even though they were not actually suspending and perform the work in the thread they are called from.
586
-
In particular, they can be blocking when the underlying cache is doing IO, so calling them from the main thread can lead to ANRs.
587
-
588
-
In Apollo Kotlin 4.0 this is still the case but the functions are no longer marked as `suspend` to avoid any confusion.
589
-
590
585
### Configuration order
591
586
592
587
The normalized cache must be configured before the auto persisted queries, configuring it after will now fail (see https://github.com/apollographql/apollo-kotlin/pull/4709).
If you are looking for inspiration, we updated the [3.x integration tests to use 4.0](https://github.com/apollographql/apollo-kotlin/pull/5418/). If you have an open source project that migrated, feel free to share it and we'll include it here.
655
+
If you are looking for inspiration, we updated the [version 3 integration tests to use version 4](https://github.com/apollographql/apollo-kotlin/pull/5418/). If you have an open source project that migrated, feel free to share it and we'll include it here.
0 commit comments