Mirror: The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.

chore(request-policy): add TSDoc for the requestPolicyExchange (#3036)

Co-authored-by: Phil Pluckthun <phil@kitten.sh>

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