@mearie/react 0.3.4 → 0.4.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/dist/index.cjs CHANGED
@@ -30,10 +30,13 @@ const useQuery = ((query, variables, options) => {
30
30
  const initialized = (0, react.useRef)(false);
31
31
  const stableVariables = (0, react.useMemo)(() => (0, _mearie_core.stringify)(variables), [variables]);
32
32
  const stableOptions = (0, react.useMemo)(() => options, [options?.skip]);
33
- const execute = (0, react.useCallback)(() => {
33
+ const execute = (0, react.useCallback)((force = false) => {
34
34
  unsubscribe.current?.();
35
- if (stableOptions?.skip) return;
36
- if (!initialized.current && options?.initialData) setLoading(true);
35
+ if (!force && stableOptions?.skip) {
36
+ setLoading(false);
37
+ return;
38
+ }
39
+ if (initialized.current || !options?.initialData) setLoading(true);
37
40
  initialized.current = true;
38
41
  setError(void 0);
39
42
  unsubscribe.current = (0, _mearie_core_stream.pipe)(client.executeQuery(query, variables, stableOptions), (0, _mearie_core_stream.subscribe)({ next: (result) => {
@@ -42,7 +45,9 @@ const useQuery = ((query, variables, options) => {
42
45
  setError(new _mearie_core.AggregatedError(result.errors));
43
46
  setLoading(false);
44
47
  } else {
45
- setData(result.data);
48
+ const patches = result.metadata?.cache?.patches;
49
+ if (patches) setData((prev) => (0, _mearie_core.applyPatchesImmutable)(prev, patches));
50
+ else setData(result.data);
46
51
  setLoading(false);
47
52
  setError(void 0);
48
53
  }
@@ -53,6 +58,7 @@ const useQuery = ((query, variables, options) => {
53
58
  stableVariables,
54
59
  stableOptions
55
60
  ]);
61
+ const refetch = (0, react.useCallback)(() => execute(true), [execute]);
56
62
  (0, react.useEffect)(() => {
57
63
  execute();
58
64
  return () => unsubscribe.current?.();
@@ -62,7 +68,7 @@ const useQuery = ((query, variables, options) => {
62
68
  loading,
63
69
  error,
64
70
  metadata,
65
- refetch: execute
71
+ refetch
66
72
  };
67
73
  });
68
74
 
@@ -172,8 +178,14 @@ const useFragment = ((fragment, fragmentRef, options) => {
172
178
  return () => {};
173
179
  }
174
180
  return (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), (0, _mearie_core_stream.subscribe)({ next: (result) => {
175
- if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
176
- storeRef.current = {
181
+ const patches = result.metadata?.cache?.patches;
182
+ if (patches) {
183
+ const prevData = storeRef.current?.data;
184
+ storeRef.current = {
185
+ data: (0, _mearie_core.applyPatchesImmutable)(prevData, patches),
186
+ metadata: result.metadata
187
+ };
188
+ } else if (result.data !== void 0) storeRef.current = {
177
189
  data: result.data,
178
190
  metadata: result.metadata
179
191
  };
@@ -189,7 +201,7 @@ const useFragment = ((fragment, fragmentRef, options) => {
189
201
  if (fragmentRef == null) return NULL_STORE;
190
202
  if (storeRef.current === void 0) {
191
203
  const result = (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), _mearie_core_stream.peek);
192
- if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
204
+ if (result.data === void 0) throw new Error("Fragment data not found");
193
205
  storeRef.current = {
194
206
  data: result.data,
195
207
  metadata: result.metadata
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { AggregatedError, stringify } from "@mearie/core";
1
+ import { AggregatedError, applyPatchesImmutable, stringify } from "@mearie/core";
2
2
  import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
3
3
  import { jsx } from "react/jsx-runtime";
4
4
  import { collect, peek, pipe, subscribe, take } from "@mearie/core/stream";
@@ -31,10 +31,13 @@ const useQuery = ((query, variables, options) => {
31
31
  const initialized = useRef(false);
32
32
  const stableVariables = useMemo(() => stringify(variables), [variables]);
33
33
  const stableOptions = useMemo(() => options, [options?.skip]);
34
- const execute = useCallback(() => {
34
+ const execute = useCallback((force = false) => {
35
35
  unsubscribe.current?.();
36
- if (stableOptions?.skip) return;
37
- if (!initialized.current && options?.initialData) setLoading(true);
36
+ if (!force && stableOptions?.skip) {
37
+ setLoading(false);
38
+ return;
39
+ }
40
+ if (initialized.current || !options?.initialData) setLoading(true);
38
41
  initialized.current = true;
39
42
  setError(void 0);
40
43
  unsubscribe.current = pipe(client.executeQuery(query, variables, stableOptions), subscribe({ next: (result) => {
@@ -43,7 +46,9 @@ const useQuery = ((query, variables, options) => {
43
46
  setError(new AggregatedError(result.errors));
44
47
  setLoading(false);
45
48
  } else {
46
- setData(result.data);
49
+ const patches = result.metadata?.cache?.patches;
50
+ if (patches) setData((prev) => applyPatchesImmutable(prev, patches));
51
+ else setData(result.data);
47
52
  setLoading(false);
48
53
  setError(void 0);
49
54
  }
@@ -54,6 +59,7 @@ const useQuery = ((query, variables, options) => {
54
59
  stableVariables,
55
60
  stableOptions
56
61
  ]);
62
+ const refetch = useCallback(() => execute(true), [execute]);
57
63
  useEffect(() => {
58
64
  execute();
59
65
  return () => unsubscribe.current?.();
@@ -63,7 +69,7 @@ const useQuery = ((query, variables, options) => {
63
69
  loading,
64
70
  error,
65
71
  metadata,
66
- refetch: execute
72
+ refetch
67
73
  };
68
74
  });
69
75
 
@@ -173,8 +179,14 @@ const useFragment = ((fragment, fragmentRef, options) => {
173
179
  return () => {};
174
180
  }
175
181
  return pipe(client.executeFragment(fragment, fragmentRef, options), subscribe({ next: (result) => {
176
- if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
177
- storeRef.current = {
182
+ const patches = result.metadata?.cache?.patches;
183
+ if (patches) {
184
+ const prevData = storeRef.current?.data;
185
+ storeRef.current = {
186
+ data: applyPatchesImmutable(prevData, patches),
187
+ metadata: result.metadata
188
+ };
189
+ } else if (result.data !== void 0) storeRef.current = {
178
190
  data: result.data,
179
191
  metadata: result.metadata
180
192
  };
@@ -190,7 +202,7 @@ const useFragment = ((fragment, fragmentRef, options) => {
190
202
  if (fragmentRef == null) return NULL_STORE;
191
203
  if (storeRef.current === void 0) {
192
204
  const result = pipe(client.executeFragment(fragment, fragmentRef, options), peek);
193
- if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
205
+ if (result.data === void 0) throw new Error("Fragment data not found");
194
206
  storeRef.current = {
195
207
  data: result.data,
196
208
  metadata: result.metadata
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mearie/react",
3
- "version": "0.3.4",
3
+ "version": "0.4.0",
4
4
  "description": "Type-safe, zero-overhead GraphQL client",
5
5
  "keywords": [
6
6
  "graphql",
@@ -52,11 +52,15 @@
52
52
  "README.md"
53
53
  ],
54
54
  "dependencies": {
55
- "@mearie/core": "0.5.2"
55
+ "@mearie/core": "0.6.0"
56
56
  },
57
57
  "devDependencies": {
58
58
  "@types/react": "^19.2.14",
59
+ "@types/react-dom": "^19.2.3",
60
+ "@vitejs/plugin-react": "^5.1.4",
61
+ "happy-dom": "^20.8.3",
59
62
  "react": "^19.2.4",
63
+ "react-dom": "^19.2.4",
60
64
  "tsdown": "^0.20.3",
61
65
  "typescript": "^5.9.3"
62
66
  },