diff --git a/.changeset/rude-glasses-battle.md b/.changeset/rude-glasses-battle.md new file mode 100644 index 00000000..709e9746 --- /dev/null +++ b/.changeset/rude-glasses-battle.md @@ -0,0 +1,5 @@ +--- +"@supabase-cache-helpers/postgrest-swr": patch +--- + +fix: pass key to mutate when revalidating in swr diff --git a/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx b/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx index 846fe54f..73e3a29d 100644 --- a/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx +++ b/packages/postgrest-swr/__tests__/mutate/use-update-mutation.integration.spec.tsx @@ -160,4 +160,82 @@ describe('useUpdateMutation', () => { expect(screen.getByTestId('count').textContent).toEqual('count: 1'); await screen.findByText('success: true', {}, { timeout: 10000 }); }); + + it('revalidate relations should work', async () => { + const USERNAME = `${testRunPrefix}-rev-relations`; + const USERNAME_UPDATED = `${testRunPrefix}-rev-relations-updated`; + const NOTE_1 = `${testRunPrefix}-note-1`; + const NOTE_2 = `${testRunPrefix}-note-2`; + + const { data: contact } = await client + .from('contact') + .insert([{ username: USERNAME }]) + .select('id') + .single() + .throwOnError(); + + await client + .from('contact_note') + .insert([{ contact_id: contact!.id, text: NOTE_1 }]) + .throwOnError(); + + function Page() { + const [success, setSuccess] = useState(false); + const { data } = useQuery( + client + .from('contact_note') + .select('id,text') + .ilike('text', `${testRunPrefix}%`), + { + revalidateOnFocus: false, + revalidateOnReconnect: false, + }, + ); + const { trigger: update } = useUpdateMutation( + client.from('contact'), + ['id'], + 'id', + { + revalidateTables: [{ schema: 'public', table: 'contact_note' }], + onSuccess: () => setSuccess(true), + onError: (error) => console.error(error), + }, + ); + return ( +
+
+ await update({ + id: contact!.id, + username: USERNAME_UPDATED, + }) + } + /> + + {(data ?? []) + .map((d) => d.text) + .sort() + .join(',')} + + {`success: ${success}`} +
+ ); + } + + renderWithConfig(, { provider: () => provider }); + await screen.findByText([NOTE_1].join(','), {}, { timeout: 10000 }); + + await client + .from('contact_note') + .insert([{ contact_id: contact!.id, text: NOTE_2 }]) + .throwOnError(); + + await screen.findByText([NOTE_1].join(','), {}, { timeout: 10000 }); + + fireEvent.click(screen.getByTestId('update')); + + await screen.findByText([NOTE_1, NOTE_2].join(','), {}, { timeout: 10000 }); + await screen.findByText('success: true', {}, { timeout: 10000 }); + }); }); diff --git a/packages/postgrest-swr/src/cache/use-delete-item.ts b/packages/postgrest-swr/src/cache/use-delete-item.ts index 38f8c851..f5ac0fa1 100644 --- a/packages/postgrest-swr/src/cache/use-delete-item.ts +++ b/packages/postgrest-swr/src/cache/use-delete-item.ts @@ -30,7 +30,9 @@ export function useDeleteItem>( { cacheKeys: getMutableKeys(Array.from(cache.keys())), getPostgrestFilter, - revalidate: (key) => mutate({ ...opts, revalidate: true }), + revalidate: (key) => { + mutate(key, { ...opts, revalidate: true }); + }, mutate: (key, data) => { mutate(key, data, { ...opts, revalidate: opts?.revalidate ?? false }); }, diff --git a/packages/postgrest-swr/src/cache/use-mutate-item.ts b/packages/postgrest-swr/src/cache/use-mutate-item.ts index 8caa3812..e8bb83c4 100644 --- a/packages/postgrest-swr/src/cache/use-mutate-item.ts +++ b/packages/postgrest-swr/src/cache/use-mutate-item.ts @@ -32,7 +32,9 @@ export function useMutateItem>( { cacheKeys: getMutableKeys(Array.from(cache.keys())), getPostgrestFilter, - revalidate: (key) => mutate({ ...opts, revalidate: true }), + revalidate: (key) => { + mutate(key, { ...opts, revalidate: true }); + }, mutate: (key, data) => { mutate(key, data, { ...opts, diff --git a/packages/postgrest-swr/src/cache/use-upsert-item.ts b/packages/postgrest-swr/src/cache/use-upsert-item.ts index a1f16ad1..74cda6c8 100644 --- a/packages/postgrest-swr/src/cache/use-upsert-item.ts +++ b/packages/postgrest-swr/src/cache/use-upsert-item.ts @@ -30,7 +30,9 @@ export function useUpsertItem>( { cacheKeys: getMutableKeys(Array.from(cache.keys())), getPostgrestFilter, - revalidate: (key) => mutate({ ...opts, revalidate: true }), + revalidate: (key) => { + mutate(key, { ...opts, revalidate: true }); + }, mutate: (key, data) => { mutate(key, data, { ...opts,