Skip to content

Commit 2cb197f

Browse files
committed
Add documentation for cache options
1 parent 7d30110 commit 2cb197f

4 files changed

Lines changed: 67 additions & 32 deletions

File tree

Writerside/doc.tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
<toc-element toc-title="Kdoc" href="https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc" />
1212
<toc-element topic="welcome.md" />
1313
<toc-element topic="migration-guide.md" />
14-
<toc-element topic="cache-control.md" />
14+
<toc-element topic="expiration.md" />
15+
<toc-element topic="options.md" />
1516
<toc-element topic="pagination-home.md">
1617
<toc-element topic="pagination-relay-style.md" />
1718
<toc-element topic="pagination-other.md" />
Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1-
# Cache control
1+
# Expiration
22

3-
The cache control feature takes the freshness of fields into consideration when accessing the cache. This is also sometimes referred to as TTL (Time To Live) or expiration.
3+
The cache can be configured to store expiration information using a max-age. This is also sometimes referred to as TTL (Time To Live) or freshness.
44

5-
Freshness can be configured by the server, by the client, or both.
5+
Max-age can be configured by the server, by the client, or both.
66

7-
## Server-controlled
7+
## Server-controlled max-age
88

9-
When receiving a response from the server, the [`Cache-Control` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) can be used to determine the **expiration date** of the fields in the response.
9+
When receiving a response from the server, the [`Cache-Control` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) can be used to determine the **max age** of the fields in the response.
1010

1111
> Apollo Server can be configured to include the `Cache-Control` header in responses. See the [caching documentation](https://www.apollographql.com/docs/apollo-server/performance/caching/) for more information.
1212
13+
> The [`Expires` HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Expires) is not supported. Only `Cache-Control` is.
14+
{style="note"}
15+
1316
The cache can be configured to store the **expiration date** of the received fields in the corresponding records. To do so, call [`.storeExpirationDate(true)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/store-expiration-date.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.storeExpirationDate(storeExpirationDate:%20Boolean):%20T), and set your client's cache resolver to [
1417
`CacheControlCacheResolver`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-cache-control-cache-resolver/index.html):
1518

@@ -24,11 +27,11 @@ val apolloClient = ApolloClient.builder()
2427
.build()
2528
```
2629

27-
**Expiration dates** will be stored and when a field is resolved, the cache resolver will check if the field is stale. If so, it will throw a `CacheMissException`.
30+
**Expiration dates** are stored and when a field is resolved, the cache resolver will check if the field is stale. If so, it will return an error..
2831

29-
## Client-controlled
32+
## Client-controlled max-age
3033

31-
When storing fields, the cache can also store their **received date**. This date can then be compared to the current date when resolving a field to determine if its age is above its **maximum age**.
34+
When storing fields, the cache can also store their **received date**. This date can then be compared to the current date when resolving a field to determine if its age is above its **max age**.
3235

3336
To store the **received date** of fields, call [`.storeReceivedDate(true)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/store-receive-date.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.storeReceivedDate(storeReceivedDate:%20Boolean):%20T), and set your client's cache resolver to [
3437
`CacheControlCacheResolver`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-cache-control-cache-resolver/index.html):
@@ -46,7 +49,7 @@ val apolloClient = ApolloClient.builder()
4649

4750
> Expiration dates and received dates can be both stored to combine server-controlled and client-controlled expiration strategies.
4851
49-
The **maximum age** of fields can be configured either programmatically, or declaratively in the schema. This is done by passing a [`MaxAgeProvider`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-max-age-provider/index.html?query=interface%20MaxAgeProvider) to the `CacheControlCacheResolver`.
52+
The **max age** of fields can be configured either programmatically, or declaratively in the schema. This is done by passing a [`MaxAgeProvider`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized.api/-max-age-provider/index.html?query=interface%20MaxAgeProvider) to the `CacheControlCacheResolver`.
5053

5154
### Global max age
5255

@@ -112,24 +115,3 @@ cacheResolver = CacheControlCacheResolver(
112115
)
113116
),
114117
```
115-
116-
## Maximum staleness
117-
118-
If stale fields are acceptable up to a certain value, you can set a maximum staleness duration. This duration is the maximum time that a stale field will be resolved without resulting in a cache miss. To set this duration, call [`.maxStale(Duration)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/max-stale.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.maxStale(maxStale:%20Duration):%20T) either globally on your client, or per operation:
119-
120-
```kotlin
121-
val response = client.query(MyQuery())
122-
.fetchPolicy(FetchPolicy.CacheOnly)
123-
.maxStale(1.hours)
124-
.execute()
125-
```
126-
127-
### `isStale`
128-
129-
With `maxStale`, it is possible to get data from the cache even if it is stale. To know if the response contains stale fields, you can check [`CacheInfo.isStale`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/-cache-info/is-stale.html):
130-
131-
```kotlin
132-
if (response.cacheInfo?.isStale == true) {
133-
// The response contains at least one stale field
134-
}
135-
```

Writerside/topics/garbage-collection.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The garbage collection feature allows to remove unused data from the cache to re
77
A field is considered stale if its **received date** is older than its (client controlled) max age, or if its (server controlled)
88
**expiration date** has passed.
99

10-
See [](cache-control.md) for more information about staleness.
10+
See [](expiration.md) for more information about staleness.
1111

1212
Stale fields can be removed from the cache by calling the [`ApolloStore.removeStaleFields()`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/remove-stale-fields.html) function.
1313

Writerside/topics/options.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Query options
2+
3+
When you execute a query, options control how the query is executed. `ApolloClient` returns an `ApolloResponse` that satisfies the options or an exceptional response otherwise (`response.exception` is not null).
4+
5+
## `fetchPolicy`
6+
7+
Fetch policy controls how and if the cache is used. The default is `CacheFirst`.
8+
9+
### CacheFirst
10+
11+
A response is fetched from the cache first. If no valid response cannot be found, the network is queried.
12+
13+
### CacheOnly
14+
15+
The response is fetched from the cache. If no valid response cannot be found, `response.exception` is set.
16+
17+
### NetworkOnly
18+
19+
The response is fetched from the network. If no valid response cannot be found, `response.exception` is set.
20+
21+
### NetworkFirst
22+
23+
A response is fetched from the network first. If no valid response cannot be found, the cache is queried.
24+
25+
## `allowCachedErrors`
26+
27+
Sets whether to allow GraphQL errors to be returned from the cache. If set to false, if any field is an Error in the cache, the returned response has a null data and a non-null exception of type ApolloGraphQLException.
28+
29+
## `allowCachedPartialResults`
30+
31+
Sets whether to allow partial results to be returned from the cache. If set to false, if any field is missing in the cache, the returned response has a null data and a non-null exception of type CacheMissException.
32+
33+
## `maxStale`
34+
35+
If stale fields are acceptable up to a certain value, you can set a maximum staleness duration. This duration is the maximum time that a stale field will be resolved without resulting in a cache miss. To set this duration, call [`.maxStale(Duration)`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/max-stale.html?query=fun%20%3CT%3E%20MutableExecutionOptions%3CT%3E.maxStale(maxStale:%20Duration):%20T) either globally on your client, or per operation:
36+
37+
```kotlin
38+
val response = client.query(MyQuery())
39+
.fetchPolicy(FetchPolicy.CacheOnly)
40+
.maxStale(1.hours)
41+
.execute()
42+
```
43+
44+
### `isStale`
45+
46+
With `maxStale`, it is possible to get data from the cache even if it is stale. To know if the response contains stale fields, you can check [`CacheInfo.isStale`](https://apollographql.github.io/apollo-kotlin-normalized-cache/kdoc/normalized-cache/com.apollographql.cache.normalized/-cache-info/is-stale.html):
47+
48+
```kotlin
49+
if (response.cacheInfo?.isStale == true) {
50+
// The response contains at least one stale field
51+
}
52+
```

0 commit comments

Comments
 (0)