@mearie/svelte 0.2.1 → 0.2.3
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/svelte/client-context.svelte.js +12 -0
- package/dist/svelte/create-fragment.svelte.js +40 -0
- package/dist/svelte/create-mutation.svelte.js +48 -0
- package/dist/svelte/create-query.svelte.js +59 -0
- package/dist/svelte/create-subscription.svelte.js +48 -0
- package/dist/svelte/index.svelte.js +6 -0
- package/package.json +3 -2
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { getContext, setContext } from 'svelte';
|
|
2
|
+
const CLIENT_KEY = Symbol('mearie-client');
|
|
3
|
+
export const setClient = (client) => {
|
|
4
|
+
setContext(CLIENT_KEY, client);
|
|
5
|
+
};
|
|
6
|
+
export const getClient = () => {
|
|
7
|
+
const client = getContext(CLIENT_KEY);
|
|
8
|
+
if (!client) {
|
|
9
|
+
throw new Error('getClient must be used within a context that has called setClient');
|
|
10
|
+
}
|
|
11
|
+
return client;
|
|
12
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { pipe, subscribe, peek } from '@mearie/core/stream';
|
|
2
|
+
import { getClient } from "./client-context.svelte.js";
|
|
3
|
+
export const createFragment = ((fragment, fragmentRef, options) => {
|
|
4
|
+
const client = getClient();
|
|
5
|
+
const ref = fragmentRef();
|
|
6
|
+
let data;
|
|
7
|
+
if (ref == null) {
|
|
8
|
+
data = null;
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
const result = pipe(client.executeFragment(fragment, $state.snapshot(ref), options?.()), peek);
|
|
12
|
+
if (result.data === undefined) {
|
|
13
|
+
throw new Error('Fragment data not found');
|
|
14
|
+
}
|
|
15
|
+
data = result.data;
|
|
16
|
+
}
|
|
17
|
+
let state = $state(data);
|
|
18
|
+
$effect(() => {
|
|
19
|
+
const currentRef = fragmentRef();
|
|
20
|
+
if (currentRef == null) {
|
|
21
|
+
state = null;
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const unsubscribe = pipe(client.executeFragment(fragment, $state.snapshot(currentRef), options?.()), subscribe({
|
|
25
|
+
next: (result) => {
|
|
26
|
+
if (result.data !== undefined) {
|
|
27
|
+
state = result.data;
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
return () => {
|
|
32
|
+
unsubscribe();
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
return {
|
|
36
|
+
get data() {
|
|
37
|
+
return state;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AggregatedError } from '@mearie/core';
|
|
2
|
+
import { pipe, take, collect } from '@mearie/core/stream';
|
|
3
|
+
import { getClient } from "./client-context.svelte.js";
|
|
4
|
+
export const createMutation = (mutation) => {
|
|
5
|
+
const client = getClient();
|
|
6
|
+
let data = $state();
|
|
7
|
+
let loading = $state(false);
|
|
8
|
+
let error = $state();
|
|
9
|
+
const execute = async (variables, options) => {
|
|
10
|
+
loading = true;
|
|
11
|
+
error = undefined;
|
|
12
|
+
try {
|
|
13
|
+
const result = await pipe(
|
|
14
|
+
// @ts-expect-error - conditional signature makes this hard to type correctly
|
|
15
|
+
client.executeMutation(mutation, variables, options), take(1), collect);
|
|
16
|
+
if (result.errors && result.errors.length > 0) {
|
|
17
|
+
const err = new AggregatedError(result.errors);
|
|
18
|
+
error = err;
|
|
19
|
+
loading = false;
|
|
20
|
+
throw err;
|
|
21
|
+
}
|
|
22
|
+
data = result.data;
|
|
23
|
+
loading = false;
|
|
24
|
+
return result.data;
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
if (err instanceof AggregatedError) {
|
|
28
|
+
error = err;
|
|
29
|
+
}
|
|
30
|
+
loading = false;
|
|
31
|
+
throw err;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
return [
|
|
35
|
+
execute,
|
|
36
|
+
{
|
|
37
|
+
get data() {
|
|
38
|
+
return data;
|
|
39
|
+
},
|
|
40
|
+
get loading() {
|
|
41
|
+
return loading;
|
|
42
|
+
},
|
|
43
|
+
get error() {
|
|
44
|
+
return error;
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
];
|
|
48
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { untrack } from 'svelte';
|
|
2
|
+
import { AggregatedError } from '@mearie/core';
|
|
3
|
+
import { pipe, subscribe } from '@mearie/core/stream';
|
|
4
|
+
import { getClient } from "./client-context.svelte.js";
|
|
5
|
+
export const createQuery = ((query, variables, options) => {
|
|
6
|
+
const client = getClient();
|
|
7
|
+
const initialOpts = options?.();
|
|
8
|
+
let data = $state.raw(initialOpts?.initialData);
|
|
9
|
+
let loading = $state(!initialOpts?.skip && !initialOpts?.initialData);
|
|
10
|
+
let error = $state();
|
|
11
|
+
let unsubscribe = null;
|
|
12
|
+
let initialized = false;
|
|
13
|
+
const execute = () => {
|
|
14
|
+
unsubscribe?.();
|
|
15
|
+
if (options?.().skip) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
if (!initialized && !!initialOpts?.initialData) {
|
|
19
|
+
loading = true;
|
|
20
|
+
}
|
|
21
|
+
initialized = true;
|
|
22
|
+
error = undefined;
|
|
23
|
+
unsubscribe = pipe(
|
|
24
|
+
// @ts-expect-error - conditional signature makes this hard to type correctly
|
|
25
|
+
client.executeQuery(query, typeof variables === 'function' ? variables() : undefined, options?.()), subscribe({
|
|
26
|
+
next: (result) => {
|
|
27
|
+
if (result.errors && result.errors.length > 0) {
|
|
28
|
+
error = new AggregatedError(result.errors);
|
|
29
|
+
loading = false;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
data = result.data;
|
|
33
|
+
loading = false;
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
}));
|
|
37
|
+
};
|
|
38
|
+
const refetch = () => {
|
|
39
|
+
untrack(execute);
|
|
40
|
+
};
|
|
41
|
+
$effect(() => {
|
|
42
|
+
execute();
|
|
43
|
+
return () => {
|
|
44
|
+
unsubscribe?.();
|
|
45
|
+
};
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
get data() {
|
|
49
|
+
return data;
|
|
50
|
+
},
|
|
51
|
+
get loading() {
|
|
52
|
+
return loading;
|
|
53
|
+
},
|
|
54
|
+
get error() {
|
|
55
|
+
return error;
|
|
56
|
+
},
|
|
57
|
+
refetch,
|
|
58
|
+
};
|
|
59
|
+
});
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { AggregatedError } from '@mearie/core';
|
|
2
|
+
import { pipe, subscribe } from '@mearie/core/stream';
|
|
3
|
+
import { getClient } from "./client-context.svelte.js";
|
|
4
|
+
export const createSubscription = (subscription, ...[variables, options]) => {
|
|
5
|
+
const client = getClient();
|
|
6
|
+
let data = $state.raw();
|
|
7
|
+
let loading = $state(!options?.().skip);
|
|
8
|
+
let error = $state();
|
|
9
|
+
$effect(() => {
|
|
10
|
+
if (options?.().skip) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
loading = true;
|
|
14
|
+
error = undefined;
|
|
15
|
+
const unsubscribe = pipe(
|
|
16
|
+
// @ts-expect-error - conditional signature makes this hard to type correctly
|
|
17
|
+
client.executeSubscription(subscription, typeof variables === 'function' ? variables() : undefined, options?.()), subscribe({
|
|
18
|
+
next: (result) => {
|
|
19
|
+
if (result.errors && result.errors.length > 0) {
|
|
20
|
+
const err = new AggregatedError(result.errors);
|
|
21
|
+
error = err;
|
|
22
|
+
loading = false;
|
|
23
|
+
options?.().onError?.(err);
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const resultData = result.data;
|
|
27
|
+
data = resultData;
|
|
28
|
+
loading = false;
|
|
29
|
+
options?.().onData?.(resultData);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
}));
|
|
33
|
+
return () => {
|
|
34
|
+
unsubscribe();
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
get data() {
|
|
39
|
+
return data;
|
|
40
|
+
},
|
|
41
|
+
get loading() {
|
|
42
|
+
return loading;
|
|
43
|
+
},
|
|
44
|
+
get error() {
|
|
45
|
+
return error;
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from '@mearie/core';
|
|
2
|
+
export { setClient, getClient } from "./client-context.svelte.js";
|
|
3
|
+
export { createQuery } from "./create-query.svelte.js";
|
|
4
|
+
export { createSubscription } from "./create-subscription.svelte.js";
|
|
5
|
+
export { createMutation, } from "./create-mutation.svelte.js";
|
|
6
|
+
export { createFragment, } from "./create-fragment.svelte.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mearie/svelte",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Type-safe, zero-overhead GraphQL client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graphql",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"type": "module",
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
|
31
|
+
"svelte": "./dist/svelte/index.svelte.js",
|
|
31
32
|
"import": {
|
|
32
33
|
"types": "./dist/index.svelte.d.mts",
|
|
33
34
|
"default": "./dist/index.svelte.mjs"
|
|
@@ -64,7 +65,7 @@
|
|
|
64
65
|
"access": "public"
|
|
65
66
|
},
|
|
66
67
|
"scripts": {
|
|
67
|
-
"build": "tsdown",
|
|
68
|
+
"build": "tsdown && tsc -p tsconfig.svelte.json",
|
|
68
69
|
"typecheck": "tsc"
|
|
69
70
|
},
|
|
70
71
|
"main": "./dist/index.svelte.cjs",
|