@mearie/react 0.2.3 → 0.3.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
@@ -25,6 +25,7 @@ const useQuery = ((query, variables, options) => {
25
25
  const [data, setData] = (0, react.useState)(options?.initialData);
26
26
  const [loading, setLoading] = (0, react.useState)(!options?.skip && !options?.initialData);
27
27
  const [error, setError] = (0, react.useState)();
28
+ const [metadata, setMetadata] = (0, react.useState)();
28
29
  const unsubscribe = (0, react.useRef)(null);
29
30
  const initialized = (0, react.useRef)(false);
30
31
  const stableVariables = (0, react.useMemo)(() => (0, _mearie_core.stringify)(variables), [variables]);
@@ -36,6 +37,7 @@ const useQuery = ((query, variables, options) => {
36
37
  initialized.current = true;
37
38
  setError(void 0);
38
39
  unsubscribe.current = (0, _mearie_core_stream.pipe)(client.executeQuery(query, variables, stableOptions), (0, _mearie_core_stream.subscribe)({ next: (result) => {
40
+ setMetadata(result.metadata);
39
41
  if (result.errors && result.errors.length > 0) {
40
42
  setError(new _mearie_core.AggregatedError(result.errors));
41
43
  setLoading(false);
@@ -59,6 +61,7 @@ const useQuery = ((query, variables, options) => {
59
61
  data,
60
62
  loading,
61
63
  error,
64
+ metadata,
62
65
  refetch: execute
63
66
  };
64
67
  });
@@ -70,6 +73,7 @@ const useSubscription = (subscription, ...[variables, options]) => {
70
73
  const [data, setData] = (0, react.useState)();
71
74
  const [loading, setLoading] = (0, react.useState)(!options?.skip);
72
75
  const [error, setError] = (0, react.useState)();
76
+ const [metadata, setMetadata] = (0, react.useState)();
73
77
  const unsubscribe = (0, react.useRef)(null);
74
78
  const stableVariables = (0, react.useMemo)(() => (0, _mearie_core.stringify)(variables), [variables]);
75
79
  const stableOptions = (0, react.useMemo)(() => options, [
@@ -83,6 +87,7 @@ const useSubscription = (subscription, ...[variables, options]) => {
83
87
  setLoading(true);
84
88
  setError(void 0);
85
89
  unsubscribe.current = (0, _mearie_core_stream.pipe)(client.executeSubscription(subscription, variables, stableOptions), (0, _mearie_core_stream.subscribe)({ next: (result) => {
90
+ setMetadata(result.metadata);
86
91
  if (result.errors && result.errors.length > 0) {
87
92
  const err = new _mearie_core.AggregatedError(result.errors);
88
93
  setError(err);
@@ -109,7 +114,8 @@ const useSubscription = (subscription, ...[variables, options]) => {
109
114
  return {
110
115
  data,
111
116
  loading,
112
- error
117
+ error,
118
+ metadata
113
119
  };
114
120
  };
115
121
 
@@ -120,17 +126,21 @@ const useMutation = (mutation) => {
120
126
  const [data, setData] = (0, react.useState)();
121
127
  const [loading, setLoading] = (0, react.useState)(false);
122
128
  const [error, setError] = (0, react.useState)();
129
+ const [metadata, setMetadata] = (0, react.useState)();
123
130
  return [(0, react.useCallback)(async (variables, options) => {
131
+ setMetadata(void 0);
124
132
  setLoading(true);
125
133
  setError(void 0);
126
134
  try {
127
135
  const result = await (0, _mearie_core_stream.pipe)(client.executeMutation(mutation, variables, options), (0, _mearie_core_stream.take)(1), _mearie_core_stream.collect);
128
136
  if (result.errors && result.errors.length > 0) {
129
137
  const err = new _mearie_core.AggregatedError(result.errors);
138
+ setMetadata(result.metadata);
130
139
  setError(err);
131
140
  setLoading(false);
132
141
  throw err;
133
142
  }
143
+ setMetadata(result.metadata);
134
144
  setData(result.data);
135
145
  setLoading(false);
136
146
  return result.data;
@@ -142,23 +152,31 @@ const useMutation = (mutation) => {
142
152
  }, [client, mutation]), {
143
153
  data,
144
154
  loading,
145
- error
155
+ error,
156
+ metadata
146
157
  }];
147
158
  };
148
159
 
149
160
  //#endregion
150
161
  //#region src/use-fragment.ts
162
+ const NULL_STORE = {
163
+ data: null,
164
+ metadata: void 0
165
+ };
151
166
  const useFragment = ((fragment, fragmentRef, options) => {
152
167
  const client = useClient();
153
- const dataRef = (0, react.useRef)(void 0);
168
+ const storeRef = (0, react.useRef)(void 0);
154
169
  const subscribe_ = (0, react.useCallback)((onChange) => {
155
170
  if (fragmentRef == null) {
156
- dataRef.current = null;
171
+ storeRef.current = NULL_STORE;
157
172
  return () => {};
158
173
  }
159
174
  return (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), (0, _mearie_core_stream.subscribe)({ next: (result) => {
160
175
  if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
161
- dataRef.current = result.data;
176
+ storeRef.current = {
177
+ data: result.data,
178
+ metadata: result.metadata
179
+ };
162
180
  onChange();
163
181
  } }));
164
182
  }, [
@@ -168,23 +186,31 @@ const useFragment = ((fragment, fragmentRef, options) => {
168
186
  options
169
187
  ]);
170
188
  const snapshot = (0, react.useCallback)(() => {
171
- if (fragmentRef == null) return null;
172
- if (dataRef.current === void 0) {
189
+ if (fragmentRef == null) return NULL_STORE;
190
+ if (storeRef.current === void 0) {
173
191
  const result = (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), _mearie_core_stream.peek);
174
192
  if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
175
- dataRef.current = result.data;
193
+ storeRef.current = {
194
+ data: result.data,
195
+ metadata: result.metadata
196
+ };
176
197
  }
177
- return dataRef.current;
198
+ return storeRef.current;
178
199
  }, [
179
200
  client,
180
201
  fragment,
181
202
  fragmentRef,
182
203
  options
183
204
  ]);
184
- const data = (0, react.useSyncExternalStore)(subscribe_, snapshot, snapshot);
185
- return { get data() {
186
- return data;
187
- } };
205
+ const store = (0, react.useSyncExternalStore)(subscribe_, snapshot, snapshot);
206
+ return {
207
+ get data() {
208
+ return store.data;
209
+ },
210
+ get metadata() {
211
+ return store.metadata;
212
+ }
213
+ };
188
214
  });
189
215
 
190
216
  //#endregion
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { AggregatedError, Artifact, Client, DataOf, FragmentOptions, FragmentRefs, MutationOptions, QueryOptions, SchemaMeta, SubscriptionOptions, VariablesOf } from "@mearie/core";
1
+ import { AggregatedError, Artifact, Client, DataOf, FragmentOptions, FragmentRefs, MutationOptions, OperationResult, QueryOptions, SchemaMeta, SubscriptionOptions, VariablesOf } from "@mearie/core";
2
2
  import { ReactNode } from "react";
3
3
  export * from "@mearie/core";
4
4
 
@@ -21,32 +21,38 @@ type Query<T extends Artifact<'query'>> = {
21
21
  data: undefined;
22
22
  loading: true;
23
23
  error: undefined;
24
+ metadata: OperationResult['metadata'];
24
25
  refetch: () => void;
25
26
  } | {
26
27
  data: DataOf<T>;
27
28
  loading: false;
28
29
  error: undefined;
30
+ metadata: OperationResult['metadata'];
29
31
  refetch: () => void;
30
32
  } | {
31
33
  data: DataOf<T> | undefined;
32
34
  loading: false;
33
35
  error: AggregatedError;
36
+ metadata: OperationResult['metadata'];
34
37
  refetch: () => void;
35
38
  };
36
39
  type DefinedQuery<T extends Artifact<'query'>> = {
37
40
  data: DataOf<T>;
38
41
  loading: true;
39
42
  error: undefined;
43
+ metadata: OperationResult['metadata'];
40
44
  refetch: () => void;
41
45
  } | {
42
46
  data: DataOf<T>;
43
47
  loading: false;
44
48
  error: undefined;
49
+ metadata: OperationResult['metadata'];
45
50
  refetch: () => void;
46
51
  } | {
47
52
  data: DataOf<T>;
48
53
  loading: false;
49
54
  error: AggregatedError;
55
+ metadata: OperationResult['metadata'];
50
56
  refetch: () => void;
51
57
  };
52
58
  type UseQueryFn = {
@@ -62,14 +68,17 @@ type Subscription<T extends Artifact<'subscription'>> = {
62
68
  data: undefined;
63
69
  loading: true;
64
70
  error: undefined;
71
+ metadata: OperationResult['metadata'];
65
72
  } | {
66
73
  data: DataOf<T> | undefined;
67
74
  loading: false;
68
75
  error: undefined;
76
+ metadata: OperationResult['metadata'];
69
77
  } | {
70
78
  data: DataOf<T> | undefined;
71
79
  loading: false;
72
80
  error: AggregatedError;
81
+ metadata: OperationResult['metadata'];
73
82
  };
74
83
  type UseSubscriptionOptions<T extends Artifact<'subscription'>> = SubscriptionOptions & {
75
84
  skip?: boolean;
@@ -83,14 +92,17 @@ type MutationResult<T extends Artifact<'mutation'>> = {
83
92
  data: undefined;
84
93
  loading: true;
85
94
  error: undefined;
95
+ metadata: OperationResult['metadata'];
86
96
  } | {
87
97
  data: DataOf<T> | undefined;
88
98
  loading: false;
89
99
  error: undefined;
100
+ metadata: OperationResult['metadata'];
90
101
  } | {
91
102
  data: DataOf<T> | undefined;
92
103
  loading: false;
93
104
  error: AggregatedError;
105
+ metadata: OperationResult['metadata'];
94
106
  };
95
107
  type UseMutationOptions = MutationOptions;
96
108
  type Mutation<T extends Artifact<'mutation'>> = [(...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseMutationOptions?] : [VariablesOf<T>, UseMutationOptions?]) => Promise<DataOf<T>>, MutationResult<T>];
@@ -100,12 +112,15 @@ declare const useMutation: <T extends Artifact<"mutation">>(mutation: T) => Muta
100
112
  type UseFragmentOptions = FragmentOptions;
101
113
  type Fragment<T extends Artifact<'fragment'>> = {
102
114
  data: DataOf<T>;
115
+ metadata: OperationResult['metadata'];
103
116
  };
104
117
  type FragmentList<T extends Artifact<'fragment'>> = {
105
118
  data: DataOf<T>[];
119
+ metadata: OperationResult['metadata'];
106
120
  };
107
121
  type OptionalFragment<T extends Artifact<'fragment'>> = {
108
122
  data: DataOf<T> | null;
123
+ metadata: OperationResult['metadata'];
109
124
  };
110
125
  type UseFragmentFn = {
111
126
  <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>[], options?: UseFragmentOptions): FragmentList<T>;
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { AggregatedError, Artifact, Client, DataOf, FragmentOptions, FragmentRefs, MutationOptions, QueryOptions, SchemaMeta, SubscriptionOptions, VariablesOf } from "@mearie/core";
1
+ import { AggregatedError, Artifact, Client, DataOf, FragmentOptions, FragmentRefs, MutationOptions, OperationResult, QueryOptions, SchemaMeta, SubscriptionOptions, VariablesOf } from "@mearie/core";
2
2
  import { ReactNode } from "react";
3
3
  export * from "@mearie/core";
4
4
 
@@ -21,32 +21,38 @@ type Query<T extends Artifact<'query'>> = {
21
21
  data: undefined;
22
22
  loading: true;
23
23
  error: undefined;
24
+ metadata: OperationResult['metadata'];
24
25
  refetch: () => void;
25
26
  } | {
26
27
  data: DataOf<T>;
27
28
  loading: false;
28
29
  error: undefined;
30
+ metadata: OperationResult['metadata'];
29
31
  refetch: () => void;
30
32
  } | {
31
33
  data: DataOf<T> | undefined;
32
34
  loading: false;
33
35
  error: AggregatedError;
36
+ metadata: OperationResult['metadata'];
34
37
  refetch: () => void;
35
38
  };
36
39
  type DefinedQuery<T extends Artifact<'query'>> = {
37
40
  data: DataOf<T>;
38
41
  loading: true;
39
42
  error: undefined;
43
+ metadata: OperationResult['metadata'];
40
44
  refetch: () => void;
41
45
  } | {
42
46
  data: DataOf<T>;
43
47
  loading: false;
44
48
  error: undefined;
49
+ metadata: OperationResult['metadata'];
45
50
  refetch: () => void;
46
51
  } | {
47
52
  data: DataOf<T>;
48
53
  loading: false;
49
54
  error: AggregatedError;
55
+ metadata: OperationResult['metadata'];
50
56
  refetch: () => void;
51
57
  };
52
58
  type UseQueryFn = {
@@ -62,14 +68,17 @@ type Subscription<T extends Artifact<'subscription'>> = {
62
68
  data: undefined;
63
69
  loading: true;
64
70
  error: undefined;
71
+ metadata: OperationResult['metadata'];
65
72
  } | {
66
73
  data: DataOf<T> | undefined;
67
74
  loading: false;
68
75
  error: undefined;
76
+ metadata: OperationResult['metadata'];
69
77
  } | {
70
78
  data: DataOf<T> | undefined;
71
79
  loading: false;
72
80
  error: AggregatedError;
81
+ metadata: OperationResult['metadata'];
73
82
  };
74
83
  type UseSubscriptionOptions<T extends Artifact<'subscription'>> = SubscriptionOptions & {
75
84
  skip?: boolean;
@@ -83,14 +92,17 @@ type MutationResult<T extends Artifact<'mutation'>> = {
83
92
  data: undefined;
84
93
  loading: true;
85
94
  error: undefined;
95
+ metadata: OperationResult['metadata'];
86
96
  } | {
87
97
  data: DataOf<T> | undefined;
88
98
  loading: false;
89
99
  error: undefined;
100
+ metadata: OperationResult['metadata'];
90
101
  } | {
91
102
  data: DataOf<T> | undefined;
92
103
  loading: false;
93
104
  error: AggregatedError;
105
+ metadata: OperationResult['metadata'];
94
106
  };
95
107
  type UseMutationOptions = MutationOptions;
96
108
  type Mutation<T extends Artifact<'mutation'>> = [(...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseMutationOptions?] : [VariablesOf<T>, UseMutationOptions?]) => Promise<DataOf<T>>, MutationResult<T>];
@@ -100,12 +112,15 @@ declare const useMutation: <T extends Artifact<"mutation">>(mutation: T) => Muta
100
112
  type UseFragmentOptions = FragmentOptions;
101
113
  type Fragment<T extends Artifact<'fragment'>> = {
102
114
  data: DataOf<T>;
115
+ metadata: OperationResult['metadata'];
103
116
  };
104
117
  type FragmentList<T extends Artifact<'fragment'>> = {
105
118
  data: DataOf<T>[];
119
+ metadata: OperationResult['metadata'];
106
120
  };
107
121
  type OptionalFragment<T extends Artifact<'fragment'>> = {
108
122
  data: DataOf<T> | null;
123
+ metadata: OperationResult['metadata'];
109
124
  };
110
125
  type UseFragmentFn = {
111
126
  <T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>[], options?: UseFragmentOptions): FragmentList<T>;
package/dist/index.mjs CHANGED
@@ -26,6 +26,7 @@ const useQuery = ((query, variables, options) => {
26
26
  const [data, setData] = useState(options?.initialData);
27
27
  const [loading, setLoading] = useState(!options?.skip && !options?.initialData);
28
28
  const [error, setError] = useState();
29
+ const [metadata, setMetadata] = useState();
29
30
  const unsubscribe = useRef(null);
30
31
  const initialized = useRef(false);
31
32
  const stableVariables = useMemo(() => stringify(variables), [variables]);
@@ -37,6 +38,7 @@ const useQuery = ((query, variables, options) => {
37
38
  initialized.current = true;
38
39
  setError(void 0);
39
40
  unsubscribe.current = pipe(client.executeQuery(query, variables, stableOptions), subscribe({ next: (result) => {
41
+ setMetadata(result.metadata);
40
42
  if (result.errors && result.errors.length > 0) {
41
43
  setError(new AggregatedError(result.errors));
42
44
  setLoading(false);
@@ -60,6 +62,7 @@ const useQuery = ((query, variables, options) => {
60
62
  data,
61
63
  loading,
62
64
  error,
65
+ metadata,
63
66
  refetch: execute
64
67
  };
65
68
  });
@@ -71,6 +74,7 @@ const useSubscription = (subscription, ...[variables, options]) => {
71
74
  const [data, setData] = useState();
72
75
  const [loading, setLoading] = useState(!options?.skip);
73
76
  const [error, setError] = useState();
77
+ const [metadata, setMetadata] = useState();
74
78
  const unsubscribe = useRef(null);
75
79
  const stableVariables = useMemo(() => stringify(variables), [variables]);
76
80
  const stableOptions = useMemo(() => options, [
@@ -84,6 +88,7 @@ const useSubscription = (subscription, ...[variables, options]) => {
84
88
  setLoading(true);
85
89
  setError(void 0);
86
90
  unsubscribe.current = pipe(client.executeSubscription(subscription, variables, stableOptions), subscribe({ next: (result) => {
91
+ setMetadata(result.metadata);
87
92
  if (result.errors && result.errors.length > 0) {
88
93
  const err = new AggregatedError(result.errors);
89
94
  setError(err);
@@ -110,7 +115,8 @@ const useSubscription = (subscription, ...[variables, options]) => {
110
115
  return {
111
116
  data,
112
117
  loading,
113
- error
118
+ error,
119
+ metadata
114
120
  };
115
121
  };
116
122
 
@@ -121,17 +127,21 @@ const useMutation = (mutation) => {
121
127
  const [data, setData] = useState();
122
128
  const [loading, setLoading] = useState(false);
123
129
  const [error, setError] = useState();
130
+ const [metadata, setMetadata] = useState();
124
131
  return [useCallback(async (variables, options) => {
132
+ setMetadata(void 0);
125
133
  setLoading(true);
126
134
  setError(void 0);
127
135
  try {
128
136
  const result = await pipe(client.executeMutation(mutation, variables, options), take(1), collect);
129
137
  if (result.errors && result.errors.length > 0) {
130
138
  const err = new AggregatedError(result.errors);
139
+ setMetadata(result.metadata);
131
140
  setError(err);
132
141
  setLoading(false);
133
142
  throw err;
134
143
  }
144
+ setMetadata(result.metadata);
135
145
  setData(result.data);
136
146
  setLoading(false);
137
147
  return result.data;
@@ -143,23 +153,31 @@ const useMutation = (mutation) => {
143
153
  }, [client, mutation]), {
144
154
  data,
145
155
  loading,
146
- error
156
+ error,
157
+ metadata
147
158
  }];
148
159
  };
149
160
 
150
161
  //#endregion
151
162
  //#region src/use-fragment.ts
163
+ const NULL_STORE = {
164
+ data: null,
165
+ metadata: void 0
166
+ };
152
167
  const useFragment = ((fragment, fragmentRef, options) => {
153
168
  const client = useClient();
154
- const dataRef = useRef(void 0);
169
+ const storeRef = useRef(void 0);
155
170
  const subscribe_ = useCallback((onChange) => {
156
171
  if (fragmentRef == null) {
157
- dataRef.current = null;
172
+ storeRef.current = NULL_STORE;
158
173
  return () => {};
159
174
  }
160
175
  return pipe(client.executeFragment(fragment, fragmentRef, options), subscribe({ next: (result) => {
161
176
  if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
162
- dataRef.current = result.data;
177
+ storeRef.current = {
178
+ data: result.data,
179
+ metadata: result.metadata
180
+ };
163
181
  onChange();
164
182
  } }));
165
183
  }, [
@@ -169,23 +187,31 @@ const useFragment = ((fragment, fragmentRef, options) => {
169
187
  options
170
188
  ]);
171
189
  const snapshot = useCallback(() => {
172
- if (fragmentRef == null) return null;
173
- if (dataRef.current === void 0) {
190
+ if (fragmentRef == null) return NULL_STORE;
191
+ if (storeRef.current === void 0) {
174
192
  const result = pipe(client.executeFragment(fragment, fragmentRef, options), peek);
175
193
  if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
176
- dataRef.current = result.data;
194
+ storeRef.current = {
195
+ data: result.data,
196
+ metadata: result.metadata
197
+ };
177
198
  }
178
- return dataRef.current;
199
+ return storeRef.current;
179
200
  }, [
180
201
  client,
181
202
  fragment,
182
203
  fragmentRef,
183
204
  options
184
205
  ]);
185
- const data = useSyncExternalStore(subscribe_, snapshot, snapshot);
186
- return { get data() {
187
- return data;
188
- } };
206
+ const store = useSyncExternalStore(subscribe_, snapshot, snapshot);
207
+ return {
208
+ get data() {
209
+ return store.data;
210
+ },
211
+ get metadata() {
212
+ return store.metadata;
213
+ }
214
+ };
189
215
  });
190
216
 
191
217
  //#endregion
package/package.json CHANGED
@@ -1,15 +1,22 @@
1
1
  {
2
2
  "name": "@mearie/react",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "Type-safe, zero-overhead GraphQL client",
5
5
  "keywords": [
6
6
  "graphql",
7
7
  "graphql-client",
8
8
  "typescript",
9
+ "type-safe",
9
10
  "codegen",
10
- "cache"
11
+ "cache",
12
+ "normalized-cache",
13
+ "react",
14
+ "vue",
15
+ "svelte",
16
+ "solid",
17
+ "vite"
11
18
  ],
12
- "homepage": "https://github.com/devunt/mearie#readme",
19
+ "homepage": "https://mearie.dev/",
13
20
  "bugs": {
14
21
  "url": "https://github.com/devunt/mearie/issues"
15
22
  },
@@ -45,7 +52,7 @@
45
52
  "README.md"
46
53
  ],
47
54
  "dependencies": {
48
- "@mearie/core": "0.2.3"
55
+ "@mearie/core": "0.3.0"
49
56
  },
50
57
  "devDependencies": {
51
58
  "@types/react": "^19.2.14",