@@ -48,6 +48,9 @@ function getPrismaOperationTypes(model: string, operation: string) {
4848
4949 let argsType : string ;
5050 let resultType : string ;
51+ const argsOptional = [ 'findMany' , 'findFirst' , 'findFirstOrThrow' , 'createMany' , 'deleteMany' , 'count' ] . includes (
52+ operation
53+ ) ;
5154
5255 switch ( operation ) {
5356 case 'findUnique' :
@@ -178,7 +181,7 @@ function getPrismaOperationTypes(model: string, operation: string) {
178181 throw new PluginError ( name , `Unsupported operation: "${ operation } "` ) ;
179182 }
180183
181- return { genericBase, argsType, resultType } ;
184+ return { genericBase, argsType, resultType, argsOptional } ;
182185}
183186
184187/**
@@ -192,22 +195,23 @@ export function generateRouterTyping(
192195 version : string
193196) {
194197 const procType = getProcedureTypeByOpName ( baseOpType ) ;
195- const { genericBase, argsType, resultType } = getPrismaOperationTypes ( modelName , opType ) ;
198+ const { genericBase, argsType, argsOptional , resultType } = getPrismaOperationTypes ( modelName , opType ) ;
196199 const errorType = `TRPCClientErrorLike<AppRouter>` ;
200+ const inputOptional = argsOptional ? '?' : '' ;
197201
198202 writer . block ( ( ) => {
199203 if ( procType === 'query' ) {
200204 if ( version === 'v10' ) {
201205 writer . writeLine ( `
202206 useQuery: <T extends ${ genericBase } , TData = ${ resultType } >(
203- input: ${ argsType } ,
207+ input${ inputOptional } : ${ argsType } ,
204208 opts?: UseTRPCQueryOptions<string, T, ${ resultType } , TData, Error>
205209 ) => UseTRPCQueryResult<
206210 TData,
207211 ${ errorType }
208212 >;
209213 useInfiniteQuery: <T extends ${ genericBase } >(
210- input: Omit<${ argsType } , 'cursor'>,
214+ input${ inputOptional } : Omit<${ argsType } , 'cursor'>,
211215 opts?: UseTRPCInfiniteQueryOptions<string, T, ${ resultType } , Error>
212216 ) => UseTRPCInfiniteQueryResult<
213217 ${ resultType } ,
@@ -217,26 +221,26 @@ export function generateRouterTyping(
217221 } else {
218222 writer . writeLine ( `
219223 useQuery: <T extends ${ genericBase } , TData = ${ resultType } >(
220- input: ${ argsType } ,
224+ input${ inputOptional } : ${ argsType } ,
221225 opts?: UseTRPCQueryOptions<${ resultType } , TData, Error>
222226 ) => UseTRPCQueryResult<
223227 TData,
224228 ${ errorType }
225229 >;
226230 useInfiniteQuery: <T extends ${ genericBase } >(
227- input: Omit<${ argsType } , 'cursor'>,
231+ input${ inputOptional } : Omit<${ argsType } , 'cursor'>,
228232 opts?: UseTRPCInfiniteQueryOptions<T, ${ resultType } , Error>
229233 ) => UseTRPCInfiniteQueryResult<
230234 ${ resultType } ,
231235 ${ errorType } ,
232236 T
233237 >;
234238 useSuspenseQuery: <T extends ${ genericBase } , TData = ${ resultType } >(
235- input: ${ argsType } ,
239+ input${ inputOptional } : ${ argsType } ,
236240 opts?: UseTRPCSuspenseQueryOptions<${ resultType } , TData, Error>
237241 ) => UseTRPCSuspenseQueryResult<TData, ${ errorType } >;
238242 useSuspenseInfiniteQuery: <T extends ${ genericBase } >(
239- input: Omit<${ argsType } , 'cursor'>,
243+ input${ inputOptional } : Omit<${ argsType } , 'cursor'>,
240244 opts?: UseTRPCSuspenseInfiniteQueryOptions<T, ${ resultType } , Error>
241245 ) => UseTRPCSuspenseInfiniteQueryResult<${ resultType } , ${ errorType } , T>;
242246 ` ) ;
@@ -298,10 +302,10 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
298302 inputType = `$Schema.${ capModelName } InputSchema.findUnique` ;
299303 break ;
300304 case 'findFirst' :
301- inputType = `$Schema.${ capModelName } InputSchema.findFirst` ;
305+ inputType = `$Schema.${ capModelName } InputSchema.findFirst.optional() ` ;
302306 break ;
303307 case 'findMany' :
304- inputType = `$Schema.${ capModelName } InputSchema.findMany` ;
308+ inputType = `$Schema.${ capModelName } InputSchema.findMany.optional() ` ;
305309 break ;
306310 case 'findRaw' :
307311 inputType = `$Schema.${ capModelName } InputSchema.findRawObject` ;
@@ -310,7 +314,7 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
310314 inputType = `$Schema.${ capModelName } InputSchema.create` ;
311315 break ;
312316 case 'createMany' :
313- inputType = `$Schema.${ capModelName } InputSchema.createMany` ;
317+ inputType = `$Schema.${ capModelName } InputSchema.createMany.optional() ` ;
314318 break ;
315319 case 'deleteOne' :
316320 inputType = `$Schema.${ capModelName } InputSchema.delete` ;
@@ -319,7 +323,7 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
319323 inputType = `$Schema.${ capModelName } InputSchema.update` ;
320324 break ;
321325 case 'deleteMany' :
322- inputType = `$Schema.${ capModelName } InputSchema.deleteMany` ;
326+ inputType = `$Schema.${ capModelName } InputSchema.deleteMany.optional() ` ;
323327 break ;
324328 case 'updateMany' :
325329 inputType = `$Schema.${ capModelName } InputSchema.updateMany` ;
@@ -337,7 +341,7 @@ export const getInputSchemaByOpName = (opName: string, modelName: string) => {
337341 inputType = `$Schema.${ capModelName } InputSchema.groupBy` ;
338342 break ;
339343 case 'count' :
340- inputType = `$Schema.${ capModelName } InputSchema.count` ;
344+ inputType = `$Schema.${ capModelName } InputSchema.count.optional() ` ;
341345 break ;
342346 default :
343347 break ;
0 commit comments