Skip to content

Commit 9bbac1d

Browse files
committed
fixing-import-fixing-reactivity
1 parent 17ad113 commit 9bbac1d

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

packages/plugins/tanstack-query/src/generator.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ function generateQueryHook(
117117
const capOperation = upperCaseFirst(operation);
118118

119119
const argsType = overrideInputType ?? `Prisma.${model}${capOperation}Args`;
120-
const inputType = makeQueryArgsType(target, argsType);
120+
let inputType = makeQueryArgsType(target, argsType);
121+
if (target === 'angular') {
122+
inputType = `${inputType} | (() => ${inputType})`;
123+
}
121124

122125
const infinite = generateMode.includes('Infinite');
123126
const suspense = generateMode.includes('Suspense');
@@ -673,7 +676,7 @@ function makeBaseImports(target: TargetFramework, version: TanStackVersion) {
673676
}
674677
case 'angular': {
675678
return [
676-
`import type { CreateMutationOptions, CreateQueryOptions, CreateInfiniteQueryOptions, InfiniteData } from '@tanstack/angular-query-v5';`,
679+
`import type { CreateMutationOptions, CreateQueryOptions, CreateInfiniteQueryOptions, InfiniteData } from '@tanstack/angular-query-experimental';`,
677680
`import { getHooksContext } from '${runtimeImportBase}/${target}';`,
678681
...shared,
679682
];

packages/plugins/tanstack-query/src/runtime-v5/angular.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import {
88
type CreateMutationOptions,
99
type CreateInfiniteQueryOptions,
1010
type InfiniteData,
11-
CreateInfiniteQueryResult,
12-
QueryKey,
1311
} from '@tanstack/angular-query-v5';
1412
import type { ModelMeta } from '@zenstackhq/runtime/cross';
1513
import { inject, InjectionToken } from '@angular/core';
@@ -29,6 +27,9 @@ import {
2927

3028
export { APIContext as RequestHandlerContext } from '../runtime/common';
3129

30+
type AnyFn = (...a: unknown[]) => unknown;
31+
const isFn = (v: unknown): v is AnyFn => typeof v === 'function';
32+
3233
export const AngularQueryContextKey = new InjectionToken<APIContext>('zenstack-angular-query-context');
3334

3435
/**
@@ -74,19 +75,21 @@ export function useModelQuery<TQueryFnData, TData, TError>(
7475
options?: Omit<CreateQueryOptions<TQueryFnData, TError, TData>, 'queryKey'> & ExtraQueryOptions,
7576
fetch?: FetchFn
7677
) {
77-
const reqUrl = makeUrl(url, args);
78-
const queryKey = getQueryKey(model, url, args, {
79-
infinite: false,
80-
optimisticUpdate: options?.optimisticUpdate !== false,
81-
});
82-
return {
83-
queryKey,
84-
...injectQuery(() => ({
78+
const query = injectQuery(() => {
79+
const resolvedArgs = isFn(args) ? args() : args;
80+
81+
const reqUrl = makeUrl(url, resolvedArgs);
82+
const queryKey = getQueryKey(model, url, resolvedArgs, {
83+
infinite: false,
84+
optimisticUpdate: options?.optimisticUpdate !== false,
85+
});
86+
return {
8587
queryKey,
8688
queryFn: ({ signal }) => fetcher<TQueryFnData, false>(reqUrl, { signal }, fetch, false),
8789
...options,
88-
})),
89-
};
90+
};
91+
});
92+
return query;
9093
}
9194

9295
/**
@@ -108,19 +111,22 @@ export function useInfiniteModelQuery<TQueryFnData, TData, TError>(
108111
'queryKey' | 'initialPageParam'
109112
>,
110113
fetch?: FetchFn
111-
): CreateInfiniteQueryResult<InfiniteData<TData>, TError> & { queryKey: QueryKey } {
112-
const queryKey = getQueryKey(model, url, args, { infinite: true, optimisticUpdate: false });
113-
return {
114-
queryKey,
115-
...injectInfiniteQuery(() => ({
114+
) {
115+
const query = injectInfiniteQuery(() => {
116+
const resolvedArgs = isFn(args) ? args() : args;
117+
118+
const queryKey = getQueryKey(model, url, resolvedArgs, { infinite: true, optimisticUpdate: false });
119+
120+
return {
116121
queryKey,
117122
queryFn: ({ pageParam, signal }) => {
118-
return fetcher<TQueryFnData, false>(makeUrl(url, pageParam ?? args), { signal }, fetch, false);
123+
return fetcher<TQueryFnData, false>(makeUrl(url, pageParam ?? resolvedArgs), { signal }, fetch, false);
119124
},
120-
initialPageParam: args,
125+
initialPageParam: resolvedArgs,
121126
...options,
122-
})),
123-
};
127+
};
128+
});
129+
return query;
124130
}
125131

126132
/**

0 commit comments

Comments
 (0)