@mearie/svelte 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.
@@ -19,16 +19,19 @@ const getClient = () => {
19
19
  const createQuery = ((query, variables, options) => {
20
20
  const client = getClient();
21
21
  const initialOpts = options?.();
22
- let data = $state.raw(initialOpts?.initialData);
22
+ let data = $state(initialOpts?.initialData);
23
23
  let loading = $state(!initialOpts?.skip && !initialOpts?.initialData);
24
24
  let error = $state();
25
25
  let metadata = $state();
26
26
  let unsubscribe = null;
27
27
  let initialized = false;
28
- const execute = () => {
28
+ const execute = (force = false) => {
29
29
  unsubscribe?.();
30
- if (options?.().skip) return;
31
- if (!initialized && !!initialOpts?.initialData) loading = true;
30
+ if (!force && options?.().skip) {
31
+ loading = false;
32
+ return;
33
+ }
34
+ if (initialized || !initialOpts?.initialData) loading = true;
32
35
  initialized = true;
33
36
  error = void 0;
34
37
  unsubscribe = (0, _mearie_core_stream.pipe)(client.executeQuery(query, typeof variables === "function" ? variables() : void 0, options?.()), (0, _mearie_core_stream.subscribe)({ next: (result) => {
@@ -37,13 +40,17 @@ const createQuery = ((query, variables, options) => {
37
40
  error = new _mearie_core.AggregatedError(result.errors);
38
41
  loading = false;
39
42
  } else {
40
- data = result.data;
43
+ const patches = result.metadata?.cache?.patches;
44
+ if (patches) {
45
+ const root = (0, _mearie_core.applyPatchesMutable)(data, patches);
46
+ if (root !== void 0) data = root;
47
+ } else data = result.data;
41
48
  loading = false;
42
49
  }
43
50
  } }));
44
51
  };
45
52
  const refetch = () => {
46
- (0, svelte.untrack)(execute);
53
+ (0, svelte.untrack)(() => execute(true));
47
54
  };
48
55
  $effect(() => {
49
56
  execute();
@@ -183,9 +190,13 @@ const createFragment = ((fragment, fragmentRef, options) => {
183
190
  metadata = void 0;
184
191
  return;
185
192
  }
186
- const unsubscribe = (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, $state.snapshot(currentRef), options?.()), (0, _mearie_core_stream.subscribe)({ next: (result) => {
193
+ const unsubscribe = (0, _mearie_core_stream.pipe)(client.executeFragment(fragment, (0, svelte.untrack)(() => $state.snapshot(currentRef)), options?.()), (0, _mearie_core_stream.subscribe)({ next: (result) => {
187
194
  metadata = result.metadata;
188
- if (result.data !== void 0) state = result.data;
195
+ const patches = result.metadata?.cache?.patches;
196
+ if (patches) {
197
+ const root = (0, _mearie_core.applyPatchesMutable)(state, patches);
198
+ if (root !== void 0) state = root;
199
+ } else if (result.data !== void 0) state = result.data;
189
200
  } }));
190
201
  return () => {
191
202
  unsubscribe();
@@ -1,4 +1,4 @@
1
- import { AggregatedError } from "@mearie/core";
1
+ import { AggregatedError, applyPatchesMutable } from "@mearie/core";
2
2
  import { getContext, setContext, untrack } from "svelte";
3
3
  import { collect, peek, pipe, subscribe, take } from "@mearie/core/stream";
4
4
 
@@ -20,16 +20,19 @@ const getClient = () => {
20
20
  const createQuery = ((query, variables, options) => {
21
21
  const client = getClient();
22
22
  const initialOpts = options?.();
23
- let data = $state.raw(initialOpts?.initialData);
23
+ let data = $state(initialOpts?.initialData);
24
24
  let loading = $state(!initialOpts?.skip && !initialOpts?.initialData);
25
25
  let error = $state();
26
26
  let metadata = $state();
27
27
  let unsubscribe = null;
28
28
  let initialized = false;
29
- const execute = () => {
29
+ const execute = (force = false) => {
30
30
  unsubscribe?.();
31
- if (options?.().skip) return;
32
- if (!initialized && !!initialOpts?.initialData) loading = true;
31
+ if (!force && options?.().skip) {
32
+ loading = false;
33
+ return;
34
+ }
35
+ if (initialized || !initialOpts?.initialData) loading = true;
33
36
  initialized = true;
34
37
  error = void 0;
35
38
  unsubscribe = pipe(client.executeQuery(query, typeof variables === "function" ? variables() : void 0, options?.()), subscribe({ next: (result) => {
@@ -38,13 +41,17 @@ const createQuery = ((query, variables, options) => {
38
41
  error = new AggregatedError(result.errors);
39
42
  loading = false;
40
43
  } else {
41
- data = result.data;
44
+ const patches = result.metadata?.cache?.patches;
45
+ if (patches) {
46
+ const root = applyPatchesMutable(data, patches);
47
+ if (root !== void 0) data = root;
48
+ } else data = result.data;
42
49
  loading = false;
43
50
  }
44
51
  } }));
45
52
  };
46
53
  const refetch = () => {
47
- untrack(execute);
54
+ untrack(() => execute(true));
48
55
  };
49
56
  $effect(() => {
50
57
  execute();
@@ -184,9 +191,13 @@ const createFragment = ((fragment, fragmentRef, options) => {
184
191
  metadata = void 0;
185
192
  return;
186
193
  }
187
- const unsubscribe = pipe(client.executeFragment(fragment, $state.snapshot(currentRef), options?.()), subscribe({ next: (result) => {
194
+ const unsubscribe = pipe(client.executeFragment(fragment, untrack(() => $state.snapshot(currentRef)), options?.()), subscribe({ next: (result) => {
188
195
  metadata = result.metadata;
189
- if (result.data !== void 0) state = result.data;
196
+ const patches = result.metadata?.cache?.patches;
197
+ if (patches) {
198
+ const root = applyPatchesMutable(state, patches);
199
+ if (root !== void 0) state = root;
200
+ } else if (result.data !== void 0) state = result.data;
190
201
  } }));
191
202
  return () => {
192
203
  unsubscribe();
@@ -1,3 +1,5 @@
1
+ import { untrack } from 'svelte';
2
+ import { applyPatchesMutable } from '@mearie/core';
1
3
  import { pipe, subscribe, peek } from '@mearie/core/stream';
2
4
  import { getClient } from "./client-context.svelte.js";
3
5
  export const createFragment = ((fragment, fragmentRef, options) => {
@@ -25,10 +27,16 @@ export const createFragment = ((fragment, fragmentRef, options) => {
25
27
  metadata = undefined;
26
28
  return;
27
29
  }
28
- const unsubscribe = pipe(client.executeFragment(fragment, $state.snapshot(currentRef), options?.()), subscribe({
30
+ const unsubscribe = pipe(client.executeFragment(fragment, untrack(() => $state.snapshot(currentRef)), options?.()), subscribe({
29
31
  next: (result) => {
30
32
  metadata = result.metadata;
31
- if (result.data !== undefined) {
33
+ const patches = result.metadata?.cache?.patches;
34
+ if (patches) {
35
+ const root = applyPatchesMutable(state, patches);
36
+ if (root !== undefined)
37
+ state = root;
38
+ }
39
+ else if (result.data !== undefined) {
32
40
  state = result.data;
33
41
  }
34
42
  },
@@ -1,22 +1,23 @@
1
1
  import { untrack } from 'svelte';
2
- import { AggregatedError } from '@mearie/core';
2
+ import { AggregatedError, applyPatchesMutable } from '@mearie/core';
3
3
  import { pipe, subscribe } from '@mearie/core/stream';
4
4
  import { getClient } from "./client-context.svelte.js";
5
5
  export const createQuery = ((query, variables, options) => {
6
6
  const client = getClient();
7
7
  const initialOpts = options?.();
8
- let data = $state.raw(initialOpts?.initialData);
8
+ let data = $state(initialOpts?.initialData);
9
9
  let loading = $state(!initialOpts?.skip && !initialOpts?.initialData);
10
10
  let error = $state();
11
11
  let metadata = $state();
12
12
  let unsubscribe = null;
13
13
  let initialized = false;
14
- const execute = () => {
14
+ const execute = (force = false) => {
15
15
  unsubscribe?.();
16
- if (options?.().skip) {
16
+ if (!force && options?.().skip) {
17
+ loading = false;
17
18
  return;
18
19
  }
19
- if (!initialized && !!initialOpts?.initialData) {
20
+ if (initialized || !initialOpts?.initialData) {
20
21
  loading = true;
21
22
  }
22
23
  initialized = true;
@@ -31,14 +32,22 @@ export const createQuery = ((query, variables, options) => {
31
32
  loading = false;
32
33
  }
33
34
  else {
34
- data = result.data;
35
+ const patches = result.metadata?.cache?.patches;
36
+ if (patches) {
37
+ const root = applyPatchesMutable(data, patches);
38
+ if (root !== undefined)
39
+ data = root;
40
+ }
41
+ else {
42
+ data = result.data;
43
+ }
35
44
  loading = false;
36
45
  }
37
46
  },
38
47
  }));
39
48
  };
40
49
  const refetch = () => {
41
- untrack(execute);
50
+ untrack(() => execute(true));
42
51
  };
43
52
  $effect(() => {
44
53
  execute();
@@ -0,0 +1,48 @@
1
+ import { vi } from 'vitest';
2
+ import { GraphQLError } from '@mearie/core';
3
+ import { makeSubject } from '@mearie/core/stream';
4
+ export const createMockClient = () => {
5
+ const subjects = {
6
+ query: makeSubject(),
7
+ mutation: makeSubject(),
8
+ subscription: makeSubject(),
9
+ fragment: makeSubject(),
10
+ };
11
+ const client = {
12
+ executeQuery: vi.fn(() => subjects.query.source),
13
+ executeMutation: vi.fn(() => subjects.mutation.source),
14
+ executeSubscription: vi.fn(() => subjects.subscription.source),
15
+ executeFragment: vi.fn(() => subjects.fragment.source),
16
+ };
17
+ return { client, subjects };
18
+ };
19
+ export const mockQuery = {
20
+ kind: 'query',
21
+ name: 'TestQuery',
22
+ body: 'query TestQuery { user { id name } }',
23
+ selections: [],
24
+ };
25
+ export const mockMutation = {
26
+ kind: 'mutation',
27
+ name: 'TestMutation',
28
+ body: 'mutation TestMutation { updateUser { id } }',
29
+ selections: [],
30
+ };
31
+ export const mockSubscription = {
32
+ kind: 'subscription',
33
+ name: 'TestSubscription',
34
+ body: 'subscription TestSubscription { onUpdate { id } }',
35
+ selections: [],
36
+ };
37
+ export const mockFragment = {
38
+ kind: 'fragment',
39
+ name: 'TestFragment',
40
+ body: 'fragment TestFragment on User { id name }',
41
+ selections: [],
42
+ };
43
+ export const makeResult = (data, opts) => ({
44
+ data,
45
+ errors: opts?.errors?.map((e) => new GraphQLError(e.message)),
46
+ metadata: opts?.metadata,
47
+ operation: {},
48
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mearie/svelte",
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",
@@ -53,9 +53,11 @@
53
53
  "README.md"
54
54
  ],
55
55
  "dependencies": {
56
- "@mearie/core": "0.5.2"
56
+ "@mearie/core": "0.6.0"
57
57
  },
58
58
  "devDependencies": {
59
+ "@sveltejs/vite-plugin-svelte": "^6.2.4",
60
+ "happy-dom": "^20.8.3",
59
61
  "svelte": "^5.53.3",
60
62
  "tsdown": "^0.20.3",
61
63
  "typescript": "^5.9.3"