@firsthandjs/data-urql 0.5.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -42
- package/dist/index.d.ts +46 -22
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,34 +1,36 @@
|
|
|
1
1
|
# @firsthandjs/data-urql
|
|
2
2
|
|
|
3
|
-
urql
|
|
3
|
+
urql as a client for [`@firsthandjs/data`](https://www.npmjs.com/package/@firsthandjs/data).
|
|
4
4
|
|
|
5
5
|
```
|
|
6
6
|
npm install @firsthandjs/data-urql
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
0.
|
|
9
|
+
0.53 kB gzip. It has **no dependency on urql** and no peer dependency either —
|
|
10
10
|
the two methods it uses are declared structurally. It was checked against
|
|
11
11
|
`@urql/core` 6, which has no React dependency of its own.
|
|
12
12
|
|
|
13
13
|
```tsx
|
|
14
14
|
import { Client, fetchExchange } from '@urql/core';
|
|
15
|
-
import {
|
|
16
|
-
import { useResource } from '@firsthandjs/data';
|
|
15
|
+
import { createUrqlClient } from '@firsthandjs/data-urql';
|
|
16
|
+
import { useAction, useResource } from '@firsthandjs/data';
|
|
17
17
|
import InvoicesDocument from './invoices.gql';
|
|
18
|
+
import PayDocument from './pay.gql';
|
|
18
19
|
|
|
19
|
-
const billing =
|
|
20
|
-
new Client({
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}),
|
|
20
|
+
export const billing = createUrqlClient(
|
|
21
|
+
new Client({ url: '/graphql', exchanges: [fetchExchange] }),
|
|
22
|
+
// Read per request and untracked: the token may change, and a resource must
|
|
23
|
+
// not depend on it.
|
|
24
|
+
{ headers: () => ({ authorization: `Bearer ${session.token.peek()}` }) },
|
|
25
25
|
);
|
|
26
26
|
|
|
27
27
|
function Invoices() {
|
|
28
|
-
const invoices = useResource((
|
|
29
|
-
billing(InvoicesDocument, { month: month.value })(
|
|
28
|
+
const invoices = useResource(({ request }) =>
|
|
29
|
+
billing.query(InvoicesDocument, { month: month.value })(request),
|
|
30
30
|
);
|
|
31
|
-
|
|
31
|
+
const pay = useAction((id: string, { request }) => billing.mutate(PayDocument, { id })(request));
|
|
32
|
+
|
|
33
|
+
return <List items={invoices.data.value?.invoices ?? []} onPay={(id) => void pay.run(id)} />;
|
|
32
34
|
}
|
|
33
35
|
```
|
|
34
36
|
|
|
@@ -38,9 +40,7 @@ again and aborts the one in flight.
|
|
|
38
40
|
## Tags come out of the document
|
|
39
41
|
|
|
40
42
|
`@firsthandjs/data/vite` reads `@tag` and `@invalidates` directives at build
|
|
41
|
-
time and strips them, so what reaches urql is a plain GraphQL document.
|
|
42
|
-
package binds them against the variables of the call and declares them _before_
|
|
43
|
-
the request goes out — so an invalidation arriving mid-flight still finds it.
|
|
43
|
+
time and strips them, so what reaches urql is a plain GraphQL document.
|
|
44
44
|
|
|
45
45
|
```graphql
|
|
46
46
|
query Invoices($month: String!) @tag(name: "invoices", vars: ["month"]) {
|
|
@@ -49,27 +49,7 @@ query Invoices($month: String!) @tag(name: "invoices", vars: ["month"]) {
|
|
|
49
49
|
total
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## The decision this package leaves to you: `cacheExchange`
|
|
55
|
-
|
|
56
|
-
- **Without it,** urql is your transport and the resources are your state. One
|
|
57
|
-
source of truth.
|
|
58
|
-
- **With it,** urql also caches — which is a real choice, not a default worth
|
|
59
|
-
inheriting from a helper. `force` is passed on as
|
|
60
|
-
`requestPolicy: 'network-only'`, so an invalidation reaches past that cache.
|
|
61
|
-
|
|
62
|
-
If you want urql's cache to be the source of truth for an entity shown in many
|
|
63
|
-
places at once, subscribe to it with `fromObservable` instead of loading it
|
|
64
|
-
per call site. See [ADR-0022](https://github.com/firsthandjs/firsthand/blob/main/docs/adr/0022-resources-not-a-cache.md).
|
|
65
|
-
|
|
66
|
-
## Mutations
|
|
67
|
-
|
|
68
|
-
A document whose `kind` is `mutation` goes to `client.mutation`, and what it
|
|
69
|
-
declares is its `@invalidates` directives — so an action hands the document's
|
|
70
|
-
own declaration straight to the store:
|
|
71
52
|
|
|
72
|
-
```graphql
|
|
73
53
|
mutation Pay($id: ID!) @invalidates(name: "invoices") {
|
|
74
54
|
pay(id: $id) {
|
|
75
55
|
id
|
|
@@ -78,10 +58,22 @@ mutation Pay($id: ID!) @invalidates(name: "invoices") {
|
|
|
78
58
|
}
|
|
79
59
|
```
|
|
80
60
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
61
|
+
`query` declares the `@tag` directives into the request before it goes out — so
|
|
62
|
+
an invalidation arriving mid-flight still finds it — and `mutate` declares the
|
|
63
|
+
`@invalidates`. Inside an action the request's tags _are_ the store's
|
|
64
|
+
invalidation, so neither call site writes a tag at all.
|
|
65
|
+
|
|
66
|
+
## The decision this package leaves to you: `cacheExchange`
|
|
67
|
+
|
|
68
|
+
- **Without it,** urql is your transport and the resources are your state. Pass
|
|
69
|
+
`cache: { ttl }` here if you want one anyway; it is the same cache the rest
|
|
70
|
+
of `@firsthandjs/data` uses, queries only, keyed by operation and variables.
|
|
71
|
+
- **With it,** urql also caches — a real choice, not a default worth inheriting
|
|
72
|
+
from a helper. `force` is passed on as `requestPolicy: 'network-only'`, so an
|
|
73
|
+
invalidation reaches past it.
|
|
74
|
+
|
|
75
|
+
What you should not have is both, because two caches over one piece of data
|
|
76
|
+
disagree. If urql's cache should be the source of truth for an entity shown in
|
|
77
|
+
many places, subscribe to it with `fromObservable` instead.
|
|
86
78
|
|
|
87
|
-
MIT licensed. See the [data guide](https://github.com/firsthandjs/firsthand/blob/main/docs/guide/09-data.md).
|
|
79
|
+
MIT licensed. See the [data guide](https://github.com/firsthandjs/firsthand/blob/main/docs/guide/09-data.md#urql).
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* urql, as
|
|
2
|
+
* urql, as a client for `@firsthandjs/data`.
|
|
3
3
|
*
|
|
4
4
|
* It takes a client you built and touches two of its methods. Your exchanges,
|
|
5
5
|
* your authentication, your retry policy stay where they are — including, and
|
|
@@ -10,25 +10,26 @@
|
|
|
10
10
|
*
|
|
11
11
|
* ```ts
|
|
12
12
|
* import { Client, fetchExchange } from '@urql/core';
|
|
13
|
-
* import {
|
|
13
|
+
* import { createUrqlClient } from '@firsthandjs/data-urql';
|
|
14
14
|
*
|
|
15
|
-
* const billing =
|
|
16
|
-
* new Client({
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* }),
|
|
15
|
+
* export const billing = createUrqlClient(
|
|
16
|
+
* new Client({ url: '/graphql', exchanges: [fetchExchange] }),
|
|
17
|
+
* // Read per request and untracked: the token may change, and a resource
|
|
18
|
+
* // must not depend on it.
|
|
19
|
+
* { headers: () => ({ authorization: `Bearer ${token.value}` }) },
|
|
21
20
|
* );
|
|
22
21
|
*
|
|
23
|
-
* const invoices = useResource((
|
|
22
|
+
* const invoices = useResource(({ request }) =>
|
|
23
|
+
* billing.query(InvoicesDocument, { month: month.value })(request),
|
|
24
|
+
* );
|
|
24
25
|
* ```
|
|
25
26
|
*
|
|
26
27
|
* There is no dependency on urql here, and no peer dependency either: the
|
|
27
|
-
*
|
|
28
|
+
* shapes below are declared structurally, so this package has no opinion about
|
|
28
29
|
* which version you run. It was checked against `@urql/core` 6, which has no
|
|
29
30
|
* React dependency of its own.
|
|
30
31
|
*/
|
|
31
|
-
import { type DocumentArguments, type GraphQLDocument, type
|
|
32
|
+
import { type CacheClient, type CacheOptions, type DocumentArguments, type GraphQLDocument, type Loader, type Variables } from '@firsthandjs/data';
|
|
32
33
|
/** What urql gives back: a wonka source with a promise on it. */
|
|
33
34
|
export interface UrqlResult<T> {
|
|
34
35
|
data?: T;
|
|
@@ -43,15 +44,38 @@ export interface UrqlLike {
|
|
|
43
44
|
toPromise(): Promise<UrqlResult<unknown>>;
|
|
44
45
|
};
|
|
45
46
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
47
|
+
export interface UrqlClientOptions {
|
|
48
|
+
/**
|
|
49
|
+
* Headers for every request, sent through urql's `fetchOptions`. A function
|
|
50
|
+
* is called **per request and untracked**, which is what lets a token change
|
|
51
|
+
* without making every resource depend on it.
|
|
52
|
+
*/
|
|
53
|
+
readonly headers?: Record<string, string> | (() => Record<string, string>);
|
|
54
|
+
/**
|
|
55
|
+
* A cache in front of urql: `false` (the default, because urql has one of
|
|
56
|
+
* its own if you kept `cacheExchange`), options for a cache of this client's
|
|
57
|
+
* own, or a `CacheClient` shared with the rest of the application. Queries
|
|
58
|
+
* only, keyed by operation and variables.
|
|
59
|
+
*/
|
|
60
|
+
readonly cache?: false | CacheOptions | CacheClient;
|
|
61
|
+
/** Merged into urql's operation context for every request. */
|
|
62
|
+
readonly context?: Record<string, unknown>;
|
|
63
|
+
}
|
|
64
|
+
export interface UrqlClient {
|
|
65
|
+
/**
|
|
66
|
+
* A query, as a loader. Declares the document's `@tag` directives before the
|
|
67
|
+
* request goes out, so an invalidation sent while it is in flight finds it.
|
|
68
|
+
*/
|
|
69
|
+
query<T, V extends Variables>(document: GraphQLDocument<T, V>, ...rest: DocumentArguments<V>): Loader<T>;
|
|
70
|
+
/**
|
|
71
|
+
* A mutation, as a loader. Declares the document's `@invalidates` directives
|
|
72
|
+
* into the request — which inside an action is the store's `invalidates`, so
|
|
73
|
+
* the document's own declaration reaches the store with nothing to wire.
|
|
74
|
+
*/
|
|
75
|
+
mutate<T, V extends Variables>(document: GraphQLDocument<T, V>, ...rest: DocumentArguments<V>): Loader<T>;
|
|
76
|
+
/** A copy with some options replaced. The cache is shared unless replaced. */
|
|
77
|
+
with(options: UrqlClientOptions): UrqlClient;
|
|
78
|
+
readonly cache: CacheClient | undefined;
|
|
79
|
+
}
|
|
80
|
+
export declare function createUrqlClient(client: UrqlLike, options?: UrqlClientOptions): UrqlClient;
|
|
57
81
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,OAAO,EAGL,KAAK,WAAW,EAChB,KAAK,YAAY,EAEjB,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,MAAM,EACX,KAAK,SAAS,EACf,MAAM,mBAAmB,CAAC;AAG3B,iEAAiE;AACjE,MAAM,WAAW,UAAU,CAAC,CAAC;IAC3B,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,kEAAkE;AAClE,MAAM,WAAW,QAAQ;IACvB,KAAK,CACH,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,SAAS,EACpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC;QAAE,SAAS,IAAI,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAA;KAAE,CAAC;IACjD,QAAQ,CACN,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,SAAS,EACpB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAChC;QAAE,SAAS,IAAI,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC,CAAA;KAAE,CAAC;CAClD;AAED,MAAM,WAAW,iBAAiB;IAChC;;;;OAIG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAC3E;;;;;OAKG;IACH,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,YAAY,GAAG,WAAW,CAAC;IACpD,8DAA8D;IAC9D,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAC5C;AAED,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,KAAK,CAAC,CAAC,EAAE,CAAC,SAAS,SAAS,EAC1B,QAAQ,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,EAC/B,GAAG,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAC5B,MAAM,CAAC,CAAC,CAAC,CAAC;IACb;;;;OAIG;IACH,MAAM,CAAC,CAAC,EAAE,CAAC,SAAS,SAAS,EAC3B,QAAQ,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,EAC/B,GAAG,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAC5B,MAAM,CAAC,CAAC,CAAC,CAAC;IACb,8EAA8E;IAC9E,IAAI,CAAC,OAAO,EAAE,iBAAiB,GAAG,UAAU,CAAC;IAC7C,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,SAAS,CAAC;CACzC;AAED,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,QAAQ,EAAE,OAAO,GAAE,iBAAsB,GAAG,UAAU,CAsF9F"}
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{resolveTags as
|
|
1
|
+
import{createCacheClient as h,resolveTags as y}from"@firsthandjs/data";import{untrack as T}from"@firsthandjs/core";function V(c,n={}){let o=n.cache===void 0||n.cache===!1?void 0:"read"in n.cache?n.cache:h(n.cache),l=async(e,t,s,a)=>{let r=typeof n.headers=="function"?T(n.headers):n.headers,i={...n.context,fetchOptions:{signal:a.signal,...r===void 0?{}:{headers:r}},requestPolicy:a.force?"network-only":"cache-first"},u=await(e==="mutation"?c.mutation(t,s,i):c.query(t,s,i)).toPromise();if(u.error!==void 0)throw u.error;return u.data},d=(e,t,s)=>async a=>{let r=s[0]??{};if(a.tags?.(...y(e==="mutation"?t.invalidates:t.tags,r)),o===void 0||e==="mutation")return await l(e,t.source,r,a);let i=`${t.operation}(${JSON.stringify(r)})`;return await o.read(i,m=>l(e,t.source,r,m))(a)};return{cache:o,query:(e,...t)=>d("query",e,t),mutate:(e,...t)=>d("mutation",e,t),with:e=>V(c,{...n,...e,cache:e.cache??o??!1})}}export{V as createUrqlClient};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@firsthandjs/data-urql",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"description": "Loaders for urql, for @firsthandjs/data. Takes your client; never configures it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"LICENSE"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@firsthandjs/data": "0.
|
|
22
|
+
"@firsthandjs/data": "0.6.1"
|
|
23
23
|
},
|
|
24
24
|
"engines": {
|
|
25
25
|
"node": ">=20.11.0"
|