Skip to content

Commit fd7f409

Browse files
BoDmartinbonnin
andauthored
Migration guide tweaks (#6001)
* Migration guide tweaks * More consistency and address feedback * move package name to next_docs --------- Co-authored-by: Martin Bonnin <[email protected]>
1 parent 6fa5117 commit fd7f409

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

docs/source/migration/4.0.mdx

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
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
44
---
55

66
<Note>
@@ -9,27 +9,29 @@ description: From 3.x
99

1010
</Note>
1111

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.
1313

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.
1515

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`.
1717

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.
1921

2022
## Automatic migration using the Android Studio plugin
2123

2224
Apollo Kotlin 4 ships with a companion [Android Studio plugin](../testing/android-studio-plugin#migration-helpers) that automates most of the migration.
2325

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.
2527

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.
2729

2830
## Error handling
2931

3032
<Caution>
3133

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.
3335

3436
See [`executeV3` and `toFlowV3`](#migration-helpers) for temporary methods to help with the migration.
3537

@@ -39,9 +41,9 @@ See [nullability](../advanced/nullability) for a quick peek at the future of Gra
3941

4042
### Fetch errors do not throw
4143

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.
4345

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.
4547

4648
This allows consumers to handle different kinds of errors at the same place, and it prevents Flows from being terminated.
4749

@@ -116,9 +118,9 @@ apolloClient.query(query).watch().filter { it.exception == null }
116118

117119
### ApolloCompositeException is not thrown anymore
118120

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.
120122

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:
122124

123125
```kotlin
124126
// Replace
@@ -134,15 +136,15 @@ val networkException = exception.suppressedExceptions.firstOrNull()
134136

135137
### emitCacheMisses(Boolean) is removed
136138

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.
138140

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.
140142

141143
With the `CacheFirst`, `NetworkFirst` and `CacheAndNetwork` policies, cache misses and network errors are now emitted in `ApolloResponse.exception`.
142144

143145
### Migration helpers
144146

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:
146148

147149
* `ApolloCall.executeV3()`
148150
* `ApolloCall.toFlowV3()`
@@ -152,7 +154,7 @@ Those helper functions:
152154
* make `CacheFirst`, `NetworkFirst` and `CacheAndNetwork` policies ignore fetch errors.
153155
* throw ApolloComposite exception if needed.
154156

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.
156158

157159
## Other Apollo Runtime changes
158160

@@ -177,7 +179,7 @@ val apolloClient = ApolloClient.Builder()
177179

178180
### `ApolloCall.Builder.httpHeaders` is additive
179181

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)`.
181183

182184
```kotlin
183185
// Replace
@@ -203,7 +205,7 @@ Over the years, a lot of support functionality was added alongside `apollo-api`
203205

204206
Moving forward, some artifacts are released separately and have new Maven coordinates, GitHub repositories and package names. Others are deprecated.
205207

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.
207209

208210
The artifacts are:
209211

@@ -276,7 +278,7 @@ apollo {
276278

277279
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.
278280

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.
280282

281283
To enable this, add the "opposite" link of dependencies with `isADependencyOf()`.
282284

@@ -379,7 +381,7 @@ Apollo Kotlin 3 was using the operation root directories to compute the schema n
379381

380382
### Migrating to ApolloCompilerPlugin
381383

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.
383385

384386
`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:
385387

@@ -501,11 +503,11 @@ The [Android Studio plugin](../testing/android-studio-plugin#migration-helpers)
501503

502504
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.
503505

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.
505507

506508
### Enum class names now have their first letter capitalized
507509

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`:
509511

510512
```graphql
511513
# Make sure `someEnum` isn't renamed to `SomeEnum`
@@ -545,7 +547,7 @@ This is a backward compatible change.
545547

546548
### Directive usages are validated and require a matching directive definition
547549

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:
549551

550552
```graphql
551553
query HeroName {
@@ -580,13 +582,6 @@ val myEnum = MyEnum.safeValueOf("foo")
580582

581583
## Cache
582584

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-
590585
### Configuration order
591586

592587
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).
@@ -657,4 +652,4 @@ apolloClient.query().toFlow().asFlowable().firstOrError()
657652

658653
## Example of a migration
659654

660-
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

Comments
 (0)