Skip to content

Commit c6523e3

Browse files
authored
Update migration guide changelog and welcome page. (#158)
1 parent e860f26 commit c6523e3

File tree

3 files changed

+88
-26
lines changed

3 files changed

+88
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Next version (unreleased)
22

33
- The computation of cache keys when multiple key fields are used has changed to avoid potential collisions. Note: this can lead to cache misses after upgrading to this version. (#80)
4+
- Make SQL cache more robust. (#152)
5+
- Support simple list cases in `FieldPolicyCacheResolver`. (#142)
6+
- Fragments selecting the key fields are now automatically added for union members and interface possible types by the compiler plugin. (#141)
7+
- Introduce `CacheKey.Scope`. (#102)
48

59
# Version 1.0.0-alpha.1
610

Writerside/topics/migration-guide.md

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
# Migration guide
22

3-
> This guide is a work in progress
3+
The Apollo Kotlin Normalized Cache used to be part of the [Apollo Kotlin main repository](https://github.com/apollographql/apollo-kotlin).
4+
It is now hosted in this dedicated repository and published at its own cadence and versioning scheme.
45

5-
{style="warning"}
6-
7-
This guide highlights the main differences between this library and the version hosted on the
8-
[main Apollo Kotlin repository](https://github.com/apollographql/apollo-kotlin).
6+
This guide highlights the main differences between this library and the "classic" version, and how to migrate to it.
97

108
## Artifacts and packages
119

@@ -42,6 +40,23 @@ import com.apollographql.cache.normalized.memory.MemoryCacheFactory
4240

4341
In most cases, this will be enough to migrate your project, but there were a few renames and API breaking changes. Read on for more details.
4442

43+
## Compiler plugin
44+
45+
Configure the compiler plugin in your `build.gradle.kts` file:
46+
47+
```kotlin
48+
apollo {
49+
service("service") {
50+
packageName.set("com.example")
51+
52+
// Add this
53+
plugin("com.apollographql.cache:normalized-cache-apollo-compiler-plugin:%latest_version%") {
54+
argument("packageName", packageName.get())
55+
}
56+
}
57+
}
58+
```
59+
4560
## Database schema
4661

4762
The SQLite cache now uses a different schema.
@@ -60,10 +75,6 @@ See an example on how to do that [here](https://github.com/apollographql/apollo-
6075

6176
Make sure you thoroughly test migration scenarios before deploying to production.
6277

63-
> Expect more changes to the schema as the library evolves and stabilizes.
64-
65-
{style="warning"}
66-
6778
## `ApolloStore`
6879

6980
### Partial cache reads
@@ -108,14 +119,14 @@ Previously, if you configured custom scalar adapters on your client, you had to
108119
Now, `ApolloStore` has a reference to the client's `CustomScalarAdapters` so individual methods no longer need an adapters argument.
109120

110121
```kotlin
111-
// Before
122+
// Replace
112123
client.apolloStore.writeOperation(
113124
operation = operation,
114125
data = data,
115126
customScalarAdapters = client.customScalarAdapters
116127
)
117128

118-
// After
129+
// With
119130
client.apolloStore.writeOperation(
120131
operation = operation,
121132
data = data
@@ -128,7 +139,7 @@ The `ApolloStore` interface has been renamed to `CacheManager`. If you provide y
128139
Correspondingly, the `ApolloClient.Builder.store()` extension has been renamed to `ApolloClient.Builder.cacheManager()`.
129140

130141
```kotlin
131-
// Before
142+
// Replace
132143
val MyStore = object : ApolloStore {
133144
// ...
134145
}
@@ -137,7 +148,7 @@ val apolloClient = ApolloClient.Builder()
137148
.store(MyStore)
138149
.build()
139150

140-
// After
151+
// With
141152
val MyStore = object : CacheManager {
142153
// ...
143154
}
@@ -152,13 +163,13 @@ val apolloClient = ApolloClient.Builder()
152163
- `readFragment()` now returns a `ReadResult<D>` (it previously returned a `<D>`). This allows for surfacing metadata associated to the returned data, e.g. staleness.
153164
- Records are now rooted per operation type (`QUERY_ROOT`, `MUTATION_ROOT`, `SUBSCRIPTION_ROOT`), when previously these were all at the same level, which could cause conflicts.
154165

155-
## CacheResolver, CacheKeyResolver
166+
## `CacheResolver`, `CacheKeyResolver`
156167

157168
The APIs of `CacheResolver` and `CacheKeyResolver` have been tweaked to be more future-proof. The main change is that the methods now takes a `ResolverContext` instead of
158169
individual parameters.
159170

160171
```kotlin
161-
// Before
172+
// Replace
162173
interface CacheResolver {
163174
fun resolveField(
164175
field: CompiledField,
@@ -168,23 +179,69 @@ interface CacheResolver {
168179
): Any?
169180
}
170181

171-
// After
182+
// With
172183
interface CacheResolver {
173184
fun resolveField(context: ResolverContext): Any?
174185
}
175186
```
176187

177188
`resolveField` can also now return a `ResolvedValue` when metadata should be returned with the resolved value (e.g. staleness).
178189

179-
### CacheKey
190+
### `TypePolicyCacheKeyGenerator`
191+
192+
You can now pass the type policies to the `TypePolicyCacheKeyGenerator` constructor, and it is recommended to do so.
193+
The type policies are generated by the compiler plugin in `yourpackage.cache.Cache.typePolicies`.
194+
195+
If your entities ids are unique across the service, you can pass `CacheKey.Scope.SERVICE` to the `TypePolicyCacheKeyGenerator` constructor
196+
to save space in the cache and improve hit rates in certain cases.
197+
198+
```kotlin
199+
// Replace
200+
val apolloClient = ApolloClient.Builder()
201+
// ...
202+
.normalizedCache(cacheFactory)
203+
.build()
204+
205+
// With
206+
val apolloClient = ApolloClient.Builder()
207+
// ...
208+
.normalizedCache(
209+
cacheFactory,
210+
cacheKeyGenerator = TypePolicyCacheKeyGenerator(
211+
typePolicies = Cache.typePolicies,
212+
keyScope = CacheKey.Scope.SERVICE // defaults to TYPE
213+
)
214+
)
215+
.build()
216+
```
217+
218+
### `FieldPolicyCacheResolver`
219+
220+
- `FieldPolicyCacheResolver` now supports simple list cases. If your field takes a flat list of ids, you no longer need to implement a custom `CacheResolver` for it and can use `@fieldPolicy`.
221+
- As for `TypePolicyCacheKeyGenerator`, you can pass `CacheKey.Scope.SERVICE` to the constructor if your ids are unique across the service:
222+
223+
```kotlin
224+
val apolloClient = ApolloClient.Builder()
225+
// ...
226+
.normalizedCache(
227+
cacheFactory,
228+
cacheKeyGenerator = /*...*/,
229+
cacheResolver = FieldPolicyCacheResolver(
230+
keyScope = CacheKey.Scope.SERVICE // defaults to TYPE
231+
)
232+
)
233+
.build()
234+
```
235+
236+
## CacheKey
180237

181238
For consistency, the `CacheKey` type is now used instead of `String` in more APIs, e.g.:
182239

183240
- `ApolloStore.remove()`
184241
- `Record.key`
185242
- `NormalizedCache.loadRecord()`
186243

187-
### Removed APIs
244+
## Removed APIs
188245

189246
- `ApolloCacheHeaders.EVICT_AFTER_READ` is removed. Manually call `ApolloStore.remove()` when needed instead.
190247
- `NormalizedCache.remove(pattern: String)` is removed. Please open an issue if you need this feature back.

Writerside/topics/welcome.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
# Welcome
22

3-
This repository hosts [Apollo Kotlin](https://github.com/apollographql/apollo-kotlin)'s new normalized cache, aiming to replace the main repository version.
3+
This is [Apollo Kotlin](https://github.com/apollographql/apollo-kotlin)'s Normalized Cache.
4+
5+
For an introduction please read the Normalized Cache [documentation](https://www.apollographql.com/docs/kotlin/caching/normalized-cache).
6+
7+
Note: the Normalized Cache used to be part of the [Apollo Kotlin main repository](https://github.com/apollographql/apollo-kotlin).
8+
It is now hosted in a dedicated repository and published at its own cadence and versioning scheme.
49

510
## Use in your project
611

7-
> This version is not yet stable and is subject to change. It is recommended to experiment with it in
8-
> non-critical projects/modules, or behind a feature flag.
9-
>
10-
> In particular,
11-
> - there are no guarantees about the format of the cached data, so you should assume that it may be lost when upgrading
12-
> - performance and size may not be optimal
12+
> During the alpha phase, the API is still subject to change, although we will try to make changes in non-breaking ways.
13+
> For now it is recommended to experiment with this library in non-critical projects/modules, or behind a feature flag.
1314
1415
{style="warning"}
1516

@@ -26,4 +27,4 @@ dependencies {
2627
}
2728
```
2829

29-
If you were using the stable Normalized Cache before, you can update your imports to the new package, `com.apollographql.cache.normalized.*`.
30+
If you were using the classic Normalized Cache before, please consult the [migration guide](migration-guide.md).

0 commit comments

Comments
 (0)