@@ -84,9 +84,9 @@ function generateQueryHook(
84
84
const capOperation = upperCaseFirst ( operation ) ;
85
85
86
86
const argsType = overrideInputType ?? `Prisma.${ model } ${ capOperation } Args` ;
87
- const inputType = `Prisma.SelectSubset<T , ${ argsType } >` ;
87
+ const inputType = `Prisma.SelectSubset<TArgs , ${ argsType } >` ;
88
88
89
- let defaultReturnType = `Prisma.${ model } GetPayload<T >` ;
89
+ let defaultReturnType = `Prisma.${ model } GetPayload<TArgs >` ;
90
90
if ( optimisticUpdate ) {
91
91
defaultReturnType += '& { $optimistic?: boolean }' ;
92
92
}
@@ -95,11 +95,16 @@ function generateQueryHook(
95
95
}
96
96
97
97
const returnType = overrideReturnType ?? defaultReturnType ;
98
- const optionsType = makeQueryOptions ( target , returnType , infinite , version ) ;
98
+ const optionsType = makeQueryOptions ( target , 'TQueryFnData' , 'TData' , infinite , version ) ;
99
99
100
100
const func = sf . addFunction ( {
101
101
name : `use${ infinite ? 'Infinite' : '' } ${ capOperation } ${ model } ` ,
102
- typeParameters : overrideTypeParameters ?? [ `T extends ${ argsType } ` ] ,
102
+ typeParameters : overrideTypeParameters ?? [
103
+ `TArgs extends ${ argsType } ` ,
104
+ `TQueryFnData = ${ returnType } ` ,
105
+ 'TData = TQueryFnData' ,
106
+ 'TError = DefaultError' ,
107
+ ] ,
103
108
parameters : [
104
109
{
105
110
name : optionalInput ? 'args?' : 'args' ,
@@ -129,7 +134,9 @@ function generateQueryHook(
129
134
130
135
func . addStatements ( [
131
136
makeGetContext ( target ) ,
132
- `return ${ infinite ? 'useInfiniteModelQuery' : 'useModelQuery' } ('${ model } ', \`\${endpoint}/${ lowerCaseFirst (
137
+ `return ${
138
+ infinite ? 'useInfiniteModelQuery' : 'useModelQuery'
139
+ } <TQueryFnData, TData, TError>('${ model } ', \`\${endpoint}/${ lowerCaseFirst (
133
140
model
134
141
) } /${ operation } \`, args, options, fetch${ optimisticUpdate ? ', optimisticUpdate' : '' } );`,
135
142
] ) ;
@@ -403,7 +410,7 @@ function generateModelHooks(
403
410
'aggregate' ,
404
411
false ,
405
412
false ,
406
- `Prisma.Get${ modelNameCap } AggregateType<T >`
413
+ `Prisma.Get${ modelNameCap } AggregateType<TArgs >`
407
414
) ;
408
415
}
409
416
@@ -415,16 +422,27 @@ function generateModelHooks(
415
422
useName = model . name ;
416
423
}
417
424
425
+ const returnType = `{} extends InputErrors ?
426
+ Array<PickEnumerable<Prisma.${ modelNameCap } GroupByOutputType, TArgs['by']> &
427
+ {
428
+ [P in ((keyof TArgs) & (keyof Prisma.${ modelNameCap } GroupByOutputType))]: P extends '_count'
429
+ ? TArgs[P] extends boolean
430
+ ? number
431
+ : Prisma.GetScalarType<TArgs[P], Prisma.${ modelNameCap } GroupByOutputType[P]>
432
+ : Prisma.GetScalarType<TArgs[P], Prisma.${ modelNameCap } GroupByOutputType[P]>
433
+ }
434
+ > : InputErrors` ;
435
+
418
436
const typeParameters = [
419
- `T extends Prisma.${ useName } GroupByArgs` ,
420
- `HasSelectOrTake extends Prisma.Or<Prisma.Extends<'skip', Prisma.Keys<T >>, Prisma.Extends<'take', Prisma.Keys<T >>>` ,
437
+ `TArgs extends Prisma.${ useName } GroupByArgs` ,
438
+ `HasSelectOrTake extends Prisma.Or<Prisma.Extends<'skip', Prisma.Keys<TArgs >>, Prisma.Extends<'take', Prisma.Keys<TArgs >>>` ,
421
439
`OrderByArg extends Prisma.True extends HasSelectOrTake ? { orderBy: Prisma.${ useName } GroupByArgs['orderBy'] }: { orderBy?: Prisma.${ useName } GroupByArgs['orderBy'] },` ,
422
- `OrderFields extends Prisma.ExcludeUnderscoreKeys<Prisma.Keys<Prisma.MaybeTupleToUnion<T ['orderBy']>>>` ,
423
- `ByFields extends Prisma.MaybeTupleToUnion<T ['by']>` ,
440
+ `OrderFields extends Prisma.ExcludeUnderscoreKeys<Prisma.Keys<Prisma.MaybeTupleToUnion<TArgs ['orderBy']>>>` ,
441
+ `ByFields extends Prisma.MaybeTupleToUnion<TArgs ['by']>` ,
424
442
`ByValid extends Prisma.Has<ByFields, OrderFields>` ,
425
- `HavingFields extends Prisma.GetHavingFields<T ['having']>` ,
443
+ `HavingFields extends Prisma.GetHavingFields<TArgs ['having']>` ,
426
444
`HavingValid extends Prisma.Has<ByFields, HavingFields>` ,
427
- `ByEmpty extends T ['by'] extends never[] ? Prisma.True : Prisma.False` ,
445
+ `ByEmpty extends TArgs ['by'] extends never[] ? Prisma.True : Prisma.False` ,
428
446
`InputErrors extends ByEmpty extends Prisma.True
429
447
? \`Error: "by" must not be empty.\`
430
448
: HavingValid extends Prisma.False
@@ -440,8 +458,8 @@ function generateModelHooks(
440
458
\` in "having" needs to be provided in "by"\`,
441
459
]
442
460
}[HavingFields]
443
- : 'take' extends Prisma.Keys<T >
444
- ? 'orderBy' extends Prisma.Keys<T >
461
+ : 'take' extends Prisma.Keys<TArgs >
462
+ ? 'orderBy' extends Prisma.Keys<TArgs >
445
463
? ByValid extends Prisma.True
446
464
? {}
447
465
: {
@@ -450,8 +468,8 @@ function generateModelHooks(
450
468
: \`Error: Field "\${P}" in "orderBy" needs to be provided in "by"\`
451
469
}[OrderFields]
452
470
: 'Error: If you provide "take", you also need to provide "orderBy"'
453
- : 'skip' extends Prisma.Keys<T >
454
- ? 'orderBy' extends Prisma.Keys<T >
471
+ : 'skip' extends Prisma.Keys<TArgs >
472
+ ? 'orderBy' extends Prisma.Keys<TArgs >
455
473
? ByValid extends Prisma.True
456
474
? {}
457
475
: {
@@ -467,19 +485,11 @@ function generateModelHooks(
467
485
? never
468
486
: \`Error: Field "\${P}" in "orderBy" needs to be provided in "by"\`
469
487
}[OrderFields]` ,
488
+ `TQueryFnData = ${ returnType } ` ,
489
+ `TData = TQueryFnData` ,
490
+ `TError = DefaultError` ,
470
491
] ;
471
492
472
- const returnType = `{} extends InputErrors ?
473
- Array<PickEnumerable<Prisma.${ modelNameCap } GroupByOutputType, T['by']> &
474
- {
475
- [P in ((keyof T) & (keyof Prisma.${ modelNameCap } GroupByOutputType))]: P extends '_count'
476
- ? T[P] extends boolean
477
- ? number
478
- : Prisma.GetScalarType<T[P], Prisma.${ modelNameCap } GroupByOutputType[P]>
479
- : Prisma.GetScalarType<T[P], Prisma.${ modelNameCap } GroupByOutputType[P]>
480
- }
481
- > : InputErrors` ;
482
-
483
493
generateQueryHook (
484
494
target ,
485
495
version ,
@@ -489,7 +499,7 @@ function generateModelHooks(
489
499
false ,
490
500
false ,
491
501
returnType ,
492
- `Prisma.SubsetIntersection<T , Prisma.${ useName } GroupByArgs, OrderByArg> & InputErrors` ,
502
+ `Prisma.SubsetIntersection<TArgs , Prisma.${ useName } GroupByArgs, OrderByArg> & InputErrors` ,
493
503
typeParameters
494
504
) ;
495
505
}
@@ -504,7 +514,7 @@ function generateModelHooks(
504
514
'count' ,
505
515
false ,
506
516
true ,
507
- `T extends { select: any; } ? T ['select'] extends true ? number : Prisma.GetScalarType<T ['select'], Prisma.${ modelNameCap } CountAggregateOutputType> : number`
517
+ `TArgs extends { select: any; } ? TArgs ['select'] extends true ? number : Prisma.GetScalarType<TArgs ['select'], Prisma.${ modelNameCap } CountAggregateOutputType> : number`
508
518
) ;
509
519
}
510
520
}
@@ -552,12 +562,13 @@ function makeBaseImports(target: TargetFramework, version: TanStackVersion) {
552
562
`import { useModelQuery, useInfiniteModelQuery, useModelMutation } from '${ runtimeImportBase } /${ target } ';` ,
553
563
`import type { PickEnumerable, CheckSelect } from '${ runtimeImportBase } ';` ,
554
564
`import metadata from './__model_meta';` ,
565
+ `type DefaultError = Error;` ,
555
566
] ;
556
567
switch ( target ) {
557
568
case 'react' :
558
569
return [
559
570
`import type { UseMutationOptions, UseQueryOptions, UseInfiniteQueryOptions, InfiniteData } from '@tanstack/react-query';` ,
560
- `import { RequestHandlerContext, getHooksContext } from '${ runtimeImportBase } /${ target } ';` ,
571
+ `import { getHooksContext } from '${ runtimeImportBase } /${ target } ';` ,
561
572
...shared ,
562
573
] ;
563
574
case 'vue' :
@@ -573,32 +584,38 @@ function makeBaseImports(target: TargetFramework, version: TanStackVersion) {
573
584
...( version === 'v5'
574
585
? [ `import type { InfiniteData, StoreOrVal } from '@tanstack/svelte-query';` ]
575
586
: [ ] ) ,
576
- `import { SvelteQueryContextKey, type RequestHandlerContext, getHooksContext } from '${ runtimeImportBase } /${ target } ';` ,
587
+ `import { getHooksContext } from '${ runtimeImportBase } /${ target } ';` ,
577
588
...shared ,
578
589
] ;
579
590
default :
580
591
throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
581
592
}
582
593
}
583
594
584
- function makeQueryOptions ( target : string , returnType : string , infinite : boolean , version : TanStackVersion ) {
595
+ function makeQueryOptions (
596
+ target : string ,
597
+ returnType : string ,
598
+ dataType : string ,
599
+ infinite : boolean ,
600
+ version : TanStackVersion
601
+ ) {
585
602
switch ( target ) {
586
603
case 'react' :
587
604
return infinite
588
605
? version === 'v4'
589
- ? `Omit<UseInfiniteQueryOptions<${ returnType } >, 'queryKey'>`
590
- : `Omit<UseInfiniteQueryOptions<${ returnType } , unknown , InfiniteData<${ returnType } >>, 'queryKey'>`
591
- : `Omit<UseQueryOptions<${ returnType } >, 'queryKey'>` ;
606
+ ? `Omit<UseInfiniteQueryOptions<${ returnType } , TError, ${ dataType } >, 'queryKey'>`
607
+ : `Omit<UseInfiniteQueryOptions<${ returnType } , TError , InfiniteData<${ dataType } >>, 'queryKey'>`
608
+ : `Omit<UseQueryOptions<${ returnType } , TError, ${ dataType } >, 'queryKey'>` ;
592
609
case 'vue' :
593
- return `Omit<Use${ infinite ? 'Infinite' : '' } QueryOptions<${ returnType } >, 'queryKey'>` ;
610
+ return `Omit<Use${ infinite ? 'Infinite' : '' } QueryOptions<${ returnType } , TError, ${ dataType } >, 'queryKey'>` ;
594
611
case 'svelte' :
595
612
return infinite
596
613
? version === 'v4'
597
- ? `Omit<CreateInfiniteQueryOptions<${ returnType } >, 'queryKey'>`
598
- : `StoreOrVal<Omit<CreateInfiniteQueryOptions<${ returnType } , unknown , InfiniteData<${ returnType } >>, 'queryKey'>>`
614
+ ? `Omit<CreateInfiniteQueryOptions<${ returnType } , TError, ${ dataType } >, 'queryKey'>`
615
+ : `StoreOrVal<Omit<CreateInfiniteQueryOptions<${ returnType } , TError , InfiniteData<${ dataType } >>, 'queryKey'>>`
599
616
: version === 'v4'
600
- ? `Omit<CreateQueryOptions<${ returnType } >, 'queryKey'>`
601
- : `StoreOrVal<Omit<CreateQueryOptions<${ returnType } >, 'queryKey'>>` ;
617
+ ? `Omit<CreateQueryOptions<${ returnType } , TError, ${ dataType } >, 'queryKey'>`
618
+ : `StoreOrVal<Omit<CreateQueryOptions<${ returnType } , TError, ${ dataType } >, 'queryKey'>>` ;
602
619
default :
603
620
throw new PluginError ( name , `Unsupported target: ${ target } ` ) ;
604
621
}
0 commit comments