···
const defaultTTL = 5 * 60 * 1000;
11
+
/** Input parameters for the {@link requestPolicyExchange}. */
export interface Options {
13
+
/** Predicate allowing you to selectively not upgrade `Operation`s.
16
+
When `shouldUpgrade` is set, it may be used to selectively return a boolean
17
+
per `Operation`. This allows certain `Operation`s to not be upgraded to a
18
+
`cache-and-network` policy, when `false` is returned.
20
+
By default, all `Operation`s are subject to be upgraded.
21
+
* operation to "cache-and-network". */
shouldUpgrade?: (op: Operation) => boolean;
23
+
/** The time-to-live (TTL) for which a request policy won't be upgraded.
26
+
The `ttl` defines the time frame in which the `Operation` won't be updated
27
+
with a `cache-and-network` request policy. If an `Operation` is sent again
28
+
and the `ttl` time period has expired, the policy is upgraded.
30
+
@defaultValue `300_000` - 5min
35
+
/** Exchange factory that upgrades request policies to `cache-and-network` for queries outside of a defined `ttl`.
37
+
* @param options - An {@link Options} configuration object.
38
+
* @returns the created request-policy {@link Exchange}.
41
+
* The `requestPolicyExchange` upgrades query operations based on {@link Options.ttl}.
42
+
* The `ttl` defines a timeframe outside of which a query's request policy is set to
43
+
* `cache-and-network` to refetch it in the background.
45
+
* You may define a {@link Options.shouldUpgrade} function to selectively ignore some
46
+
* operations by returning `false` there.
50
+
* requestPolicyExchange({
51
+
* // Upgrade when we haven't seen this operation for 1 second
53
+
* // and only upgrade operations that query the `todos` field.
54
+
* shouldUpgrade: op => op.kind === 'query' && op.query.definitions[0].name?.value === 'todos'
export const requestPolicyExchange = (options: Options): Exchange => ({