@zenstackhq/tanstack-query 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/dist/common/client.d.ts +4 -0
  2. package/dist/common/client.js +38 -0
  3. package/dist/common/client.js.map +1 -0
  4. package/dist/common/query-key.d.ts +39 -0
  5. package/dist/common/query-key.js +38 -0
  6. package/dist/common/query-key.js.map +1 -0
  7. package/dist/common/types.d.ts +60 -0
  8. package/dist/common/types.js +2 -0
  9. package/dist/common/types.js.map +1 -0
  10. package/dist/react.d.ts +99 -150
  11. package/dist/react.js +248 -1178
  12. package/dist/react.js.map +1 -1
  13. package/dist/svelte/index.svelte.d.ts +79 -0
  14. package/dist/svelte/index.svelte.js +245 -0
  15. package/dist/vue.d.ts +313 -342
  16. package/dist/vue.js +224 -1138
  17. package/dist/vue.js.map +1 -1
  18. package/package.json +36 -48
  19. package/.turbo/turbo-build.log +0 -47
  20. package/dist/react.cjs +0 -1240
  21. package/dist/react.cjs.map +0 -1
  22. package/dist/react.d.cts +0 -616
  23. package/dist/svelte.cjs +0 -1224
  24. package/dist/svelte.cjs.map +0 -1
  25. package/dist/svelte.d.cts +0 -381
  26. package/dist/svelte.d.ts +0 -381
  27. package/dist/svelte.js +0 -1183
  28. package/dist/svelte.js.map +0 -1
  29. package/dist/types-C8iIZD-7.d.cts +0 -99
  30. package/dist/types-C8iIZD-7.d.ts +0 -99
  31. package/dist/vue.cjs +0 -1192
  32. package/dist/vue.cjs.map +0 -1
  33. package/dist/vue.d.cts +0 -382
  34. package/eslint.config.js +0 -4
  35. package/src/react.ts +0 -562
  36. package/src/svelte.ts +0 -502
  37. package/src/utils/common.ts +0 -448
  38. package/src/utils/mutator.ts +0 -441
  39. package/src/utils/nested-read-visitor.ts +0 -61
  40. package/src/utils/nested-write-visitor.ts +0 -359
  41. package/src/utils/query-analysis.ts +0 -116
  42. package/src/utils/serialization.ts +0 -39
  43. package/src/utils/types.ts +0 -43
  44. package/src/vue.ts +0 -448
  45. package/test/react-query.test.tsx +0 -1787
  46. package/test/react-typing-test.ts +0 -113
  47. package/test/schemas/basic/input.ts +0 -110
  48. package/test/schemas/basic/models.ts +0 -14
  49. package/test/schemas/basic/schema-lite.ts +0 -172
  50. package/test/schemas/basic/schema.zmodel +0 -35
  51. package/test/svelte-typing-test.ts +0 -111
  52. package/test/vue-typing-test.ts +0 -111
  53. package/tsconfig.json +0 -7
  54. package/tsconfig.test.json +0 -8
  55. package/tsup.config.ts +0 -15
  56. package/vitest.config.ts +0 -11
package/src/react.ts DELETED
@@ -1,562 +0,0 @@
1
- import {
2
- useInfiniteQuery,
3
- useMutation,
4
- useQuery,
5
- useQueryClient,
6
- useSuspenseInfiniteQuery,
7
- useSuspenseQuery,
8
- type DefaultError,
9
- type InfiniteData,
10
- type QueryKey,
11
- type UseInfiniteQueryOptions,
12
- type UseInfiniteQueryResult,
13
- type UseMutationOptions,
14
- type UseMutationResult,
15
- type UseQueryOptions,
16
- type UseQueryResult,
17
- type UseSuspenseInfiniteQueryOptions,
18
- type UseSuspenseInfiniteQueryResult,
19
- type UseSuspenseQueryOptions,
20
- type UseSuspenseQueryResult,
21
- } from '@tanstack/react-query';
22
- import { lowerCaseFirst } from '@zenstackhq/common-helpers';
23
- import type {
24
- AggregateArgs,
25
- AggregateResult,
26
- BatchResult,
27
- CountArgs,
28
- CountResult,
29
- CreateArgs,
30
- CreateManyAndReturnArgs,
31
- CreateManyArgs,
32
- DeleteArgs,
33
- DeleteManyArgs,
34
- FindFirstArgs,
35
- FindManyArgs,
36
- FindUniqueArgs,
37
- GroupByArgs,
38
- GroupByResult,
39
- QueryOptions,
40
- SelectSubset,
41
- SimplifiedPlainResult,
42
- SimplifiedResult,
43
- Subset,
44
- UpdateArgs,
45
- UpdateManyAndReturnArgs,
46
- UpdateManyArgs,
47
- UpsertArgs,
48
- } from '@zenstackhq/orm';
49
- import type { GetModels, SchemaDef } from '@zenstackhq/schema';
50
- import { createContext, useContext } from 'react';
51
- import {
52
- fetcher,
53
- getQueryKey,
54
- makeUrl,
55
- marshal,
56
- setupInvalidation,
57
- setupOptimisticUpdate,
58
- type APIContext,
59
- type ExtraMutationOptions,
60
- type ExtraQueryOptions,
61
- } from './utils/common';
62
- import type { TrimDelegateModelOperations, WithOptimistic } from './utils/types';
63
-
64
- export type { FetchFn } from './utils/common';
65
-
66
- /**
67
- * The default query endpoint.
68
- */
69
- export const DEFAULT_QUERY_ENDPOINT = '/api/model';
70
-
71
- /**
72
- * React context for query settings.
73
- */
74
- export const QuerySettingsContext = createContext<APIContext>({
75
- endpoint: DEFAULT_QUERY_ENDPOINT,
76
- fetch: undefined,
77
- });
78
-
79
- /**
80
- * React context provider for configuring query settings.
81
- */
82
- export const QuerySettingsProvider = QuerySettingsContext.Provider;
83
-
84
- /**
85
- * React context provider for configuring query settings.
86
- *
87
- * @deprecated Use {@link QuerySettingsProvider} instead.
88
- */
89
- export const Provider = QuerySettingsProvider;
90
-
91
- function useHooksContext() {
92
- const { endpoint, ...rest } = useContext(QuerySettingsContext);
93
- return { endpoint: endpoint ?? DEFAULT_QUERY_ENDPOINT, ...rest };
94
- }
95
-
96
- export type ModelQueryOptions<T> = Omit<UseQueryOptions<T, DefaultError>, 'queryKey'> & ExtraQueryOptions;
97
-
98
- export type ModelQueryResult<T> = UseQueryResult<WithOptimistic<T>, DefaultError> & { queryKey: QueryKey };
99
-
100
- export type ModelSuspenseQueryOptions<T> = Omit<UseSuspenseQueryOptions<T, DefaultError>, 'queryKey'> &
101
- ExtraQueryOptions;
102
-
103
- export type ModelSuspenseQueryResult<T> = UseSuspenseQueryResult<WithOptimistic<T>, DefaultError> & {
104
- queryKey: QueryKey;
105
- };
106
-
107
- export type ModelInfiniteQueryOptions<T> = Omit<
108
- UseInfiniteQueryOptions<T, DefaultError, InfiniteData<T>>,
109
- 'queryKey' | 'initialPageParam'
110
- >;
111
-
112
- export type ModelInfiniteQueryResult<T> = UseInfiniteQueryResult<T, DefaultError> & { queryKey: QueryKey };
113
-
114
- export type ModelSuspenseInfiniteQueryOptions<T> = Omit<
115
- UseSuspenseInfiniteQueryOptions<T, DefaultError, InfiniteData<T>>,
116
- 'queryKey' | 'initialPageParam'
117
- >;
118
-
119
- export type ModelSuspenseInfiniteQueryResult<T> = UseSuspenseInfiniteQueryResult<T, DefaultError> & {
120
- queryKey: QueryKey;
121
- };
122
-
123
- export type ModelMutationOptions<T, TArgs> = Omit<UseMutationOptions<T, DefaultError, TArgs>, 'mutationFn'> &
124
- ExtraMutationOptions;
125
-
126
- export type ModelMutationResult<T, TArgs> = UseMutationResult<T, DefaultError, TArgs>;
127
-
128
- export type ModelMutationModelResult<
129
- Schema extends SchemaDef,
130
- Model extends GetModels<Schema>,
131
- TArgs,
132
- Array extends boolean = false,
133
- Options extends QueryOptions<Schema> = QueryOptions<Schema>,
134
- > = Omit<ModelMutationResult<SimplifiedResult<Schema, Model, TArgs, Options, false, Array>, TArgs>, 'mutateAsync'> & {
135
- mutateAsync<T extends TArgs>(
136
- args: T,
137
- options?: ModelMutationOptions<SimplifiedResult<Schema, Model, T, Options, false, Array>, T>,
138
- ): Promise<SimplifiedResult<Schema, Model, T, Options, false, Array>>;
139
- };
140
-
141
- export type ClientHooks<Schema extends SchemaDef, Options extends QueryOptions<Schema> = QueryOptions<Schema>> = {
142
- [Model in GetModels<Schema> as `${Uncapitalize<Model>}`]: ModelQueryHooks<Schema, Model, Options>;
143
- };
144
-
145
- // Note that we can potentially use TypeScript's mapped type to directly map from ORM contract, but that seems
146
- // to significantly slow down tsc performance ...
147
- export type ModelQueryHooks<
148
- Schema extends SchemaDef,
149
- Model extends GetModels<Schema>,
150
- Options extends QueryOptions<Schema> = QueryOptions<Schema>,
151
- > = TrimDelegateModelOperations<
152
- Schema,
153
- Model,
154
- {
155
- useFindUnique<T extends FindUniqueArgs<Schema, Model>>(
156
- args: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
157
- options?: ModelQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options> | null>,
158
- ): ModelQueryResult<SimplifiedPlainResult<Schema, Model, T, Options> | null>;
159
-
160
- useSuspenseFindUnique<T extends FindUniqueArgs<Schema, Model>>(
161
- args: SelectSubset<T, FindUniqueArgs<Schema, Model>>,
162
- options?: ModelSuspenseQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options> | null>,
163
- ): ModelSuspenseQueryResult<SimplifiedPlainResult<Schema, Model, T, Options> | null>;
164
-
165
- useFindFirst<T extends FindFirstArgs<Schema, Model>>(
166
- args?: SelectSubset<T, FindFirstArgs<Schema, Model>>,
167
- options?: ModelQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options> | null>,
168
- ): ModelQueryResult<SimplifiedPlainResult<Schema, Model, T, Options> | null>;
169
-
170
- useSuspenseFindFirst<T extends FindFirstArgs<Schema, Model>>(
171
- args?: SelectSubset<T, FindFirstArgs<Schema, Model>>,
172
- options?: ModelSuspenseQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options> | null>,
173
- ): ModelSuspenseQueryResult<SimplifiedPlainResult<Schema, Model, T, Options> | null>;
174
-
175
- useFindMany<T extends FindManyArgs<Schema, Model>>(
176
- args?: SelectSubset<T, FindManyArgs<Schema, Model>>,
177
- options?: ModelQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options>[]>,
178
- ): ModelQueryResult<SimplifiedPlainResult<Schema, Model, T, Options>[]>;
179
-
180
- useSuspenseFindMany<T extends FindManyArgs<Schema, Model>>(
181
- args?: SelectSubset<T, FindManyArgs<Schema, Model>>,
182
- options?: ModelSuspenseQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options>[]>,
183
- ): ModelSuspenseQueryResult<SimplifiedPlainResult<Schema, Model, T, Options>[]>;
184
-
185
- useInfiniteFindMany<T extends FindManyArgs<Schema, Model>>(
186
- args?: SelectSubset<T, FindManyArgs<Schema, Model>>,
187
- options?: ModelInfiniteQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options>[]>,
188
- ): ModelInfiniteQueryResult<InfiniteData<SimplifiedPlainResult<Schema, Model, T, Options>[]>>;
189
-
190
- useSuspenseInfiniteFindMany<T extends FindManyArgs<Schema, Model>>(
191
- args?: SelectSubset<T, FindManyArgs<Schema, Model>>,
192
- options?: ModelSuspenseInfiniteQueryOptions<SimplifiedPlainResult<Schema, Model, T, Options>[]>,
193
- ): ModelSuspenseInfiniteQueryResult<InfiniteData<SimplifiedPlainResult<Schema, Model, T, Options>[]>>;
194
-
195
- useCreate<T extends CreateArgs<Schema, Model>>(
196
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
197
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
198
-
199
- useCreateMany<T extends CreateManyArgs<Schema, Model>>(
200
- options?: ModelMutationOptions<BatchResult, T>,
201
- ): ModelMutationResult<BatchResult, T>;
202
-
203
- useCreateManyAndReturn<T extends CreateManyAndReturnArgs<Schema, Model>>(
204
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>[], T>,
205
- ): ModelMutationModelResult<Schema, Model, T, true, Options>;
206
-
207
- useUpdate<T extends UpdateArgs<Schema, Model>>(
208
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
209
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
210
-
211
- useUpdateMany<T extends UpdateManyArgs<Schema, Model>>(
212
- options?: ModelMutationOptions<BatchResult, T>,
213
- ): ModelMutationResult<BatchResult, T>;
214
-
215
- useUpdateManyAndReturn<T extends UpdateManyAndReturnArgs<Schema, Model>>(
216
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>[], T>,
217
- ): ModelMutationModelResult<Schema, Model, T, true, Options>;
218
-
219
- useUpsert<T extends UpsertArgs<Schema, Model>>(
220
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
221
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
222
-
223
- useDelete<T extends DeleteArgs<Schema, Model>>(
224
- options?: ModelMutationOptions<SimplifiedPlainResult<Schema, Model, T, Options>, T>,
225
- ): ModelMutationModelResult<Schema, Model, T, false, Options>;
226
-
227
- useDeleteMany<T extends DeleteManyArgs<Schema, Model>>(
228
- options?: ModelMutationOptions<BatchResult, T>,
229
- ): ModelMutationResult<BatchResult, T>;
230
-
231
- useCount<T extends CountArgs<Schema, Model>>(
232
- args?: Subset<T, CountArgs<Schema, Model>>,
233
- options?: ModelQueryOptions<CountResult<Schema, Model, T>>,
234
- ): ModelQueryResult<CountResult<Schema, Model, T>>;
235
-
236
- useSuspenseCount<T extends CountArgs<Schema, Model>>(
237
- args?: Subset<T, CountArgs<Schema, Model>>,
238
- options?: ModelSuspenseQueryOptions<CountResult<Schema, Model, T>>,
239
- ): ModelSuspenseQueryResult<CountResult<Schema, Model, T>>;
240
-
241
- useAggregate<T extends AggregateArgs<Schema, Model>>(
242
- args: Subset<T, AggregateArgs<Schema, Model>>,
243
- options?: ModelQueryOptions<AggregateResult<Schema, Model, T>>,
244
- ): ModelQueryResult<AggregateResult<Schema, Model, T>>;
245
-
246
- useSuspenseAggregate<T extends AggregateArgs<Schema, Model>>(
247
- args: Subset<T, AggregateArgs<Schema, Model>>,
248
- options?: ModelSuspenseQueryOptions<AggregateResult<Schema, Model, T>>,
249
- ): ModelSuspenseQueryResult<AggregateResult<Schema, Model, T>>;
250
-
251
- useGroupBy<T extends GroupByArgs<Schema, Model>>(
252
- args: Subset<T, GroupByArgs<Schema, Model>>,
253
- options?: ModelQueryOptions<GroupByResult<Schema, Model, T>>,
254
- ): ModelQueryResult<GroupByResult<Schema, Model, T>>;
255
-
256
- useSuspenseGroupBy<T extends GroupByArgs<Schema, Model>>(
257
- args: Subset<T, GroupByArgs<Schema, Model>>,
258
- options?: ModelSuspenseQueryOptions<GroupByResult<Schema, Model, T>>,
259
- ): ModelSuspenseQueryResult<GroupByResult<Schema, Model, T>>;
260
- }
261
- >;
262
-
263
- /**
264
- * Gets data query hooks for all models in the schema.
265
- */
266
- export function useClientQueries<Schema extends SchemaDef, Options extends QueryOptions<Schema> = QueryOptions<Schema>>(
267
- schema: Schema,
268
- ): ClientHooks<Schema, Options> {
269
- return Object.keys(schema.models).reduce(
270
- (acc, model) => {
271
- (acc as any)[lowerCaseFirst(model)] = useModelQueries<Schema, GetModels<Schema>, Options>(
272
- schema,
273
- model as GetModels<Schema>,
274
- );
275
- return acc;
276
- },
277
- {} as ClientHooks<Schema, Options>,
278
- );
279
- }
280
-
281
- /**
282
- * Gets data query hooks for a specific model in the schema.
283
- */
284
- export function useModelQueries<
285
- Schema extends SchemaDef,
286
- Model extends GetModels<Schema>,
287
- Options extends QueryOptions<Schema>,
288
- >(schema: Schema, model: Model): ModelQueryHooks<Schema, Model, Options> {
289
- const modelDef = Object.values(schema.models).find((m) => m.name.toLowerCase() === model.toLowerCase());
290
- if (!modelDef) {
291
- throw new Error(`Model "${model}" not found in schema`);
292
- }
293
-
294
- const modelName = modelDef.name;
295
-
296
- return {
297
- useFindUnique: (args: any, options?: any) => {
298
- return useInternalQuery(schema, modelName, 'findUnique', args, options);
299
- },
300
-
301
- useSuspenseFindUnique: (args: any, options?: any) => {
302
- return useInternalSuspenseQuery(schema, modelName, 'findUnique', args, options);
303
- },
304
-
305
- useFindFirst: (args: any, options?: any) => {
306
- return useInternalQuery(schema, modelName, 'findFirst', args, options);
307
- },
308
-
309
- useSuspenseFindFirst: (args: any, options?: any) => {
310
- return useInternalSuspenseQuery(schema, modelName, 'findFirst', args, options);
311
- },
312
-
313
- useFindMany: (args: any, options?: any) => {
314
- return useInternalQuery(schema, modelName, 'findMany', args, options);
315
- },
316
-
317
- useSuspenseFindMany: (args: any, options?: any) => {
318
- return useInternalSuspenseQuery(schema, modelName, 'findMany', args, options);
319
- },
320
-
321
- useInfiniteFindMany: (args: any, options?: any) => {
322
- return useInternalInfiniteQuery(schema, modelName, 'findMany', args, options);
323
- },
324
-
325
- useSuspenseInfiniteFindMany: (args: any, options?: any) => {
326
- return useInternalSuspenseInfiniteQuery(schema, modelName, 'findMany', args, options);
327
- },
328
-
329
- useCreate: (options?: any) => {
330
- return useInternalMutation(schema, modelName, 'POST', 'create', options);
331
- },
332
-
333
- useCreateMany: (options?: any) => {
334
- return useInternalMutation(schema, modelName, 'POST', 'createMany', options);
335
- },
336
-
337
- useCreateManyAndReturn: (options?: any) => {
338
- return useInternalMutation(schema, modelName, 'POST', 'createManyAndReturn', options);
339
- },
340
-
341
- useUpdate: (options?: any) => {
342
- return useInternalMutation(schema, modelName, 'PUT', 'update', options);
343
- },
344
-
345
- useUpdateMany: (options?: any) => {
346
- return useInternalMutation(schema, modelName, 'PUT', 'updateMany', options);
347
- },
348
-
349
- useUpdateManyAndReturn: (options?: any) => {
350
- return useInternalMutation(schema, modelName, 'PUT', 'updateManyAndReturn', options);
351
- },
352
-
353
- useUpsert: (options?: any) => {
354
- return useInternalMutation(schema, modelName, 'POST', 'upsert', options);
355
- },
356
-
357
- useDelete: (options?: any) => {
358
- return useInternalMutation(schema, modelName, 'DELETE', 'delete', options);
359
- },
360
-
361
- useDeleteMany: (options?: any) => {
362
- return useInternalMutation(schema, modelName, 'DELETE', 'deleteMany', options);
363
- },
364
-
365
- useCount: (args: any, options?: any) => {
366
- return useInternalQuery(schema, modelName, 'count', args, options);
367
- },
368
-
369
- useSuspenseCount: (args: any, options?: any) => {
370
- return useInternalSuspenseQuery(schema, modelName, 'count', args, options);
371
- },
372
-
373
- useAggregate: (args: any, options?: any) => {
374
- return useInternalQuery(schema, modelName, 'aggregate', args, options);
375
- },
376
-
377
- useSuspenseAggregate: (args: any, options?: any) => {
378
- return useInternalSuspenseQuery(schema, modelName, 'aggregate', args, options);
379
- },
380
-
381
- useGroupBy: (args: any, options?: any) => {
382
- return useInternalQuery(schema, modelName, 'groupBy', args, options);
383
- },
384
-
385
- useSuspenseGroupBy: (args: any, options?: any) => {
386
- return useInternalSuspenseQuery(schema, modelName, 'groupBy', args, options);
387
- },
388
- } as ModelQueryHooks<Schema, Model, Options>;
389
- }
390
-
391
- export function useInternalQuery<TQueryFnData, TData>(
392
- _schema: SchemaDef,
393
- model: string,
394
- operation: string,
395
- args?: unknown,
396
- options?: Omit<UseQueryOptions<TQueryFnData, DefaultError, TData>, 'queryKey'> & ExtraQueryOptions,
397
- ) {
398
- const { endpoint, fetch } = useHooksContext();
399
- const reqUrl = makeUrl(endpoint, model, operation, args);
400
- const queryKey = getQueryKey(model, operation, args, {
401
- infinite: false,
402
- optimisticUpdate: options?.optimisticUpdate !== false,
403
- });
404
- return {
405
- queryKey,
406
- ...useQuery({
407
- queryKey,
408
- queryFn: ({ signal }) => fetcher<TQueryFnData>(reqUrl, { signal }, fetch),
409
- ...options,
410
- }),
411
- };
412
- }
413
-
414
- export function useInternalSuspenseQuery<TQueryFnData, TData>(
415
- _schema: SchemaDef,
416
- model: string,
417
- operation: string,
418
- args?: unknown,
419
- options?: Omit<UseSuspenseQueryOptions<TQueryFnData, DefaultError, TData>, 'queryKey'> & ExtraQueryOptions,
420
- ) {
421
- const { endpoint, fetch } = useHooksContext();
422
- const reqUrl = makeUrl(endpoint, model, operation, args);
423
- const queryKey = getQueryKey(model, operation, args, {
424
- infinite: false,
425
- optimisticUpdate: options?.optimisticUpdate !== false,
426
- });
427
- return {
428
- queryKey,
429
- ...useSuspenseQuery({
430
- queryKey,
431
- queryFn: ({ signal }) => fetcher<TQueryFnData>(reqUrl, { signal }, fetch),
432
- ...options,
433
- }),
434
- };
435
- }
436
-
437
- export function useInternalInfiniteQuery<TQueryFnData, TData>(
438
- _schema: SchemaDef,
439
- model: string,
440
- operation: string,
441
- args: unknown,
442
- options:
443
- | Omit<
444
- UseInfiniteQueryOptions<TQueryFnData, DefaultError, InfiniteData<TData>>,
445
- 'queryKey' | 'initialPageParam'
446
- >
447
- | undefined,
448
- ) {
449
- options = options ?? { getNextPageParam: () => undefined };
450
- const { endpoint, fetch } = useHooksContext();
451
- const queryKey = getQueryKey(model, operation, args, { infinite: true, optimisticUpdate: false });
452
- return {
453
- queryKey,
454
- ...useInfiniteQuery({
455
- queryKey,
456
- queryFn: ({ pageParam, signal }) => {
457
- return fetcher<TQueryFnData>(makeUrl(endpoint, model, operation, pageParam ?? args), { signal }, fetch);
458
- },
459
- initialPageParam: args,
460
- ...options,
461
- }),
462
- };
463
- }
464
-
465
- export function useInternalSuspenseInfiniteQuery<TQueryFnData, TData>(
466
- _schema: SchemaDef,
467
- model: string,
468
- operation: string,
469
- args: unknown,
470
- options: Omit<
471
- UseSuspenseInfiniteQueryOptions<TQueryFnData, DefaultError, InfiniteData<TData>>,
472
- 'queryKey' | 'initialPageParam'
473
- >,
474
- ) {
475
- const { endpoint, fetch } = useHooksContext();
476
- const queryKey = getQueryKey(model, operation, args, { infinite: true, optimisticUpdate: false });
477
- return {
478
- queryKey,
479
- ...useSuspenseInfiniteQuery({
480
- queryKey,
481
- queryFn: ({ pageParam, signal }) => {
482
- return fetcher<TQueryFnData>(makeUrl(endpoint, model, operation, pageParam ?? args), { signal }, fetch);
483
- },
484
- initialPageParam: args,
485
- ...options,
486
- }),
487
- };
488
- }
489
-
490
- /**
491
- * Creates a react-query mutation
492
- *
493
- * @private
494
- *
495
- * @param model The name of the model under mutation.
496
- * @param method The HTTP method.
497
- * @param operation The mutation operation (e.g. `create`).
498
- * @param options The react-query options.
499
- * @param checkReadBack Whether to check for read back errors and return undefined if found.
500
- */
501
- export function useInternalMutation<TArgs, R = any>(
502
- schema: SchemaDef,
503
- model: string,
504
- method: 'POST' | 'PUT' | 'DELETE',
505
- operation: string,
506
- options?: Omit<UseMutationOptions<R, DefaultError, TArgs>, 'mutationFn'> & ExtraMutationOptions,
507
- ) {
508
- const { endpoint, fetch, logging } = useHooksContext();
509
- const queryClient = useQueryClient();
510
- const mutationFn = (data: any) => {
511
- const reqUrl =
512
- method === 'DELETE' ? makeUrl(endpoint, model, operation, data) : makeUrl(endpoint, model, operation);
513
- const fetchInit: RequestInit = {
514
- method,
515
- ...(method !== 'DELETE' && {
516
- headers: {
517
- 'content-type': 'application/json',
518
- },
519
- body: marshal(data),
520
- }),
521
- };
522
- return fetcher<R>(reqUrl, fetchInit, fetch) as Promise<R>;
523
- };
524
-
525
- const finalOptions = { ...options, mutationFn };
526
- const invalidateQueries = options?.invalidateQueries !== false;
527
- const optimisticUpdate = !!options?.optimisticUpdate;
528
-
529
- if (operation) {
530
- if (invalidateQueries) {
531
- setupInvalidation(
532
- model,
533
- operation,
534
- schema,
535
- finalOptions,
536
- (predicate) => queryClient.invalidateQueries({ predicate }),
537
- logging,
538
- );
539
- }
540
-
541
- if (optimisticUpdate) {
542
- setupOptimisticUpdate(
543
- model,
544
- operation,
545
- schema,
546
- finalOptions,
547
- queryClient.getQueryCache().getAll(),
548
- (queryKey, data) => {
549
- // update query cache
550
- queryClient.setQueryData<unknown>(queryKey, data);
551
- // cancel on-flight queries to avoid redundant cache updates,
552
- // the settlement of the current mutation will trigger a new revalidation
553
- queryClient.cancelQueries({ queryKey }, { revert: false, silent: true });
554
- },
555
- invalidateQueries ? (predicate) => queryClient.invalidateQueries({ predicate }) : undefined,
556
- logging,
557
- );
558
- }
559
- }
560
-
561
- return useMutation(finalOptions);
562
- }