Skip to content

Commit 7c09498

Browse files
authored
Merge pull request psteinroe#399 from psteinroe/fix/revalidate-function-call
2 parents 5cea6b9 + cee8acb commit 7c09498

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

.changeset/short-beds-knock.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@supabase-cache-helpers/postgrest-swr": patch
3+
---
4+
5+
fix: properly pass parameters to mutate for revalidation

packages/postgrest-swr/__tests__/query/use-pagination-query.integration.spec.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import React, { useState } from 'react';
55
import {
66
useInfiniteOffsetPaginationQuery,
77
fetchOffsetPaginationHasMoreFallbackData,
8+
useInsertMutation,
89
} from '../../src';
910
import type { Database } from '../database.types';
1011
import { renderWithConfig } from '../utils';
@@ -221,4 +222,57 @@ describe('useInfiniteOffsetPaginationQuery', () => {
221222
renderWithConfig(<Page />, { provider: () => provider });
222223
await screen.findByText(contacts[0].username!, {}, { timeout: 10000 });
223224
});
225+
226+
it('revalidation should work', async () => {
227+
function Page() {
228+
const [success, setSuccess] = useState(false);
229+
const { currentPage, isLoading, setPage } =
230+
useInfiniteOffsetPaginationQuery(
231+
client
232+
.from('contact')
233+
.select('id,username')
234+
.ilike('username', `${testRunPrefix}%`)
235+
.order('username', { ascending: true }),
236+
{ pageSize: 1, revalidateOnReconnect: true },
237+
);
238+
239+
const { trigger: insert } = useInsertMutation(
240+
client.from('contact_note'),
241+
['id'],
242+
null,
243+
{
244+
revalidateTables: [{ schema: 'public', table: 'contact' }],
245+
onSuccess: () => setSuccess(true),
246+
onError: (error) => console.error(error),
247+
},
248+
);
249+
250+
const contact = currentPage?.[0];
251+
252+
return (
253+
<div>
254+
<div
255+
data-testid="revalidate"
256+
onClick={() => insert([{ contact_id: contact!.id, text: 'Test' }])}
257+
/>
258+
<div data-testid="pages">
259+
{(currentPage ?? [])[0]?.username ?? 'undefined'}
260+
</div>
261+
<div>{`isLoading: ${isLoading}`}</div>
262+
<div>{`success: ${success}`}</div>
263+
</div>
264+
);
265+
}
266+
267+
renderWithConfig(<Page />, { provider: () => provider });
268+
await screen.findByText('isLoading: false', {}, { timeout: 10000 });
269+
await screen.findByText(contacts[0].username!, {}, { timeout: 10000 });
270+
fireEvent.click(screen.getByTestId('revalidate'));
271+
await screen.findByText('success: true', {}, { timeout: 10000 });
272+
await screen.findByText(
273+
`${testRunPrefix}-username-1`,
274+
{},
275+
{ timeout: 10000 },
276+
);
277+
});
224278
});

packages/postgrest-swr/src/cache/use-delete-item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function useDeleteItem<Type extends Record<string, unknown>>(
3131
cacheKeys: getMutableKeys(Array.from(cache.keys())),
3232
getPostgrestFilter,
3333
revalidate: (key) => {
34-
mutate(key, { ...opts, revalidate: true });
34+
mutate(key, null, { ...opts, revalidate: true });
3535
},
3636
mutate: (key, data) => {
3737
mutate(key, data, { ...opts, revalidate: opts?.revalidate ?? false });

packages/postgrest-swr/src/cache/use-mutate-item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function useMutateItem<Type extends Record<string, unknown>>(
3333
cacheKeys: getMutableKeys(Array.from(cache.keys())),
3434
getPostgrestFilter,
3535
revalidate: (key) => {
36-
mutate(key, { ...opts, revalidate: true });
36+
mutate(key, null, { ...opts, revalidate: true });
3737
},
3838
mutate: (key, data) => {
3939
mutate(key, data, {

packages/postgrest-swr/src/cache/use-upsert-item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function useUpsertItem<Type extends Record<string, unknown>>(
3131
cacheKeys: getMutableKeys(Array.from(cache.keys())),
3232
getPostgrestFilter,
3333
revalidate: (key) => {
34-
mutate(key, { ...opts, revalidate: true });
34+
mutate(key, null, { ...opts, revalidate: true });
3535
},
3636
mutate: (key, data) => {
3737
mutate(key, data, {

0 commit comments

Comments
 (0)