diff --git a/.changeset/short-beds-knock.md b/.changeset/short-beds-knock.md new file mode 100644 index 00000000..09fdc53a --- /dev/null +++ b/.changeset/short-beds-knock.md @@ -0,0 +1,5 @@ +--- +"@supabase-cache-helpers/postgrest-swr": patch +--- + +fix: properly pass parameters to mutate for revalidation diff --git a/packages/postgrest-swr/__tests__/query/use-pagination-query.integration.spec.tsx b/packages/postgrest-swr/__tests__/query/use-pagination-query.integration.spec.tsx index 7bb63178..c015f693 100644 --- a/packages/postgrest-swr/__tests__/query/use-pagination-query.integration.spec.tsx +++ b/packages/postgrest-swr/__tests__/query/use-pagination-query.integration.spec.tsx @@ -5,6 +5,7 @@ import React, { useState } from 'react'; import { useInfiniteOffsetPaginationQuery, fetchOffsetPaginationHasMoreFallbackData, + useInsertMutation, } from '../../src'; import type { Database } from '../database.types'; import { renderWithConfig } from '../utils'; @@ -221,4 +222,57 @@ describe('useInfiniteOffsetPaginationQuery', () => { renderWithConfig(, { provider: () => provider }); await screen.findByText(contacts[0].username!, {}, { timeout: 10000 }); }); + + it('revalidation should work', async () => { + function Page() { + const [success, setSuccess] = useState(false); + const { currentPage, isLoading, setPage } = + useInfiniteOffsetPaginationQuery( + client + .from('contact') + .select('id,username') + .ilike('username', `${testRunPrefix}%`) + .order('username', { ascending: true }), + { pageSize: 1, revalidateOnReconnect: true }, + ); + + const { trigger: insert } = useInsertMutation( + client.from('contact_note'), + ['id'], + null, + { + revalidateTables: [{ schema: 'public', table: 'contact' }], + onSuccess: () => setSuccess(true), + onError: (error) => console.error(error), + }, + ); + + const contact = currentPage?.[0]; + + return ( +
+
insert([{ contact_id: contact!.id, text: 'Test' }])} + /> +
+ {(currentPage ?? [])[0]?.username ?? 'undefined'} +
+
{`isLoading: ${isLoading}`}
+
{`success: ${success}`}
+
+ ); + } + + renderWithConfig(, { provider: () => provider }); + await screen.findByText('isLoading: false', {}, { timeout: 10000 }); + await screen.findByText(contacts[0].username!, {}, { timeout: 10000 }); + fireEvent.click(screen.getByTestId('revalidate')); + await screen.findByText('success: true', {}, { timeout: 10000 }); + await screen.findByText( + `${testRunPrefix}-username-1`, + {}, + { 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 f5ac0fa1..576f0466 100644 --- a/packages/postgrest-swr/src/cache/use-delete-item.ts +++ b/packages/postgrest-swr/src/cache/use-delete-item.ts @@ -31,7 +31,7 @@ export function useDeleteItem>( cacheKeys: getMutableKeys(Array.from(cache.keys())), getPostgrestFilter, revalidate: (key) => { - mutate(key, { ...opts, revalidate: true }); + mutate(key, null, { ...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 e8bb83c4..085914c4 100644 --- a/packages/postgrest-swr/src/cache/use-mutate-item.ts +++ b/packages/postgrest-swr/src/cache/use-mutate-item.ts @@ -33,7 +33,7 @@ export function useMutateItem>( cacheKeys: getMutableKeys(Array.from(cache.keys())), getPostgrestFilter, revalidate: (key) => { - mutate(key, { ...opts, revalidate: true }); + mutate(key, null, { ...opts, revalidate: true }); }, mutate: (key, data) => { mutate(key, data, { diff --git a/packages/postgrest-swr/src/cache/use-upsert-item.ts b/packages/postgrest-swr/src/cache/use-upsert-item.ts index 74cda6c8..318740a0 100644 --- a/packages/postgrest-swr/src/cache/use-upsert-item.ts +++ b/packages/postgrest-swr/src/cache/use-upsert-item.ts @@ -31,7 +31,7 @@ export function useUpsertItem>( cacheKeys: getMutableKeys(Array.from(cache.keys())), getPostgrestFilter, revalidate: (key) => { - mutate(key, { ...opts, revalidate: true }); + mutate(key, null, { ...opts, revalidate: true }); }, mutate: (key, data) => { mutate(key, data, {