Skip to content

Commit

Permalink
feat(solid): reconcile data updates (#3674)
Browse files Browse the repository at this point in the history
  • Loading branch information
XiNiHa authored Sep 14, 2024
1 parent b9f34fd commit 6308211
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/perfect-worms-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/solid': patch
---

feat(solid): reconcile data updates
19 changes: 11 additions & 8 deletions packages/solid-urql/src/createMutation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createStore } from 'solid-js/store';
import { createStore, reconcile } from 'solid-js/store';
import {
type AnyVariables,
type DocumentInput,
Expand All @@ -10,6 +10,7 @@ import {
} from '@urql/core';
import { useClient } from './context';
import { pipe, onPush, filter, take, toPromise } from 'wonka';
import { batch } from 'solid-js';

export type CreateMutationState<
Data = any,
Expand Down Expand Up @@ -158,13 +159,15 @@ export const createMutation = <
return pipe(
client.executeMutation(request, context),
onPush(result => {
setState({
fetching: false,
stale: result.stale,
data: result.data,
error: result.error,
extensions: result.extensions,
operation: result.operation,
batch(() => {
setState('data', reconcile(result.data));
setState({
fetching: false,
stale: result.stale,
error: result.error,
extensions: result.extensions,
operation: result.operation,
});
});
}),
filter(result => !result.hasNext),
Expand Down
25 changes: 14 additions & 11 deletions packages/solid-urql/src/createQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
createRequest,
} from '@urql/core';
import {
batch,
createComputed,
createMemo,
createResource,
createSignal,
onCleanup,
} from 'solid-js';
import { createStore, produce } from 'solid-js/store';
import { createStore, produce, reconcile } from 'solid-js/store';
import { useClient } from './context';
import { type MaybeAccessor, asAccessor } from './utils';
import type { Source, Subscription } from 'wonka';
Expand Down Expand Up @@ -258,16 +259,18 @@ export const createQuery = <
);
}),
subscribe(res => {
setResult(
produce(draft => {
draft.data = res.data;
draft.stale = !!res.stale;
draft.fetching = false;
draft.error = res.error;
draft.operation = res.operation;
draft.extensions = res.extensions;
})
);
batch(() => {
setResult('data', reconcile(res.data));
setResult(
produce(draft => {
draft.stale = !!res.stale;
draft.fetching = false;
draft.error = res.error;
draft.operation = res.operation;
draft.extensions = res.extensions;
})
);
});
})
).unsubscribe
);
Expand Down
46 changes: 29 additions & 17 deletions packages/solid-urql/src/createSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ import {
createRequest,
} from '@urql/core';
import { useClient } from './context';
import { createStore, produce } from 'solid-js/store';
import { createComputed, createSignal, onCleanup } from 'solid-js';
import { createStore, produce, reconcile } from 'solid-js/store';
import {
batch,
createComputed,
createSignal,
onCleanup,
untrack,
} from 'solid-js';
import { type Source, onEnd, pipe, subscribe } from 'wonka';

/** Triggers {@link createSubscription} to re-execute a GraphQL subscription operation.
Expand Down Expand Up @@ -266,21 +272,27 @@ export const createSubscription = <
);
}),
subscribe(res => {
setState(
produce(draft => {
draft.data =
res.data !== undefined
? typeof handler === 'function'
? handler(draft.data, res.data)
: res.data
: (draft.data as any);
draft.stale = !!res.stale;
draft.fetching = true;
draft.error = res.error;
draft.operation = res.operation;
draft.extensions = res.extensions;
})
);
batch(() => {
if (res.data !== undefined) {
const newData =
typeof handler === 'function'
? handler(
untrack(() => state.data),
res.data
)
: (res.data as Result);
setState('data', reconcile(newData));
}
setState(
produce(draft => {
draft.stale = !!res.stale;
draft.fetching = true;
draft.error = res.error;
draft.operation = res.operation;
draft.extensions = res.extensions;
})
);
});
})
).unsubscribe
);
Expand Down

0 comments on commit 6308211

Please sign in to comment.