Skip to content

Commit

Permalink
Fix server queries prefetch (#39)
Browse files Browse the repository at this point in the history
* fix: update query cache finding for server queries to use proper key

* bump version
  • Loading branch information
vishalbalaji authored May 14, 2024
1 parent 67b6d21 commit d9e7cd2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trpc-svelte-query-adapter",
"version": "2.3.1",
"version": "2.3.2",
"description": "A simple adapter to use `@tanstack/svelte-query` with trpc, similar to `@trpc/react-query`.",
"keywords": [
"trpc",
Expand Down
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type HasMutate = { mutate: (...args: any[]) => any }
type HasSubscribe = { subscribe: (...args: any[]) => any }
type OnlyQueries<TClient> = Without<TClient, HasMutate | HasSubscribe>


// createUtils
const UtilsProcedureNames = {
client: 'client',
Expand Down Expand Up @@ -157,6 +158,7 @@ type CreateQueries<TClient, TError> = <TOpts extends CreateQueryOptionsForCreate
queriesCallback: (t: CreateQueriesRecord<OnlyQueries<TClient>, TError>) => readonly [...TOpts]
) => CreateQueriesResult<TOpts>


// createServerQueries
type CreateQueryOptionsForCreateServerQueries<TOutput, TError, TData> = CreateQueryOptionsForCreateQueries<TOutput, TError, TData> & {
ssr?: boolean
Expand All @@ -174,6 +176,7 @@ type CreateServerQueries<TClient, TError> = <TOpts extends CreateQueryOptionsFor
queriesCallback: (t: CreateServerQueriesRecord<OnlyQueries<TClient>, TError>) => readonly [...TOpts]
) => Promise<(queriesCallback?: (t: CreateQueriesRecord<OnlyQueries<TClient>, TError>, old: readonly [...TOpts]) => readonly [...TOpts]) => CreateQueriesResult<TOpts>>


// Procedures
const ProcedureNames = {
query: 'createQuery',
Expand Down Expand Up @@ -260,6 +263,8 @@ type AddQueryPropTypes<TClient, TError> = TClient extends Record<any, any> ? {
: TClient[K] extends HasSubscribe ? CreateSubscriptionProcedure<Parameters<TClient[K]['subscribe']>[0], GetSubscriptionOutput<Parameters<TClient[K]['subscribe']>[1]>, TError>
: GetQueryKey & AddQueryPropTypes<TClient[K], TError>
} : TClient;


// Implementation
function createQueriesProxy(client: any) {
return new DeepProxy({}, {
Expand Down Expand Up @@ -592,7 +597,7 @@ const procedures: Record<PropertyKey,
return async (input: (...args: any[]) => any) => {
const queries = await Promise.all(
input(proxy).map(async (query: any) => {
const cache = queryClient.getQueryCache().find(query.queryKey);
const cache = queryClient.getQueryCache().find({ queryKey: query.queryKey });
const cacheNotFound = !cache?.state?.data;

if (query.ssr !== false && cacheNotFound) {
Expand Down Expand Up @@ -625,6 +630,7 @@ const procedures: Record<PropertyKey,
},
};


// getQueryKey
type QueryType = 'query' | 'infinite' | 'any';

Expand Down Expand Up @@ -669,7 +675,6 @@ type GetQueryKey<TInput = undefined> = [TInput] extends [undefined | void]
} & {}



export function svelteQueryWrapper<TRouter extends AnyRouter>({
client,
queryClient,
Expand Down

0 comments on commit d9e7cd2

Please sign in to comment.