@mearie/svelte 0.2.2 → 0.2.4
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 +4 -8
- package/src/client-context.svelte.ts +0 -18
- package/src/create-fragment.svelte.ts +0 -87
- package/src/create-mutation.svelte.ts +0 -91
- package/src/create-query.svelte.ts +0 -135
- package/src/create-subscription.svelte.ts +0 -89
- package/src/index.svelte.ts +0 -17
|
@@ -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.4",
|
|
4
4
|
"description": "Type-safe, zero-overhead GraphQL client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"graphql",
|
|
@@ -28,10 +28,7 @@
|
|
|
28
28
|
"type": "module",
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
|
31
|
-
"svelte":
|
|
32
|
-
"types": "./src/index.svelte.ts",
|
|
33
|
-
"default": "./src/index.svelte.ts"
|
|
34
|
-
},
|
|
31
|
+
"svelte": "./dist/svelte/index.svelte.js",
|
|
35
32
|
"import": {
|
|
36
33
|
"types": "./dist/index.svelte.d.mts",
|
|
37
34
|
"default": "./dist/index.svelte.mjs"
|
|
@@ -45,12 +42,11 @@
|
|
|
45
42
|
},
|
|
46
43
|
"files": [
|
|
47
44
|
"dist",
|
|
48
|
-
"src",
|
|
49
45
|
"package.json",
|
|
50
46
|
"README.md"
|
|
51
47
|
],
|
|
52
48
|
"dependencies": {
|
|
53
|
-
"@mearie/core": "0.2.
|
|
49
|
+
"@mearie/core": "0.2.2"
|
|
54
50
|
},
|
|
55
51
|
"devDependencies": {
|
|
56
52
|
"svelte": "^5.53.3",
|
|
@@ -69,7 +65,7 @@
|
|
|
69
65
|
"access": "public"
|
|
70
66
|
},
|
|
71
67
|
"scripts": {
|
|
72
|
-
"build": "tsdown",
|
|
68
|
+
"build": "tsdown && tsc -p tsconfig.svelte.json",
|
|
73
69
|
"typecheck": "tsc"
|
|
74
70
|
},
|
|
75
71
|
"main": "./dist/index.svelte.cjs",
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { getContext, setContext } from 'svelte';
|
|
2
|
-
import type { Client, SchemaMeta } from '@mearie/core';
|
|
3
|
-
|
|
4
|
-
const CLIENT_KEY = Symbol('mearie-client');
|
|
5
|
-
|
|
6
|
-
export const setClient = <TMeta extends SchemaMeta = SchemaMeta>(client: Client<TMeta>): void => {
|
|
7
|
-
setContext(CLIENT_KEY, client as Client<SchemaMeta>);
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const getClient = <TMeta extends SchemaMeta = SchemaMeta>(): Client<TMeta> => {
|
|
11
|
-
const client = getContext<Client<SchemaMeta>>(CLIENT_KEY);
|
|
12
|
-
|
|
13
|
-
if (!client) {
|
|
14
|
-
throw new Error('getClient must be used within a context that has called setClient');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return client as Client<TMeta>;
|
|
18
|
-
};
|
|
@@ -1,87 +0,0 @@
|
|
|
1
|
-
import type { Artifact, DataOf, FragmentRefs, OperationResult, FragmentOptions } from '@mearie/core';
|
|
2
|
-
import { pipe, subscribe, peek } from '@mearie/core/stream';
|
|
3
|
-
import { getClient } from './client-context.svelte.ts';
|
|
4
|
-
|
|
5
|
-
export type CreateFragmentOptions = FragmentOptions;
|
|
6
|
-
|
|
7
|
-
export type Fragment<T extends Artifact<'fragment'>> = {
|
|
8
|
-
data: DataOf<T>;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type FragmentList<T extends Artifact<'fragment'>> = {
|
|
12
|
-
data: DataOf<T>[];
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type OptionalFragment<T extends Artifact<'fragment'>> = {
|
|
16
|
-
data: DataOf<T> | null;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
type CreateFragmentFn = {
|
|
20
|
-
<T extends Artifact<'fragment'>>(
|
|
21
|
-
fragment: T,
|
|
22
|
-
fragmentRef: () => FragmentRefs<T['name']>[],
|
|
23
|
-
options?: () => CreateFragmentOptions,
|
|
24
|
-
): FragmentList<T>;
|
|
25
|
-
<T extends Artifact<'fragment'>>(
|
|
26
|
-
fragment: T,
|
|
27
|
-
fragmentRef: () => FragmentRefs<T['name']>,
|
|
28
|
-
options?: () => CreateFragmentOptions,
|
|
29
|
-
): Fragment<T>;
|
|
30
|
-
<T extends Artifact<'fragment'>>(
|
|
31
|
-
fragment: T,
|
|
32
|
-
fragmentRef: () => FragmentRefs<T['name']> | null | undefined,
|
|
33
|
-
options?: () => CreateFragmentOptions,
|
|
34
|
-
): OptionalFragment<T>;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export const createFragment: CreateFragmentFn = (<T extends Artifact<'fragment'>>(
|
|
38
|
-
fragment: T,
|
|
39
|
-
fragmentRef: () => FragmentRefs<T['name']> | FragmentRefs<T['name']>[] | null | undefined,
|
|
40
|
-
options?: () => CreateFragmentOptions,
|
|
41
|
-
) => {
|
|
42
|
-
const client = getClient();
|
|
43
|
-
|
|
44
|
-
const ref = fragmentRef();
|
|
45
|
-
|
|
46
|
-
let data: unknown;
|
|
47
|
-
if (ref == null) {
|
|
48
|
-
data = null;
|
|
49
|
-
} else {
|
|
50
|
-
const result = pipe(client.executeFragment(fragment, $state.snapshot(ref) as typeof ref, options?.()), peek);
|
|
51
|
-
if (result.data === undefined) {
|
|
52
|
-
throw new Error('Fragment data not found');
|
|
53
|
-
}
|
|
54
|
-
data = result.data;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
let state = $state(data);
|
|
58
|
-
|
|
59
|
-
$effect(() => {
|
|
60
|
-
const currentRef = fragmentRef();
|
|
61
|
-
if (currentRef == null) {
|
|
62
|
-
state = null;
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const unsubscribe = pipe(
|
|
67
|
-
client.executeFragment(fragment, $state.snapshot(currentRef) as typeof currentRef, options?.()),
|
|
68
|
-
subscribe({
|
|
69
|
-
next: (result: OperationResult) => {
|
|
70
|
-
if (result.data !== undefined) {
|
|
71
|
-
state = result.data;
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
}),
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
return () => {
|
|
78
|
-
unsubscribe();
|
|
79
|
-
};
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return {
|
|
83
|
-
get data() {
|
|
84
|
-
return state;
|
|
85
|
-
},
|
|
86
|
-
};
|
|
87
|
-
}) as unknown as CreateFragmentFn;
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import type { VariablesOf, DataOf, Artifact, MutationOptions } from '@mearie/core';
|
|
2
|
-
import { AggregatedError } from '@mearie/core';
|
|
3
|
-
import { pipe, take, collect } from '@mearie/core/stream';
|
|
4
|
-
import { getClient } from './client-context.svelte.ts';
|
|
5
|
-
|
|
6
|
-
export type MutationResult<T extends Artifact<'mutation'>> =
|
|
7
|
-
| {
|
|
8
|
-
data: undefined;
|
|
9
|
-
loading: true;
|
|
10
|
-
error: undefined;
|
|
11
|
-
}
|
|
12
|
-
| {
|
|
13
|
-
data: DataOf<T> | undefined;
|
|
14
|
-
loading: false;
|
|
15
|
-
error: undefined;
|
|
16
|
-
}
|
|
17
|
-
| {
|
|
18
|
-
data: DataOf<T> | undefined;
|
|
19
|
-
loading: false;
|
|
20
|
-
error: AggregatedError;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type CreateMutationOptions = MutationOptions;
|
|
24
|
-
|
|
25
|
-
export type Mutation<T extends Artifact<'mutation'>> = [
|
|
26
|
-
(
|
|
27
|
-
...[variables, options]: VariablesOf<T> extends Record<string, never>
|
|
28
|
-
? [undefined?, CreateMutationOptions?]
|
|
29
|
-
: [VariablesOf<T>, CreateMutationOptions?]
|
|
30
|
-
) => Promise<DataOf<T>>,
|
|
31
|
-
MutationResult<T>,
|
|
32
|
-
];
|
|
33
|
-
|
|
34
|
-
export const createMutation = <T extends Artifact<'mutation'>>(mutation: T): Mutation<T> => {
|
|
35
|
-
const client = getClient();
|
|
36
|
-
|
|
37
|
-
let data = $state<DataOf<T> | undefined>();
|
|
38
|
-
let loading = $state<boolean>(false);
|
|
39
|
-
let error = $state<AggregatedError | undefined>();
|
|
40
|
-
|
|
41
|
-
const execute = async (variables?: VariablesOf<T>, options?: CreateMutationOptions): Promise<DataOf<T>> => {
|
|
42
|
-
loading = true;
|
|
43
|
-
error = undefined;
|
|
44
|
-
|
|
45
|
-
try {
|
|
46
|
-
const result = await pipe(
|
|
47
|
-
// @ts-expect-error - conditional signature makes this hard to type correctly
|
|
48
|
-
client.executeMutation(mutation, variables, options),
|
|
49
|
-
take(1),
|
|
50
|
-
collect,
|
|
51
|
-
);
|
|
52
|
-
|
|
53
|
-
if (result.errors && result.errors.length > 0) {
|
|
54
|
-
const err = new AggregatedError(result.errors);
|
|
55
|
-
|
|
56
|
-
error = err;
|
|
57
|
-
loading = false;
|
|
58
|
-
|
|
59
|
-
throw err;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
data = result.data as DataOf<T>;
|
|
63
|
-
loading = false;
|
|
64
|
-
|
|
65
|
-
return result.data as DataOf<T>;
|
|
66
|
-
} catch (err) {
|
|
67
|
-
if (err instanceof AggregatedError) {
|
|
68
|
-
error = err;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
loading = false;
|
|
72
|
-
|
|
73
|
-
throw err;
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
return [
|
|
78
|
-
execute,
|
|
79
|
-
{
|
|
80
|
-
get data() {
|
|
81
|
-
return data;
|
|
82
|
-
},
|
|
83
|
-
get loading() {
|
|
84
|
-
return loading;
|
|
85
|
-
},
|
|
86
|
-
get error() {
|
|
87
|
-
return error;
|
|
88
|
-
},
|
|
89
|
-
},
|
|
90
|
-
] as Mutation<T>;
|
|
91
|
-
};
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { untrack } from 'svelte';
|
|
2
|
-
import type { Artifact, VariablesOf, DataOf, QueryOptions } from '@mearie/core';
|
|
3
|
-
import { AggregatedError } from '@mearie/core';
|
|
4
|
-
import { pipe, subscribe } from '@mearie/core/stream';
|
|
5
|
-
import { getClient } from './client-context.svelte.ts';
|
|
6
|
-
|
|
7
|
-
export type CreateQueryOptions<T extends Artifact<'query'> = Artifact<'query'>> = QueryOptions<T> & {
|
|
8
|
-
skip?: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type Query<T extends Artifact<'query'>> =
|
|
12
|
-
| {
|
|
13
|
-
data: undefined;
|
|
14
|
-
loading: true;
|
|
15
|
-
error: undefined;
|
|
16
|
-
refetch: () => void;
|
|
17
|
-
}
|
|
18
|
-
| {
|
|
19
|
-
data: DataOf<T>;
|
|
20
|
-
loading: false;
|
|
21
|
-
error: undefined;
|
|
22
|
-
refetch: () => void;
|
|
23
|
-
}
|
|
24
|
-
| {
|
|
25
|
-
data: DataOf<T> | undefined;
|
|
26
|
-
loading: false;
|
|
27
|
-
error: AggregatedError;
|
|
28
|
-
refetch: () => void;
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export type DefinedQuery<T extends Artifact<'query'>> =
|
|
32
|
-
| {
|
|
33
|
-
data: DataOf<T>;
|
|
34
|
-
loading: true;
|
|
35
|
-
error: undefined;
|
|
36
|
-
refetch: () => void;
|
|
37
|
-
}
|
|
38
|
-
| {
|
|
39
|
-
data: DataOf<T>;
|
|
40
|
-
loading: false;
|
|
41
|
-
error: undefined;
|
|
42
|
-
refetch: () => void;
|
|
43
|
-
}
|
|
44
|
-
| {
|
|
45
|
-
data: DataOf<T>;
|
|
46
|
-
loading: false;
|
|
47
|
-
error: AggregatedError;
|
|
48
|
-
refetch: () => void;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
type CreateQueryFn = {
|
|
52
|
-
<T extends Artifact<'query'>>(
|
|
53
|
-
query: T,
|
|
54
|
-
variables: (() => VariablesOf<T>) | undefined,
|
|
55
|
-
options: () => CreateQueryOptions<T> & { initialData: DataOf<T> },
|
|
56
|
-
): DefinedQuery<T>;
|
|
57
|
-
<T extends Artifact<'query'>>(
|
|
58
|
-
query: T,
|
|
59
|
-
...[variables, options]: VariablesOf<T> extends Record<string, never>
|
|
60
|
-
? [undefined?, (() => CreateQueryOptions<T>)?]
|
|
61
|
-
: [() => VariablesOf<T>, (() => CreateQueryOptions<T>)?]
|
|
62
|
-
): Query<T>;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
export const createQuery: CreateQueryFn = (<T extends Artifact<'query'>>(
|
|
66
|
-
query: T,
|
|
67
|
-
variables?: () => VariablesOf<T>,
|
|
68
|
-
options?: () => CreateQueryOptions<T>,
|
|
69
|
-
): Query<T> => {
|
|
70
|
-
const client = getClient();
|
|
71
|
-
|
|
72
|
-
const initialOpts = options?.();
|
|
73
|
-
let data = $state.raw<DataOf<T> | undefined>(initialOpts?.initialData);
|
|
74
|
-
let loading = $state<boolean>(!initialOpts?.skip && !initialOpts?.initialData);
|
|
75
|
-
let error = $state<AggregatedError | undefined>();
|
|
76
|
-
|
|
77
|
-
let unsubscribe: (() => void) | null = null;
|
|
78
|
-
let initialized = false;
|
|
79
|
-
|
|
80
|
-
const execute = () => {
|
|
81
|
-
unsubscribe?.();
|
|
82
|
-
|
|
83
|
-
if (options?.().skip) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (!initialized && !!initialOpts?.initialData) {
|
|
88
|
-
loading = true;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
initialized = true;
|
|
92
|
-
error = undefined;
|
|
93
|
-
|
|
94
|
-
unsubscribe = pipe(
|
|
95
|
-
// @ts-expect-error - conditional signature makes this hard to type correctly
|
|
96
|
-
client.executeQuery(query, typeof variables === 'function' ? variables() : undefined, options?.()),
|
|
97
|
-
subscribe({
|
|
98
|
-
next: (result) => {
|
|
99
|
-
if (result.errors && result.errors.length > 0) {
|
|
100
|
-
error = new AggregatedError(result.errors);
|
|
101
|
-
loading = false;
|
|
102
|
-
} else {
|
|
103
|
-
data = result.data as DataOf<T>;
|
|
104
|
-
loading = false;
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
}),
|
|
108
|
-
);
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const refetch = () => {
|
|
112
|
-
untrack(execute);
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
$effect(() => {
|
|
116
|
-
execute();
|
|
117
|
-
|
|
118
|
-
return () => {
|
|
119
|
-
unsubscribe?.();
|
|
120
|
-
};
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
return {
|
|
124
|
-
get data() {
|
|
125
|
-
return data;
|
|
126
|
-
},
|
|
127
|
-
get loading() {
|
|
128
|
-
return loading;
|
|
129
|
-
},
|
|
130
|
-
get error() {
|
|
131
|
-
return error;
|
|
132
|
-
},
|
|
133
|
-
refetch,
|
|
134
|
-
} as Query<T>;
|
|
135
|
-
}) as unknown as CreateQueryFn;
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import type { VariablesOf, DataOf, Artifact, SubscriptionOptions } from '@mearie/core';
|
|
2
|
-
import { AggregatedError } from '@mearie/core';
|
|
3
|
-
import { pipe, subscribe } from '@mearie/core/stream';
|
|
4
|
-
import { getClient } from './client-context.svelte.ts';
|
|
5
|
-
|
|
6
|
-
export type Subscription<T extends Artifact<'subscription'>> =
|
|
7
|
-
| {
|
|
8
|
-
data: undefined;
|
|
9
|
-
loading: true;
|
|
10
|
-
error: undefined;
|
|
11
|
-
}
|
|
12
|
-
| {
|
|
13
|
-
data: DataOf<T> | undefined;
|
|
14
|
-
loading: false;
|
|
15
|
-
error: undefined;
|
|
16
|
-
}
|
|
17
|
-
| {
|
|
18
|
-
data: DataOf<T> | undefined;
|
|
19
|
-
loading: false;
|
|
20
|
-
error: AggregatedError;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type CreateSubscriptionOptions<T extends Artifact<'subscription'>> = SubscriptionOptions & {
|
|
24
|
-
skip?: boolean;
|
|
25
|
-
onData?: (data: DataOf<T>) => void;
|
|
26
|
-
onError?: (error: AggregatedError) => void;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const createSubscription = <T extends Artifact<'subscription'>>(
|
|
30
|
-
subscription: T,
|
|
31
|
-
...[variables, options]: VariablesOf<T> extends Record<string, never>
|
|
32
|
-
? [undefined?, (() => CreateSubscriptionOptions<T>)?]
|
|
33
|
-
: [() => VariablesOf<T>, (() => CreateSubscriptionOptions<T>)?]
|
|
34
|
-
): Subscription<T> => {
|
|
35
|
-
const client = getClient();
|
|
36
|
-
|
|
37
|
-
let data = $state.raw<DataOf<T> | undefined>();
|
|
38
|
-
let loading = $state<boolean>(!options?.().skip);
|
|
39
|
-
let error = $state<AggregatedError | undefined>();
|
|
40
|
-
|
|
41
|
-
$effect(() => {
|
|
42
|
-
if (options?.().skip) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
loading = true;
|
|
47
|
-
error = undefined;
|
|
48
|
-
|
|
49
|
-
const unsubscribe = pipe(
|
|
50
|
-
// @ts-expect-error - conditional signature makes this hard to type correctly
|
|
51
|
-
client.executeSubscription(subscription, typeof variables === 'function' ? variables() : undefined, options?.()),
|
|
52
|
-
subscribe({
|
|
53
|
-
next: (result) => {
|
|
54
|
-
if (result.errors && result.errors.length > 0) {
|
|
55
|
-
const err = new AggregatedError(result.errors);
|
|
56
|
-
|
|
57
|
-
error = err;
|
|
58
|
-
loading = false;
|
|
59
|
-
|
|
60
|
-
options?.().onError?.(err);
|
|
61
|
-
} else {
|
|
62
|
-
const resultData = result.data as DataOf<T>;
|
|
63
|
-
|
|
64
|
-
data = resultData;
|
|
65
|
-
loading = false;
|
|
66
|
-
|
|
67
|
-
options?.().onData?.(resultData);
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
}),
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
return () => {
|
|
74
|
-
unsubscribe();
|
|
75
|
-
};
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
return {
|
|
79
|
-
get data() {
|
|
80
|
-
return data;
|
|
81
|
-
},
|
|
82
|
-
get loading() {
|
|
83
|
-
return loading;
|
|
84
|
-
},
|
|
85
|
-
get error() {
|
|
86
|
-
return error;
|
|
87
|
-
},
|
|
88
|
-
} as Subscription<T>;
|
|
89
|
-
};
|
package/src/index.svelte.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export * from '@mearie/core';
|
|
2
|
-
export { setClient, getClient } from './client-context.svelte.ts';
|
|
3
|
-
export { createQuery, type Query, type DefinedQuery, type CreateQueryOptions } from './create-query.svelte.ts';
|
|
4
|
-
export { createSubscription, type Subscription, type CreateSubscriptionOptions } from './create-subscription.svelte.ts';
|
|
5
|
-
export {
|
|
6
|
-
createMutation,
|
|
7
|
-
type Mutation,
|
|
8
|
-
type MutationResult,
|
|
9
|
-
type CreateMutationOptions,
|
|
10
|
-
} from './create-mutation.svelte.ts';
|
|
11
|
-
export {
|
|
12
|
-
createFragment,
|
|
13
|
-
type Fragment,
|
|
14
|
-
type FragmentList,
|
|
15
|
-
type OptionalFragment,
|
|
16
|
-
type CreateFragmentOptions,
|
|
17
|
-
} from './create-fragment.svelte.ts';
|