@firsthandjs/data-urql 0.5.0
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/LICENSE +21 -0
- package/README.md +87 -0
- package/dist/index.d.ts +57 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Firsthand contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# @firsthandjs/data-urql
|
|
2
|
+
|
|
3
|
+
urql documents as loaders for [`@firsthandjs/data`](https://www.npmjs.com/package/@firsthandjs/data).
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm install @firsthandjs/data-urql
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
0.30 kB gzip. It has **no dependency on urql** and no peer dependency either —
|
|
10
|
+
the two methods it uses are declared structurally. It was checked against
|
|
11
|
+
`@urql/core` 6, which has no React dependency of its own.
|
|
12
|
+
|
|
13
|
+
```tsx
|
|
14
|
+
import { Client, fetchExchange } from '@urql/core';
|
|
15
|
+
import { urqlLoader } from '@firsthandjs/data-urql';
|
|
16
|
+
import { useResource } from '@firsthandjs/data';
|
|
17
|
+
import InvoicesDocument from './invoices.gql';
|
|
18
|
+
|
|
19
|
+
const billing = urqlLoader(
|
|
20
|
+
new Client({
|
|
21
|
+
url: '/graphql',
|
|
22
|
+
exchanges: [fetchExchange],
|
|
23
|
+
fetchOptions: () => ({ headers: { authorization: `Bearer ${session.token.value}` } }),
|
|
24
|
+
}),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
function Invoices() {
|
|
28
|
+
const invoices = useResource((context) =>
|
|
29
|
+
billing(InvoicesDocument, { month: month.value })(context),
|
|
30
|
+
);
|
|
31
|
+
return <List items={invoices.data.value?.invoices ?? []} />;
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
`month.value` is read inside the loader, so changing the month runs the query
|
|
36
|
+
again and aborts the one in flight.
|
|
37
|
+
|
|
38
|
+
## Tags come out of the document
|
|
39
|
+
|
|
40
|
+
`@firsthandjs/data/vite` reads `@tag` and `@invalidates` directives at build
|
|
41
|
+
time and strips them, so what reaches urql is a plain GraphQL document. This
|
|
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.
|
|
44
|
+
|
|
45
|
+
```graphql
|
|
46
|
+
query Invoices($month: String!) @tag(name: "invoices", vars: ["month"]) {
|
|
47
|
+
invoices(month: $month) {
|
|
48
|
+
id
|
|
49
|
+
total
|
|
50
|
+
}
|
|
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
|
+
|
|
72
|
+
```graphql
|
|
73
|
+
mutation Pay($id: ID!) @invalidates(name: "invoices") {
|
|
74
|
+
pay(id: $id) {
|
|
75
|
+
id
|
|
76
|
+
paidAt
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```tsx
|
|
82
|
+
const pay = useAction((id: string, { signal, invalidates }) =>
|
|
83
|
+
billing(PayDocument, { id })({ signal, force: true, tags: invalidates }),
|
|
84
|
+
);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
MIT licensed. See the [data guide](https://github.com/firsthandjs/firsthand/blob/main/docs/guide/09-data.md).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* urql, as loaders for `@firsthandjs/data`.
|
|
3
|
+
*
|
|
4
|
+
* It takes a client you built and touches two of its methods. Your exchanges,
|
|
5
|
+
* your authentication, your retry policy stay where they are — including, and
|
|
6
|
+
* this is the one that matters, whether you keep `cacheExchange`. Without it
|
|
7
|
+
* urql is your transport and the resources are your state; with it urql also
|
|
8
|
+
* caches, which is a decision worth making on purpose rather than inheriting
|
|
9
|
+
* from a helper (ADR-0022).
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* import { Client, fetchExchange } from '@urql/core';
|
|
13
|
+
* import { urqlLoader } from '@firsthandjs/data-urql';
|
|
14
|
+
*
|
|
15
|
+
* const billing = urqlLoader(
|
|
16
|
+
* new Client({
|
|
17
|
+
* url: '/graphql',
|
|
18
|
+
* exchanges: [fetchExchange],
|
|
19
|
+
* fetchOptions: () => ({ headers: { authorization: `Bearer ${token.value}` } }),
|
|
20
|
+
* }),
|
|
21
|
+
* );
|
|
22
|
+
*
|
|
23
|
+
* const invoices = useResource((context) => billing(InvoicesDocument, { month: month.value })(context));
|
|
24
|
+
* ```
|
|
25
|
+
*
|
|
26
|
+
* There is no dependency on urql here, and no peer dependency either: the
|
|
27
|
+
* shape below is declared structurally, so this package has no opinion about
|
|
28
|
+
* which version you run. It was checked against `@urql/core` 6, which has no
|
|
29
|
+
* React dependency of its own.
|
|
30
|
+
*/
|
|
31
|
+
import { type DocumentArguments, type GraphQLDocument, type LoadContext, type Variables } from '@firsthandjs/data';
|
|
32
|
+
/** What urql gives back: a wonka source with a promise on it. */
|
|
33
|
+
export interface UrqlResult<T> {
|
|
34
|
+
data?: T;
|
|
35
|
+
error?: unknown;
|
|
36
|
+
}
|
|
37
|
+
/** The part of an urql client this package uses. Nothing else. */
|
|
38
|
+
export interface UrqlLike {
|
|
39
|
+
query(document: string, variables: Variables, context?: Record<string, unknown>): {
|
|
40
|
+
toPromise(): Promise<UrqlResult<unknown>>;
|
|
41
|
+
};
|
|
42
|
+
mutation(document: string, variables: Variables, context?: Record<string, unknown>): {
|
|
43
|
+
toPromise(): Promise<UrqlResult<unknown>>;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Binds a urql client so a `.gql` document can be a resource's loader.
|
|
48
|
+
*
|
|
49
|
+
* The tags come from the document's directives, bound against the variables of
|
|
50
|
+
* this call, and are declared before the request goes out — so an invalidation
|
|
51
|
+
* sent while it is in flight still finds it.
|
|
52
|
+
*
|
|
53
|
+
* `force` becomes `requestPolicy: 'network-only'`, which is what makes an
|
|
54
|
+
* invalidation reach past urql's cache if you kept one.
|
|
55
|
+
*/
|
|
56
|
+
export declare function urqlLoader(client: UrqlLike): <T, V extends Variables>(document: GraphQLDocument<T, V>, ...rest: DocumentArguments<V>) => ({ tags, force, signal }: LoadContext) => Promise<T>;
|
|
57
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,OAAO,EAEL,KAAK,iBAAiB,EACtB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,SAAS,EACf,MAAM,mBAAmB,CAAC;AAE3B,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;;;;;;;;;GASG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,QAAQ,IAIjC,CAAC,EAAE,CAAC,SAAS,SAAS,EAAE,UAAU,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,GAAG,MAAM,iBAAiB,CAAC,CAAC,CAAC,MACrF,yBAAyB,WAAW,KAAG,OAAO,CAAC,CAAC,CAAC,CA0B3D"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{resolveTags as m}from"@firsthandjs/data";function p(t){return(e,...i)=>async({tags:u,force:l,signal:c})=>{let n=i[0]??{},d=e.kind==="mutation"?e.invalidates:e.tags;u(...m(d,n));let r=await((o,a,s)=>e.kind==="mutation"?t.mutation(o,a,s):t.query(o,a,s))(e.source,n,{fetchOptions:{signal:c},requestPolicy:l?"network-only":"cache-first"}).toPromise();if(r.error!==void 0)throw r.error;return r.data}}export{p as urqlLoader};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@firsthandjs/data-urql",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Loaders for urql, for @firsthandjs/data. Takes your client; never configures it.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@firsthandjs/data": "0.5.0"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20.11.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public",
|
|
29
|
+
"provenance": true
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/firsthandjs/firsthand.git",
|
|
34
|
+
"directory": "packages/data-urql"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/firsthandjs/firsthand/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/firsthandjs/firsthand#readme",
|
|
40
|
+
"keywords": [
|
|
41
|
+
"firsthand",
|
|
42
|
+
"data",
|
|
43
|
+
"urql",
|
|
44
|
+
"resources",
|
|
45
|
+
"tags"
|
|
46
|
+
]
|
|
47
|
+
}
|