@mearie/react 0.0.0-next-20260228035926
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 +20 -0
- package/README.md +68 -0
- package/dist/index.cjs +202 -0
- package/dist/index.d.cts +117 -0
- package/dist/index.d.mts +117 -0
- package/dist/index.mjs +192 -0
- package/package.json +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright 2025 Bae Junehyeon
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
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, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# @mearie/react
|
|
2
|
+
|
|
3
|
+
React bindings for Mearie GraphQL client.
|
|
4
|
+
|
|
5
|
+
This package provides React hooks, components, and the GraphQL client runtime
|
|
6
|
+
for using Mearie in React applications.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install -D mearie
|
|
12
|
+
npm install @mearie/react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The `mearie` package provides build-time code generation, while `@mearie/react`
|
|
16
|
+
includes the runtime client and React-specific hooks.
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
First, create a client and wrap your app with the provider:
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
// src/App.tsx
|
|
24
|
+
import { createClient, httpExchange, cacheExchange, dedupExchange, ClientProvider } from '@mearie/react';
|
|
25
|
+
import { schema } from '$mearie';
|
|
26
|
+
|
|
27
|
+
const client = createClient({
|
|
28
|
+
schema,
|
|
29
|
+
exchanges: [dedupExchange(), cacheExchange(), httpExchange({ url: 'https://api.example.com/graphql' })],
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
function App() {
|
|
33
|
+
return <ClientProvider client={client}>{/* Your app components */}</ClientProvider>;
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Then use it in your components:
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
// src/components/UserProfile.tsx
|
|
41
|
+
import { graphql } from '$mearie';
|
|
42
|
+
import { useQuery } from '@mearie/react';
|
|
43
|
+
|
|
44
|
+
interface UserProfileProps {
|
|
45
|
+
userId: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function UserProfile({ userId }: UserProfileProps) {
|
|
49
|
+
const { data, loading } = useQuery(
|
|
50
|
+
graphql(`
|
|
51
|
+
query GetUser($id: ID!) {
|
|
52
|
+
user(id: $id) {
|
|
53
|
+
id
|
|
54
|
+
name
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
`),
|
|
58
|
+
{ id: userId },
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (loading) return <div>Loading...</div>;
|
|
62
|
+
return <h1>{data.user.name}</h1>;
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Documentation
|
|
67
|
+
|
|
68
|
+
Full documentation is available at <https://mearie.dev/frameworks/react>.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
+
let _mearie_core = require("@mearie/core");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
5
|
+
let _mearie_core_stream = require("@mearie/core/stream");
|
|
6
|
+
|
|
7
|
+
//#region src/client-provider.tsx
|
|
8
|
+
const ClientContext = (0, react.createContext)(null);
|
|
9
|
+
const ClientProvider = ({ client, children }) => {
|
|
10
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ClientContext.Provider, {
|
|
11
|
+
value: client,
|
|
12
|
+
children
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
const useClient = () => {
|
|
16
|
+
const client = (0, react.useContext)(ClientContext);
|
|
17
|
+
if (!client) throw new Error("useClient must be used within ClientProvider");
|
|
18
|
+
return client;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/use-query.ts
|
|
23
|
+
const useQuery = ((query, variables, options) => {
|
|
24
|
+
const client = useClient();
|
|
25
|
+
const [data, setData] = (0, react.useState)(options?.initialData);
|
|
26
|
+
const [loading, setLoading] = (0, react.useState)(!options?.skip && !options?.initialData);
|
|
27
|
+
const [error, setError] = (0, react.useState)();
|
|
28
|
+
const unsubscribe = (0, react.useRef)(null);
|
|
29
|
+
const initialized = (0, react.useRef)(false);
|
|
30
|
+
const stableVariables = (0, react.useMemo)(() => (0, _mearie_core.stringify)(variables), [variables]);
|
|
31
|
+
const stableOptions = (0, react.useMemo)(() => options, [options?.skip]);
|
|
32
|
+
const execute = (0, react.useCallback)(() => {
|
|
33
|
+
unsubscribe.current?.();
|
|
34
|
+
if (stableOptions?.skip) return;
|
|
35
|
+
if (!initialized.current && options?.initialData) setLoading(true);
|
|
36
|
+
initialized.current = true;
|
|
37
|
+
setError(void 0);
|
|
38
|
+
unsubscribe.current = (0, _mearie_core_stream.pipe)(client.executeQuery(query, variables, stableOptions), (0, _mearie_core_stream.subscribe)({ next: (result) => {
|
|
39
|
+
if (result.errors && result.errors.length > 0) {
|
|
40
|
+
setError(new _mearie_core.AggregatedError(result.errors));
|
|
41
|
+
setLoading(false);
|
|
42
|
+
} else {
|
|
43
|
+
setData(result.data);
|
|
44
|
+
setLoading(false);
|
|
45
|
+
setError(void 0);
|
|
46
|
+
}
|
|
47
|
+
} }));
|
|
48
|
+
}, [
|
|
49
|
+
client,
|
|
50
|
+
query,
|
|
51
|
+
stableVariables,
|
|
52
|
+
stableOptions
|
|
53
|
+
]);
|
|
54
|
+
(0, react.useEffect)(() => {
|
|
55
|
+
execute();
|
|
56
|
+
return () => unsubscribe.current?.();
|
|
57
|
+
}, [execute]);
|
|
58
|
+
return {
|
|
59
|
+
data,
|
|
60
|
+
loading,
|
|
61
|
+
error,
|
|
62
|
+
refetch: execute
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/use-subscription.ts
|
|
68
|
+
const useSubscription = (subscription, ...[variables, options]) => {
|
|
69
|
+
const client = useClient();
|
|
70
|
+
const [data, setData] = (0, react.useState)();
|
|
71
|
+
const [loading, setLoading] = (0, react.useState)(!options?.skip);
|
|
72
|
+
const [error, setError] = (0, react.useState)();
|
|
73
|
+
const unsubscribe = (0, react.useRef)(null);
|
|
74
|
+
const stableVariables = (0, react.useMemo)(() => (0, _mearie_core.stringify)(variables), [variables]);
|
|
75
|
+
const stableOptions = (0, react.useMemo)(() => options, [
|
|
76
|
+
options?.skip,
|
|
77
|
+
options?.onData,
|
|
78
|
+
options?.onError
|
|
79
|
+
]);
|
|
80
|
+
const execute = (0, react.useCallback)(() => {
|
|
81
|
+
unsubscribe.current?.();
|
|
82
|
+
if (stableOptions?.skip) return;
|
|
83
|
+
setLoading(true);
|
|
84
|
+
setError(void 0);
|
|
85
|
+
unsubscribe.current = (0, _mearie_core_stream.pipe)(client.executeSubscription(subscription, variables, stableOptions), (0, _mearie_core_stream.subscribe)({ next: (result) => {
|
|
86
|
+
if (result.errors && result.errors.length > 0) {
|
|
87
|
+
const err = new _mearie_core.AggregatedError(result.errors);
|
|
88
|
+
setError(err);
|
|
89
|
+
setLoading(false);
|
|
90
|
+
stableOptions?.onError?.(err);
|
|
91
|
+
} else {
|
|
92
|
+
const resultData = result.data;
|
|
93
|
+
setData(resultData);
|
|
94
|
+
setLoading(false);
|
|
95
|
+
setError(void 0);
|
|
96
|
+
stableOptions?.onData?.(resultData);
|
|
97
|
+
}
|
|
98
|
+
} }));
|
|
99
|
+
}, [
|
|
100
|
+
client,
|
|
101
|
+
subscription,
|
|
102
|
+
stableVariables,
|
|
103
|
+
stableOptions
|
|
104
|
+
]);
|
|
105
|
+
(0, react.useEffect)(() => {
|
|
106
|
+
execute();
|
|
107
|
+
return () => unsubscribe.current?.();
|
|
108
|
+
}, [execute]);
|
|
109
|
+
return {
|
|
110
|
+
data,
|
|
111
|
+
loading,
|
|
112
|
+
error
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/use-mutation.ts
|
|
118
|
+
const useMutation = (mutation) => {
|
|
119
|
+
const client = useClient();
|
|
120
|
+
const [data, setData] = (0, react.useState)();
|
|
121
|
+
const [loading, setLoading] = (0, react.useState)(false);
|
|
122
|
+
const [error, setError] = (0, react.useState)();
|
|
123
|
+
return [(0, react.useCallback)(async (variables, options) => {
|
|
124
|
+
setLoading(true);
|
|
125
|
+
setError(void 0);
|
|
126
|
+
try {
|
|
127
|
+
const result = await (0, _mearie_core_stream.pipe)(client.executeMutation(mutation, variables, options), (0, _mearie_core_stream.take)(1), _mearie_core_stream.collect);
|
|
128
|
+
if (result.errors && result.errors.length > 0) {
|
|
129
|
+
const err = new _mearie_core.AggregatedError(result.errors);
|
|
130
|
+
setError(err);
|
|
131
|
+
setLoading(false);
|
|
132
|
+
throw err;
|
|
133
|
+
}
|
|
134
|
+
setData(result.data);
|
|
135
|
+
setLoading(false);
|
|
136
|
+
return result.data;
|
|
137
|
+
} catch (err) {
|
|
138
|
+
if (err instanceof _mearie_core.AggregatedError) setError(err);
|
|
139
|
+
setLoading(false);
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
142
|
+
}, [client, mutation]), {
|
|
143
|
+
data,
|
|
144
|
+
loading,
|
|
145
|
+
error
|
|
146
|
+
}];
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
//#endregion
|
|
150
|
+
//#region src/use-fragment.ts
|
|
151
|
+
const useFragment = ((fragment, fragmentRef, options) => {
|
|
152
|
+
const client = useClient();
|
|
153
|
+
const dataRef = (0, react.useRef)(void 0);
|
|
154
|
+
const subscribe_ = (0, react.useCallback)((onChange) => {
|
|
155
|
+
if (fragmentRef == null) {
|
|
156
|
+
dataRef.current = null;
|
|
157
|
+
return () => {};
|
|
158
|
+
}
|
|
159
|
+
return (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), (0, _mearie_core_stream.subscribe)({ next: (result) => {
|
|
160
|
+
if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
|
|
161
|
+
dataRef.current = result.data;
|
|
162
|
+
onChange();
|
|
163
|
+
} }));
|
|
164
|
+
}, [
|
|
165
|
+
client,
|
|
166
|
+
fragment,
|
|
167
|
+
fragmentRef,
|
|
168
|
+
options
|
|
169
|
+
]);
|
|
170
|
+
const snapshot = (0, react.useCallback)(() => {
|
|
171
|
+
if (fragmentRef == null) return null;
|
|
172
|
+
if (dataRef.current === void 0) {
|
|
173
|
+
const result = (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, fragmentRef, options), _mearie_core_stream.peek);
|
|
174
|
+
if (result.errors && result.errors.length > 0) throw new _mearie_core.AggregatedError(result.errors);
|
|
175
|
+
dataRef.current = result.data;
|
|
176
|
+
}
|
|
177
|
+
return dataRef.current;
|
|
178
|
+
}, [
|
|
179
|
+
client,
|
|
180
|
+
fragment,
|
|
181
|
+
fragmentRef,
|
|
182
|
+
options
|
|
183
|
+
]);
|
|
184
|
+
const data = (0, react.useSyncExternalStore)(subscribe_, snapshot, snapshot);
|
|
185
|
+
return { get data() {
|
|
186
|
+
return data;
|
|
187
|
+
} };
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
//#endregion
|
|
191
|
+
exports.ClientProvider = ClientProvider;
|
|
192
|
+
exports.useClient = useClient;
|
|
193
|
+
exports.useFragment = useFragment;
|
|
194
|
+
exports.useMutation = useMutation;
|
|
195
|
+
exports.useQuery = useQuery;
|
|
196
|
+
exports.useSubscription = useSubscription;
|
|
197
|
+
Object.keys(_mearie_core).forEach(function (k) {
|
|
198
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
199
|
+
enumerable: true,
|
|
200
|
+
get: function () { return _mearie_core[k]; }
|
|
201
|
+
});
|
|
202
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { AggregatedError, Artifact, Client, DataOf, FragmentOptions, FragmentRefs, MutationOptions, QueryOptions, SchemaMeta, SubscriptionOptions, VariablesOf } from "@mearie/core";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
export * from "@mearie/core";
|
|
4
|
+
|
|
5
|
+
//#region src/client-provider.d.ts
|
|
6
|
+
type ClientProviderProps<TMeta extends SchemaMeta = SchemaMeta> = {
|
|
7
|
+
client: Client<TMeta>;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
};
|
|
10
|
+
declare const ClientProvider: <TMeta extends SchemaMeta = SchemaMeta>({
|
|
11
|
+
client,
|
|
12
|
+
children
|
|
13
|
+
}: ClientProviderProps<TMeta>) => ReactNode;
|
|
14
|
+
declare const useClient: <TMeta extends SchemaMeta = SchemaMeta>() => Client<TMeta>;
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/use-query.d.ts
|
|
17
|
+
type UseQueryOptions<T extends Artifact<'query'> = Artifact<'query'>> = QueryOptions<T> & {
|
|
18
|
+
skip?: boolean;
|
|
19
|
+
};
|
|
20
|
+
type Query<T extends Artifact<'query'>> = {
|
|
21
|
+
data: undefined;
|
|
22
|
+
loading: true;
|
|
23
|
+
error: undefined;
|
|
24
|
+
refetch: () => void;
|
|
25
|
+
} | {
|
|
26
|
+
data: DataOf<T>;
|
|
27
|
+
loading: false;
|
|
28
|
+
error: undefined;
|
|
29
|
+
refetch: () => void;
|
|
30
|
+
} | {
|
|
31
|
+
data: DataOf<T> | undefined;
|
|
32
|
+
loading: false;
|
|
33
|
+
error: AggregatedError;
|
|
34
|
+
refetch: () => void;
|
|
35
|
+
};
|
|
36
|
+
type DefinedQuery<T extends Artifact<'query'>> = {
|
|
37
|
+
data: DataOf<T>;
|
|
38
|
+
loading: true;
|
|
39
|
+
error: undefined;
|
|
40
|
+
refetch: () => void;
|
|
41
|
+
} | {
|
|
42
|
+
data: DataOf<T>;
|
|
43
|
+
loading: false;
|
|
44
|
+
error: undefined;
|
|
45
|
+
refetch: () => void;
|
|
46
|
+
} | {
|
|
47
|
+
data: DataOf<T>;
|
|
48
|
+
loading: false;
|
|
49
|
+
error: AggregatedError;
|
|
50
|
+
refetch: () => void;
|
|
51
|
+
};
|
|
52
|
+
type UseQueryFn = {
|
|
53
|
+
<T extends Artifact<'query'>>(query: T, variables: VariablesOf<T> | undefined, options: UseQueryOptions<T> & {
|
|
54
|
+
initialData: DataOf<T>;
|
|
55
|
+
}): DefinedQuery<T>;
|
|
56
|
+
<T extends Artifact<'query'>>(query: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseQueryOptions<T>?] : [VariablesOf<T>, UseQueryOptions<T>?]): Query<T>;
|
|
57
|
+
};
|
|
58
|
+
declare const useQuery: UseQueryFn;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/use-subscription.d.ts
|
|
61
|
+
type Subscription<T extends Artifact<'subscription'>> = {
|
|
62
|
+
data: undefined;
|
|
63
|
+
loading: true;
|
|
64
|
+
error: undefined;
|
|
65
|
+
} | {
|
|
66
|
+
data: DataOf<T> | undefined;
|
|
67
|
+
loading: false;
|
|
68
|
+
error: undefined;
|
|
69
|
+
} | {
|
|
70
|
+
data: DataOf<T> | undefined;
|
|
71
|
+
loading: false;
|
|
72
|
+
error: AggregatedError;
|
|
73
|
+
};
|
|
74
|
+
type UseSubscriptionOptions<T extends Artifact<'subscription'>> = SubscriptionOptions & {
|
|
75
|
+
skip?: boolean;
|
|
76
|
+
onData?: (data: DataOf<T>) => void;
|
|
77
|
+
onError?: (error: AggregatedError) => void;
|
|
78
|
+
};
|
|
79
|
+
declare const useSubscription: <T extends Artifact<"subscription">>(subscription: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseSubscriptionOptions<T>?] : [VariablesOf<T>, UseSubscriptionOptions<T>?]) => Subscription<T>;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/use-mutation.d.ts
|
|
82
|
+
type MutationResult<T extends Artifact<'mutation'>> = {
|
|
83
|
+
data: undefined;
|
|
84
|
+
loading: true;
|
|
85
|
+
error: undefined;
|
|
86
|
+
} | {
|
|
87
|
+
data: DataOf<T> | undefined;
|
|
88
|
+
loading: false;
|
|
89
|
+
error: undefined;
|
|
90
|
+
} | {
|
|
91
|
+
data: DataOf<T> | undefined;
|
|
92
|
+
loading: false;
|
|
93
|
+
error: AggregatedError;
|
|
94
|
+
};
|
|
95
|
+
type UseMutationOptions = MutationOptions;
|
|
96
|
+
type Mutation<T extends Artifact<'mutation'>> = [(...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseMutationOptions?] : [VariablesOf<T>, UseMutationOptions?]) => Promise<DataOf<T>>, MutationResult<T>];
|
|
97
|
+
declare const useMutation: <T extends Artifact<"mutation">>(mutation: T) => Mutation<T>;
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/use-fragment.d.ts
|
|
100
|
+
type UseFragmentOptions = FragmentOptions;
|
|
101
|
+
type Fragment<T extends Artifact<'fragment'>> = {
|
|
102
|
+
data: DataOf<T>;
|
|
103
|
+
};
|
|
104
|
+
type FragmentList<T extends Artifact<'fragment'>> = {
|
|
105
|
+
data: DataOf<T>[];
|
|
106
|
+
};
|
|
107
|
+
type OptionalFragment<T extends Artifact<'fragment'>> = {
|
|
108
|
+
data: DataOf<T> | null;
|
|
109
|
+
};
|
|
110
|
+
type UseFragmentFn = {
|
|
111
|
+
<T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>[], options?: UseFragmentOptions): FragmentList<T>;
|
|
112
|
+
<T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>, options?: UseFragmentOptions): Fragment<T>;
|
|
113
|
+
<T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']> | null | undefined, options?: UseFragmentOptions): OptionalFragment<T>;
|
|
114
|
+
};
|
|
115
|
+
declare const useFragment: UseFragmentFn;
|
|
116
|
+
//#endregion
|
|
117
|
+
export { ClientProvider, type DefinedQuery, type Fragment, type FragmentList, type Mutation, type OptionalFragment, type Query, type Subscription, type UseFragmentOptions, type UseMutationOptions, type UseQueryOptions, type UseSubscriptionOptions, useClient, useFragment, useMutation, useQuery, useSubscription };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { AggregatedError, Artifact, Client, DataOf, FragmentOptions, FragmentRefs, MutationOptions, QueryOptions, SchemaMeta, SubscriptionOptions, VariablesOf } from "@mearie/core";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
export * from "@mearie/core";
|
|
4
|
+
|
|
5
|
+
//#region src/client-provider.d.ts
|
|
6
|
+
type ClientProviderProps<TMeta extends SchemaMeta = SchemaMeta> = {
|
|
7
|
+
client: Client<TMeta>;
|
|
8
|
+
children: ReactNode;
|
|
9
|
+
};
|
|
10
|
+
declare const ClientProvider: <TMeta extends SchemaMeta = SchemaMeta>({
|
|
11
|
+
client,
|
|
12
|
+
children
|
|
13
|
+
}: ClientProviderProps<TMeta>) => ReactNode;
|
|
14
|
+
declare const useClient: <TMeta extends SchemaMeta = SchemaMeta>() => Client<TMeta>;
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/use-query.d.ts
|
|
17
|
+
type UseQueryOptions<T extends Artifact<'query'> = Artifact<'query'>> = QueryOptions<T> & {
|
|
18
|
+
skip?: boolean;
|
|
19
|
+
};
|
|
20
|
+
type Query<T extends Artifact<'query'>> = {
|
|
21
|
+
data: undefined;
|
|
22
|
+
loading: true;
|
|
23
|
+
error: undefined;
|
|
24
|
+
refetch: () => void;
|
|
25
|
+
} | {
|
|
26
|
+
data: DataOf<T>;
|
|
27
|
+
loading: false;
|
|
28
|
+
error: undefined;
|
|
29
|
+
refetch: () => void;
|
|
30
|
+
} | {
|
|
31
|
+
data: DataOf<T> | undefined;
|
|
32
|
+
loading: false;
|
|
33
|
+
error: AggregatedError;
|
|
34
|
+
refetch: () => void;
|
|
35
|
+
};
|
|
36
|
+
type DefinedQuery<T extends Artifact<'query'>> = {
|
|
37
|
+
data: DataOf<T>;
|
|
38
|
+
loading: true;
|
|
39
|
+
error: undefined;
|
|
40
|
+
refetch: () => void;
|
|
41
|
+
} | {
|
|
42
|
+
data: DataOf<T>;
|
|
43
|
+
loading: false;
|
|
44
|
+
error: undefined;
|
|
45
|
+
refetch: () => void;
|
|
46
|
+
} | {
|
|
47
|
+
data: DataOf<T>;
|
|
48
|
+
loading: false;
|
|
49
|
+
error: AggregatedError;
|
|
50
|
+
refetch: () => void;
|
|
51
|
+
};
|
|
52
|
+
type UseQueryFn = {
|
|
53
|
+
<T extends Artifact<'query'>>(query: T, variables: VariablesOf<T> | undefined, options: UseQueryOptions<T> & {
|
|
54
|
+
initialData: DataOf<T>;
|
|
55
|
+
}): DefinedQuery<T>;
|
|
56
|
+
<T extends Artifact<'query'>>(query: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseQueryOptions<T>?] : [VariablesOf<T>, UseQueryOptions<T>?]): Query<T>;
|
|
57
|
+
};
|
|
58
|
+
declare const useQuery: UseQueryFn;
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/use-subscription.d.ts
|
|
61
|
+
type Subscription<T extends Artifact<'subscription'>> = {
|
|
62
|
+
data: undefined;
|
|
63
|
+
loading: true;
|
|
64
|
+
error: undefined;
|
|
65
|
+
} | {
|
|
66
|
+
data: DataOf<T> | undefined;
|
|
67
|
+
loading: false;
|
|
68
|
+
error: undefined;
|
|
69
|
+
} | {
|
|
70
|
+
data: DataOf<T> | undefined;
|
|
71
|
+
loading: false;
|
|
72
|
+
error: AggregatedError;
|
|
73
|
+
};
|
|
74
|
+
type UseSubscriptionOptions<T extends Artifact<'subscription'>> = SubscriptionOptions & {
|
|
75
|
+
skip?: boolean;
|
|
76
|
+
onData?: (data: DataOf<T>) => void;
|
|
77
|
+
onError?: (error: AggregatedError) => void;
|
|
78
|
+
};
|
|
79
|
+
declare const useSubscription: <T extends Artifact<"subscription">>(subscription: T, ...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseSubscriptionOptions<T>?] : [VariablesOf<T>, UseSubscriptionOptions<T>?]) => Subscription<T>;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/use-mutation.d.ts
|
|
82
|
+
type MutationResult<T extends Artifact<'mutation'>> = {
|
|
83
|
+
data: undefined;
|
|
84
|
+
loading: true;
|
|
85
|
+
error: undefined;
|
|
86
|
+
} | {
|
|
87
|
+
data: DataOf<T> | undefined;
|
|
88
|
+
loading: false;
|
|
89
|
+
error: undefined;
|
|
90
|
+
} | {
|
|
91
|
+
data: DataOf<T> | undefined;
|
|
92
|
+
loading: false;
|
|
93
|
+
error: AggregatedError;
|
|
94
|
+
};
|
|
95
|
+
type UseMutationOptions = MutationOptions;
|
|
96
|
+
type Mutation<T extends Artifact<'mutation'>> = [(...[variables, options]: VariablesOf<T> extends Record<string, never> ? [undefined?, UseMutationOptions?] : [VariablesOf<T>, UseMutationOptions?]) => Promise<DataOf<T>>, MutationResult<T>];
|
|
97
|
+
declare const useMutation: <T extends Artifact<"mutation">>(mutation: T) => Mutation<T>;
|
|
98
|
+
//#endregion
|
|
99
|
+
//#region src/use-fragment.d.ts
|
|
100
|
+
type UseFragmentOptions = FragmentOptions;
|
|
101
|
+
type Fragment<T extends Artifact<'fragment'>> = {
|
|
102
|
+
data: DataOf<T>;
|
|
103
|
+
};
|
|
104
|
+
type FragmentList<T extends Artifact<'fragment'>> = {
|
|
105
|
+
data: DataOf<T>[];
|
|
106
|
+
};
|
|
107
|
+
type OptionalFragment<T extends Artifact<'fragment'>> = {
|
|
108
|
+
data: DataOf<T> | null;
|
|
109
|
+
};
|
|
110
|
+
type UseFragmentFn = {
|
|
111
|
+
<T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>[], options?: UseFragmentOptions): FragmentList<T>;
|
|
112
|
+
<T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']>, options?: UseFragmentOptions): Fragment<T>;
|
|
113
|
+
<T extends Artifact<'fragment'>>(fragment: T, fragmentRef: FragmentRefs<T['name']> | null | undefined, options?: UseFragmentOptions): OptionalFragment<T>;
|
|
114
|
+
};
|
|
115
|
+
declare const useFragment: UseFragmentFn;
|
|
116
|
+
//#endregion
|
|
117
|
+
export { ClientProvider, type DefinedQuery, type Fragment, type FragmentList, type Mutation, type OptionalFragment, type Query, type Subscription, type UseFragmentOptions, type UseMutationOptions, type UseQueryOptions, type UseSubscriptionOptions, useClient, useFragment, useMutation, useQuery, useSubscription };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { AggregatedError, stringify } from "@mearie/core";
|
|
2
|
+
import { createContext, useCallback, useContext, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
import { collect, peek, pipe, subscribe, take } from "@mearie/core/stream";
|
|
5
|
+
|
|
6
|
+
export * from "@mearie/core"
|
|
7
|
+
|
|
8
|
+
//#region src/client-provider.tsx
|
|
9
|
+
const ClientContext = createContext(null);
|
|
10
|
+
const ClientProvider = ({ client, children }) => {
|
|
11
|
+
return /* @__PURE__ */ jsx(ClientContext.Provider, {
|
|
12
|
+
value: client,
|
|
13
|
+
children
|
|
14
|
+
});
|
|
15
|
+
};
|
|
16
|
+
const useClient = () => {
|
|
17
|
+
const client = useContext(ClientContext);
|
|
18
|
+
if (!client) throw new Error("useClient must be used within ClientProvider");
|
|
19
|
+
return client;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/use-query.ts
|
|
24
|
+
const useQuery = ((query, variables, options) => {
|
|
25
|
+
const client = useClient();
|
|
26
|
+
const [data, setData] = useState(options?.initialData);
|
|
27
|
+
const [loading, setLoading] = useState(!options?.skip && !options?.initialData);
|
|
28
|
+
const [error, setError] = useState();
|
|
29
|
+
const unsubscribe = useRef(null);
|
|
30
|
+
const initialized = useRef(false);
|
|
31
|
+
const stableVariables = useMemo(() => stringify(variables), [variables]);
|
|
32
|
+
const stableOptions = useMemo(() => options, [options?.skip]);
|
|
33
|
+
const execute = useCallback(() => {
|
|
34
|
+
unsubscribe.current?.();
|
|
35
|
+
if (stableOptions?.skip) return;
|
|
36
|
+
if (!initialized.current && options?.initialData) setLoading(true);
|
|
37
|
+
initialized.current = true;
|
|
38
|
+
setError(void 0);
|
|
39
|
+
unsubscribe.current = pipe(client.executeQuery(query, variables, stableOptions), subscribe({ next: (result) => {
|
|
40
|
+
if (result.errors && result.errors.length > 0) {
|
|
41
|
+
setError(new AggregatedError(result.errors));
|
|
42
|
+
setLoading(false);
|
|
43
|
+
} else {
|
|
44
|
+
setData(result.data);
|
|
45
|
+
setLoading(false);
|
|
46
|
+
setError(void 0);
|
|
47
|
+
}
|
|
48
|
+
} }));
|
|
49
|
+
}, [
|
|
50
|
+
client,
|
|
51
|
+
query,
|
|
52
|
+
stableVariables,
|
|
53
|
+
stableOptions
|
|
54
|
+
]);
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
execute();
|
|
57
|
+
return () => unsubscribe.current?.();
|
|
58
|
+
}, [execute]);
|
|
59
|
+
return {
|
|
60
|
+
data,
|
|
61
|
+
loading,
|
|
62
|
+
error,
|
|
63
|
+
refetch: execute
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
//#endregion
|
|
68
|
+
//#region src/use-subscription.ts
|
|
69
|
+
const useSubscription = (subscription, ...[variables, options]) => {
|
|
70
|
+
const client = useClient();
|
|
71
|
+
const [data, setData] = useState();
|
|
72
|
+
const [loading, setLoading] = useState(!options?.skip);
|
|
73
|
+
const [error, setError] = useState();
|
|
74
|
+
const unsubscribe = useRef(null);
|
|
75
|
+
const stableVariables = useMemo(() => stringify(variables), [variables]);
|
|
76
|
+
const stableOptions = useMemo(() => options, [
|
|
77
|
+
options?.skip,
|
|
78
|
+
options?.onData,
|
|
79
|
+
options?.onError
|
|
80
|
+
]);
|
|
81
|
+
const execute = useCallback(() => {
|
|
82
|
+
unsubscribe.current?.();
|
|
83
|
+
if (stableOptions?.skip) return;
|
|
84
|
+
setLoading(true);
|
|
85
|
+
setError(void 0);
|
|
86
|
+
unsubscribe.current = pipe(client.executeSubscription(subscription, variables, stableOptions), subscribe({ next: (result) => {
|
|
87
|
+
if (result.errors && result.errors.length > 0) {
|
|
88
|
+
const err = new AggregatedError(result.errors);
|
|
89
|
+
setError(err);
|
|
90
|
+
setLoading(false);
|
|
91
|
+
stableOptions?.onError?.(err);
|
|
92
|
+
} else {
|
|
93
|
+
const resultData = result.data;
|
|
94
|
+
setData(resultData);
|
|
95
|
+
setLoading(false);
|
|
96
|
+
setError(void 0);
|
|
97
|
+
stableOptions?.onData?.(resultData);
|
|
98
|
+
}
|
|
99
|
+
} }));
|
|
100
|
+
}, [
|
|
101
|
+
client,
|
|
102
|
+
subscription,
|
|
103
|
+
stableVariables,
|
|
104
|
+
stableOptions
|
|
105
|
+
]);
|
|
106
|
+
useEffect(() => {
|
|
107
|
+
execute();
|
|
108
|
+
return () => unsubscribe.current?.();
|
|
109
|
+
}, [execute]);
|
|
110
|
+
return {
|
|
111
|
+
data,
|
|
112
|
+
loading,
|
|
113
|
+
error
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/use-mutation.ts
|
|
119
|
+
const useMutation = (mutation) => {
|
|
120
|
+
const client = useClient();
|
|
121
|
+
const [data, setData] = useState();
|
|
122
|
+
const [loading, setLoading] = useState(false);
|
|
123
|
+
const [error, setError] = useState();
|
|
124
|
+
return [useCallback(async (variables, options) => {
|
|
125
|
+
setLoading(true);
|
|
126
|
+
setError(void 0);
|
|
127
|
+
try {
|
|
128
|
+
const result = await pipe(client.executeMutation(mutation, variables, options), take(1), collect);
|
|
129
|
+
if (result.errors && result.errors.length > 0) {
|
|
130
|
+
const err = new AggregatedError(result.errors);
|
|
131
|
+
setError(err);
|
|
132
|
+
setLoading(false);
|
|
133
|
+
throw err;
|
|
134
|
+
}
|
|
135
|
+
setData(result.data);
|
|
136
|
+
setLoading(false);
|
|
137
|
+
return result.data;
|
|
138
|
+
} catch (err) {
|
|
139
|
+
if (err instanceof AggregatedError) setError(err);
|
|
140
|
+
setLoading(false);
|
|
141
|
+
throw err;
|
|
142
|
+
}
|
|
143
|
+
}, [client, mutation]), {
|
|
144
|
+
data,
|
|
145
|
+
loading,
|
|
146
|
+
error
|
|
147
|
+
}];
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/use-fragment.ts
|
|
152
|
+
const useFragment = ((fragment, fragmentRef, options) => {
|
|
153
|
+
const client = useClient();
|
|
154
|
+
const dataRef = useRef(void 0);
|
|
155
|
+
const subscribe_ = useCallback((onChange) => {
|
|
156
|
+
if (fragmentRef == null) {
|
|
157
|
+
dataRef.current = null;
|
|
158
|
+
return () => {};
|
|
159
|
+
}
|
|
160
|
+
return pipe(client.executeFragment(fragment, fragmentRef, options), subscribe({ next: (result) => {
|
|
161
|
+
if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
|
|
162
|
+
dataRef.current = result.data;
|
|
163
|
+
onChange();
|
|
164
|
+
} }));
|
|
165
|
+
}, [
|
|
166
|
+
client,
|
|
167
|
+
fragment,
|
|
168
|
+
fragmentRef,
|
|
169
|
+
options
|
|
170
|
+
]);
|
|
171
|
+
const snapshot = useCallback(() => {
|
|
172
|
+
if (fragmentRef == null) return null;
|
|
173
|
+
if (dataRef.current === void 0) {
|
|
174
|
+
const result = pipe(client.executeFragment(fragment, fragmentRef, options), peek);
|
|
175
|
+
if (result.errors && result.errors.length > 0) throw new AggregatedError(result.errors);
|
|
176
|
+
dataRef.current = result.data;
|
|
177
|
+
}
|
|
178
|
+
return dataRef.current;
|
|
179
|
+
}, [
|
|
180
|
+
client,
|
|
181
|
+
fragment,
|
|
182
|
+
fragmentRef,
|
|
183
|
+
options
|
|
184
|
+
]);
|
|
185
|
+
const data = useSyncExternalStore(subscribe_, snapshot, snapshot);
|
|
186
|
+
return { get data() {
|
|
187
|
+
return data;
|
|
188
|
+
} };
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
//#endregion
|
|
192
|
+
export { ClientProvider, useClient, useFragment, useMutation, useQuery, useSubscription };
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mearie/react",
|
|
3
|
+
"version": "0.0.0-next-20260228035926",
|
|
4
|
+
"description": "Type-safe, zero-overhead GraphQL client",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"graphql",
|
|
7
|
+
"graphql-client",
|
|
8
|
+
"typescript",
|
|
9
|
+
"codegen",
|
|
10
|
+
"cache"
|
|
11
|
+
],
|
|
12
|
+
"homepage": "https://github.com/devunt/mearie#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/devunt/mearie/issues"
|
|
15
|
+
},
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/devunt/mearie.git",
|
|
19
|
+
"directory": "packages/react"
|
|
20
|
+
},
|
|
21
|
+
"funding": {
|
|
22
|
+
"type": "github",
|
|
23
|
+
"url": "https://github.com/sponsors/devunt"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"author": "Bae Junehyeon <finn@penxle.io>",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"type": "module",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": {
|
|
32
|
+
"types": "./dist/index.d.mts",
|
|
33
|
+
"default": "./dist/index.mjs"
|
|
34
|
+
},
|
|
35
|
+
"require": {
|
|
36
|
+
"types": "./dist/index.d.cts",
|
|
37
|
+
"default": "./dist/index.cjs"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"./package.json": "./package.json"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"dist",
|
|
44
|
+
"package.json",
|
|
45
|
+
"README.md"
|
|
46
|
+
],
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@mearie/core": "0.0.0-next-20260228035926"
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/react": "^19.2.14",
|
|
52
|
+
"react": "^19.2.4",
|
|
53
|
+
"tsdown": "^0.20.3",
|
|
54
|
+
"typescript": "^5.9.3"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"bun": ">=1.2.0",
|
|
61
|
+
"deno": ">=2.2.0",
|
|
62
|
+
"node": ">=20.0.0"
|
|
63
|
+
},
|
|
64
|
+
"publishConfig": {
|
|
65
|
+
"access": "public"
|
|
66
|
+
},
|
|
67
|
+
"scripts": {
|
|
68
|
+
"build": "tsdown",
|
|
69
|
+
"typecheck": "tsc"
|
|
70
|
+
},
|
|
71
|
+
"main": "./dist/index.cjs",
|
|
72
|
+
"module": "./dist/index.mjs",
|
|
73
|
+
"types": "./dist/index.d.mts"
|
|
74
|
+
}
|