Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve #465 update query on mutation success shorthand #469

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions packages/core/src/update/__tests__/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,35 @@ describe('update', () => {
expect(listeners.onStart).toBeCalledTimes(1); // only original call
});

test('refetch true causes query to be refetched with latest params shorthand', async () => {
const query = createQuery({
handler: vi.fn().mockImplementation((params: number) => setTimeout(100).then(() => params.toString())),
initialData: 'initial',
});

const mutation = createMutation({ handler: vi.fn().mockResolvedValue('from mutation') });

update(query, { on: mutation });

const scope = fork();
const { listeners } = watchRemoteOperation(query, scope);

await allSettled(query.start, { scope, params: 1 });
await allSettled(query.start, { scope, params: 2 });

allSettled(mutation.start, { scope }); // Do not wait
await setTimeout(1); // Async nature of mutations
expect(scope.getState(query.$data)).toEqual('initial');
expect(scope.getState(query.$stale)).toBeTruthy();

await allSettled(scope);
expect(scope.getState(query.$data)).toEqual('2');

expect(listeners.onStart).toBeCalledTimes(3); // 2 original and 1 by refetch
expect(listeners.onStart).toHaveBeenNthCalledWith(3, 2); // refetch uses latest params
expect(scope.getState(query.$stale)).toBeFalsy();
});

test('refetch true causes query to be refetched with latest params', async () => {
const query = createQuery({
handler: vi
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/update/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ export function update<
Q extends Query<any, any, any, any>,
M extends Mutation<any, any, any>,
BySuccessSource = void,
ByFailureSource = void,
ByFailureSource = void
>(
query: Q,
{
on: mutation,
by: rules,
}: {
on: M;
by: {
by?: {
success: DynamicallySourcedField<
{
query: QueryState<Q>;
Expand Down Expand Up @@ -94,12 +94,14 @@ export function update<
refetch?: Refetch<Q>;
}>();

const success = rules?.success || (() => ({ error: null, refetch: true }));

split({
source: sample({
clock: mutation.finished.success,
source: {
partialRule: normalizeSourced({
field: rules.success,
field: success,
}),
queryState: $queryState,
},
Expand All @@ -115,7 +117,7 @@ export function update<
cases: { fillData: fillQueryData, __: fillQueryError },
});

if (rules.failure) {
if (rules?.failure) {
split({
source: sample({
clock: mutation.finished.failure,
Expand Down
Loading