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

fix: Tanstack not deriving default client #437

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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
);
}
Loading