Skip to content

Commit 9b6337e

Browse files
feat(exchanges): add binding for @urql/exchange-request-policy (#247)
1 parent 2cb0205 commit 9b6337e

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

__tests__/Client_test.res

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,34 @@ describe("Client", () => {
241241
})
242242
})
243243
})
244+
245+
describe("requestPolicyExchange", () => {
246+
it("should return None for all requestPolicyExchange options if unspecified", () => {
247+
let requestPolicyExchangeOptions = Client.Exchanges.makeRequestPolicyExchangeOptions()
248+
249+
open Expect
250+
expect(requestPolicyExchangeOptions) |> toEqual({
251+
Client.Exchanges.shouldUpgrade: None,
252+
ttl: None,
253+
})
254+
})
255+
256+
it("should apply any specified options to the requestPolicyExchange", () => {
257+
let shouldUpgrade = (operation: Types.operation) =>
258+
operation.context.requestPolicy !== #CacheOnly
259+
260+
let requestPolicyExchangeOptions = Client.Exchanges.makeRequestPolicyExchangeOptions(
261+
~shouldUpgrade,
262+
~ttl=2000,
263+
(),
264+
)
265+
266+
open Expect
267+
expect(requestPolicyExchangeOptions) |> toEqual({
268+
Client.Exchanges.shouldUpgrade: Some(shouldUpgrade),
269+
ttl: Some(2000),
270+
})
271+
})
272+
})
244273
})
245274
})

docs/exchanges.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,47 @@ let client = Client.make(
237237

238238
Read more on the `retryExchange` [here](https://formidable.com/open-source/urql/docs/advanced/retry-operations/).
239239

240+
### `requestPolicyExchange`
241+
242+
The `requestPolicyExchange` allows `reason-urql` to automatically upgrade an operation's `requestPolicy` on a time-to-live basis. When the specified TTL has elapsed, `reason-urql` will either:
243+
244+
- Upgrade the `requestPolicy` of the operation to `cache-and-network` if no `shouldUpgrade` callback is specified, or:
245+
- Run the `shouldUpgrade` function to determine whether or not to upgrade the specific operation.
246+
247+
To use the `requestPolicyExchange`, add the package to your dependencies:
248+
249+
```sh
250+
yarn add @urql/exchange-request-policy
251+
```
252+
253+
Then, add the exchange to your array of `exchanges`, specifying the options you want to configure:
254+
255+
```rescript
256+
open ReasonUrql
257+
258+
let shouldUpgrade = (operation: Types.operation) =>
259+
operation.context.requestPolicy !== #CacheOnly
260+
261+
let requestPolicyExchangeOptions = Client.Exchanges.makeRequestPolicyExchangeOptions(
262+
~shouldUpgrade,
263+
~ttl=2000,
264+
(),
265+
)
266+
267+
let client = Client.make(
268+
~url="http://localhost:3000",
269+
~exchanges=[
270+
Client.Exchanges.dedupExchange,
271+
Client.Exchanges.cacheExchange,
272+
Client.Exchanges.requestPolicyExchange(requestPolicyExchangeOptions),
273+
Client.Exchanges.fetchExchange
274+
],
275+
()
276+
)
277+
```
278+
279+
Read more about the `requestPolicyExchange` [here](https://github.com/FormidableLabs/urql/tree/main/exchanges/request-policy).
280+
240281
## Custom Exchanges
241282

242283
`reason-urql` also allows you to write your own exchanges to modify outgoing GraphQL requests and incoming responses. To read up on the basics of exchanges, check out the excellent [`urql` documentation](https://formidable.com/open-source/urql/docs/concepts/exchanges/).

src/Client.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@ module Exchanges = {
100100
@module("@urql/exchange-retry")
101101
external retryExchange: retryExchangeOptions => t = "retryExchange"
102102

103+
type requestPolicyExchangeOptions = {
104+
shouldUpgrade: option<Types.operation => bool>,
105+
ttl: option<int>,
106+
}
107+
108+
let makeRequestPolicyExchangeOptions = (~shouldUpgrade=?, ~ttl=?, ()) => {
109+
shouldUpgrade: shouldUpgrade,
110+
ttl: ttl,
111+
}
112+
113+
@module("@urql/exchange-request-policy")
114+
external requestPolicyExchange: requestPolicyExchangeOptions => t = "requestPolicyExchange"
115+
103116
/* Specific types for the subscriptionExchange. */
104117
type observerLike<'value> = {
105118
next: 'value => unit,

src/Client.resi

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ module Exchanges: {
7676
@module("@urql/exchange-retry")
7777
external retryExchange: retryExchangeOptions => t = "retryExchange"
7878

79+
type requestPolicyExchangeOptions = {
80+
shouldUpgrade: option<Types.operation => bool>,
81+
ttl: option<int>,
82+
}
83+
84+
let makeRequestPolicyExchangeOptions: (
85+
~shouldUpgrade: Types.operation => bool=?,
86+
~ttl: int=?,
87+
unit,
88+
) => requestPolicyExchangeOptions
89+
90+
@module("@urql/exchange-request-policy")
91+
external requestPolicyExchange: requestPolicyExchangeOptions => t = "requestPolicyExchange"
92+
7993
/* Specific types for the subscriptionExchange. */
8094
type observerLike<'value> = {
8195
next: 'value => unit,

0 commit comments

Comments
 (0)