@mearie/svelte 0.3.5 → 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,7 +19,7 @@ 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();
@@ -40,7 +40,11 @@ const createQuery = ((query, variables, options) => {
40
40
  error = new _mearie_core.AggregatedError(result.errors);
41
41
  loading = false;
42
42
  } else {
43
- 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;
44
48
  loading = false;
45
49
  }
46
50
  } }));
@@ -186,9 +190,13 @@ const createFragment = ((fragment, fragmentRef, options) => {
186
190
  metadata = void 0;
187
191
  return;
188
192
  }
189
- 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) => {
190
194
  metadata = result.metadata;
191
- 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;
192
200
  } }));
193
201
  return () => {
194
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,7 +20,7 @@ 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();
@@ -41,7 +41,11 @@ const createQuery = ((query, variables, options) => {
41
41
  error = new AggregatedError(result.errors);
42
42
  loading = false;
43
43
  } else {
44
- 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;
45
49
  loading = false;
46
50
  }
47
51
  } }));
@@ -187,9 +191,13 @@ const createFragment = ((fragment, fragmentRef, options) => {
187
191
  metadata = void 0;
188
192
  return;
189
193
  }
190
- 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) => {
191
195
  metadata = result.metadata;
192
- 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;
193
201
  } }));
194
202
  return () => {
195
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,11 +1,11 @@
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();
@@ -32,7 +32,15 @@ export const createQuery = ((query, variables, options) => {
32
32
  loading = false;
33
33
  }
34
34
  else {
35
- 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
+ }
36
44
  loading = false;
37
45
  }
38
46
  },
@@ -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.5",
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"