1 | import { atomsWithQuery, clientAtom, atomsWithMutation, atomsWithSubscription } from 'jotai-urql';
|
2 | export { clientAtom } from 'jotai-urql';
|
3 | import { atom } from 'jotai';
|
4 |
|
5 | function atomWithQuery(createQueryArgs, getClient = (get) => get(clientAtom)) {
|
6 | const getArgs = (get) => {
|
7 | const queryArgs = createQueryArgs(get);
|
8 | return [
|
9 | queryArgs.query,
|
10 | queryArgs.variables,
|
11 | {
|
12 | ...queryArgs.requestPolicy && {
|
13 | requestPolicy: queryArgs.requestPolicy
|
14 | },
|
15 | ...queryArgs.context
|
16 | }
|
17 | ];
|
18 | };
|
19 | const [dataAtom, statusAtom] = atomsWithQuery(getArgs, getClient);
|
20 | return atom(
|
21 | (get) => {
|
22 | const queryArgs = createQueryArgs(get);
|
23 | if (queryArgs.pause) {
|
24 | return null;
|
25 | }
|
26 | const status = get(statusAtom);
|
27 | if (status.error) {
|
28 | throw status.error;
|
29 | }
|
30 | if ("data" in status) {
|
31 | return status;
|
32 | }
|
33 | get(dataAtom);
|
34 | return status;
|
35 | },
|
36 | (_get, set, action) => {
|
37 | if (action.type === "reexecute") {
|
38 | console.warn(
|
39 | "DEPRECATED [atomWithQuery] use refetch instead of reexecute"
|
40 | );
|
41 | action.type = "refetch";
|
42 | }
|
43 | if ("opts" in action) {
|
44 | console.warn("DEPRECATED [atomWithQuery] action.opts is no longer used");
|
45 | }
|
46 | switch (action.type) {
|
47 | case "refetch": {
|
48 | return set(statusAtom, action);
|
49 | }
|
50 | }
|
51 | }
|
52 | );
|
53 | }
|
54 |
|
55 | function atomWithMutation(createQuery, getClient = (get) => get(clientAtom)) {
|
56 | const [, statusAtom] = atomsWithMutation(getClient);
|
57 | return atom(
|
58 | (get) => {
|
59 | const status = get(statusAtom);
|
60 | return status;
|
61 | },
|
62 | async (get, set, action) => {
|
63 | const args = [
|
64 | createQuery(get),
|
65 | action.variables,
|
66 | action.context || {}
|
67 | ];
|
68 | await set(statusAtom, args);
|
69 | return Promise.resolve(get(statusAtom, { unstable_promise: true })).then(
|
70 | (status) => {
|
71 | var _a;
|
72 | (_a = action.callback) == null ? void 0 : _a.call(action, status);
|
73 | if (status.error) {
|
74 | throw status.error;
|
75 | }
|
76 | }
|
77 | );
|
78 | }
|
79 | );
|
80 | }
|
81 |
|
82 | function atomWithSubscription(createSubscriptionArgs, getClient = (get) => get(clientAtom)) {
|
83 | const getArgs = (get) => {
|
84 | const subscriptionArgs = createSubscriptionArgs(get);
|
85 | return [
|
86 | subscriptionArgs.query,
|
87 | subscriptionArgs.variables,
|
88 | subscriptionArgs.context || {}
|
89 | ];
|
90 | };
|
91 | const [dataAtom, statusAtom] = atomsWithSubscription(getArgs, getClient);
|
92 | return atom(
|
93 | (get) => {
|
94 | const subscriptionArgs = createSubscriptionArgs(get);
|
95 | if (subscriptionArgs.pause) {
|
96 | return null;
|
97 | }
|
98 | const status = get(statusAtom);
|
99 | if (status.error) {
|
100 | throw status.error;
|
101 | }
|
102 | if ("data" in status) {
|
103 | return status;
|
104 | }
|
105 | get(dataAtom);
|
106 | return status;
|
107 | },
|
108 | (_get, set, action) => {
|
109 | switch (action.type) {
|
110 | case "refetch": {
|
111 | return set(statusAtom, action);
|
112 | }
|
113 | }
|
114 | }
|
115 | );
|
116 | }
|
117 |
|
118 | export { atomWithMutation, atomWithQuery, atomWithSubscription };
|