Skip to content

Commit

Permalink
Changed how default query client is derived when not supplied as a fu…
Browse files Browse the repository at this point in the history
…nction parameter.
  • Loading branch information
Chriztiaan committed Dec 9, 2024
1 parent 53fd64e commit ead8e11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-grapes-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/tanstack-react-query': minor
---

Changed how default query client is derived when not supplied as a function parameter. Fixes some cases where deriving the query client happens too early.
16 changes: 8 additions & 8 deletions packages/tanstack-react-query/src/hooks/useQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function useQuery<TData = unknown, TError = Tanstack.DefaultError>(

export function useQuery<TData = unknown, TError = Tanstack.DefaultError>(
options: UseBaseQueryOptions<Tanstack.UseQueryOptions<TData, TError>>,
queryClient: Tanstack.QueryClient = Tanstack.useQueryClient()
queryClient?: Tanstack.QueryClient
) {
return useQueryCore(options, queryClient, Tanstack.useQuery);
}
Expand All @@ -44,7 +44,7 @@ export function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError

export function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError>(
options: UseBaseQueryOptions<Tanstack.UseSuspenseQueryOptions<TData, TError>>,
queryClient: Tanstack.QueryClient = Tanstack.useQueryClient()
queryClient?: Tanstack.QueryClient
) {
return useQueryCore(options, queryClient, Tanstack.useSuspenseQuery);
}
Expand All @@ -56,14 +56,14 @@ function useQueryCore<
TQueryResult extends Tanstack.UseQueryResult<TData, TError> | Tanstack.UseSuspenseQueryResult<TData, TError>
>(
options: UseBaseQueryOptions<TQueryOptions>,
queryClient: Tanstack.QueryClient,
queryClient: Tanstack.QueryClient | undefined,
useQueryFn: (options: TQueryOptions, queryClient?: Tanstack.QueryClient) => TQueryResult
): TQueryResult {
const powerSync = usePowerSync();

if (!powerSync) {
throw new Error('PowerSync is not available');
}
const resolvedQueryClient = queryClient ?? Tanstack.useQueryClient();

let error: Error | undefined = undefined;

Expand Down Expand Up @@ -106,7 +106,7 @@ function useQueryCore<
const l = powerSync.registerListener({
schemaChanged: async () => {
await fetchTables();
queryClient.invalidateQueries({ queryKey: options.queryKey });
resolvedQueryClient.invalidateQueries({ queryKey: options.queryKey });
}
});

Expand All @@ -132,7 +132,7 @@ function useQueryCore<
powerSync.onChangeWithCallback(
{
onChange: () => {
queryClient.invalidateQueries({
resolvedQueryClient.invalidateQueries({
queryKey: options.queryKey
});
},
Expand All @@ -146,13 +146,13 @@ function useQueryCore<
}
);
return () => abort.abort();
}, [powerSync, queryClient, stringifiedKey, tables]);
}, [powerSync, resolvedQueryClient, stringifiedKey, tables]);

return useQueryFn(
{
...(resolvedOptions as TQueryOptions),
queryFn: query ? queryFn : resolvedOptions.queryFn
} as TQueryOptions,
queryClient
resolvedQueryClient
);
}

0 comments on commit ead8e11

Please sign in to comment.