@@ -29,7 +29,14 @@ import {
2929 type Table ,
3030 type TableRelationalConfig ,
3131} from 'drizzle-orm' ;
32- import type { FieldNode } from 'graphql' ;
32+ import {
33+ type FieldNode ,
34+ Kind as GraphQLKind ,
35+ type GraphQLResolveInfo ,
36+ getNamedType ,
37+ isInterfaceType ,
38+ isObjectType ,
39+ } from 'graphql' ;
3340import type { DrizzleRef } from './interface-ref' ;
3441import type {
3542 DrizzleConnectionShape ,
@@ -302,6 +309,7 @@ export class DrizzleObjectFieldBuilder<
302309 parent : unknown ,
303310 args : PothosSchemaTypes . DefaultConnectionArguments ,
304311 context : { } ,
312+ info : GraphQLResolveInfo ,
305313 ) => {
306314 const countKey = `_${ name as string } _count` ;
307315 const parentRecord = parent as Record < string , unknown > ;
@@ -324,6 +332,34 @@ export class DrizzleObjectFieldBuilder<
324332 } ;
325333 }
326334
335+ // Detect totalCountOnly to skip cursor computation when only totalCount is requested
336+ const returnType = getNamedType ( info . returnType ) ;
337+ const fields =
338+ isObjectType ( returnType ) || isInterfaceType ( returnType ) ? returnType . getFields ( ) : { } ;
339+ const totalCountOnly = info . fieldNodes . every ( ( selection ) =>
340+ selection . selectionSet ?. selections . every (
341+ ( s ) =>
342+ s . kind === GraphQLKind . FIELD &&
343+ ( fields [ s . name . value ] ?. extensions ?. pothosDrizzleTotalCount ||
344+ s . name . value === '__typename' ) ,
345+ ) ,
346+ ) ;
347+
348+ if ( totalCountOnly ) {
349+ return {
350+ parent,
351+ args,
352+ totalCount : countValue ,
353+ edges : [ ] ,
354+ pageInfo : {
355+ startCursor : null ,
356+ endCursor : null ,
357+ hasPreviousPage : false ,
358+ hasNextPage : false ,
359+ } ,
360+ } ;
361+ }
362+
327363 const { select, cursorColumns } = getQuery ( args , context ) ;
328364
329365 return wrapConnectionResult (
@@ -347,6 +383,9 @@ export class DrizzleObjectFieldBuilder<
347383 ) => ( {
348384 totalCount : t . int ( {
349385 nullable : false ,
386+ extensions : {
387+ pothosDrizzleTotalCount : true ,
388+ } ,
350389 resolve : ( connection ) => connection . totalCount ,
351390 } ) ,
352391 ...( connectionOptions as { fields ?: ( t : unknown ) => { } } ) . fields ?.( t ) ,
0 commit comments