import DataLoader from 'dataloader' ; import { GraphQLResolveInfo , GraphQLFieldResolver } from 'graphql' ; import { approvalDLG } from './approvalDLG' ; import { attachmentDLG } from './attachmentDLG' ; import { commentDLG } from './commentDLG' ; import { contactDL } from './contactDL' ; import { customFieldDLG } from './customFieldDLG' ; import { dependencyDLG } from './dependencyDLG' ; import { folderDL } from './folderDL' ; import { taskDL } from './taskDL' ; import { timelogDLG } from './timelogDLG' ; import { accountDL } from './accountDL' ; import { timelogCategoryDLG } from './timelogCategoryDLG' ; import { workScheduleDLG } from './workScheduleDLG' ; enum DataLoaderKind { OperationGlobal , FieldNode } type DLCfg = { init : ( context : any ) => DataLoader < any , any > ; kind : DataLoaderKind . OperationGlobal ; } | { init : ( context : any , info : GraphQLResolveInfo ) => DataLoader < any , any > ; kind : DataLoaderKind . FieldNode ; } ; const DataLoadersCfg = { ApprovalID : { init : approvalDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , AttachmentID : { init : attachmentDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , CommentID : { init : commentDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , DependencyID : { init : dependencyDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , CustomFieldID : { init : customFieldDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , TimelogID : { init : timelogDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , TimelogCategoryID : { init : timelogCategoryDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , WorkScheduleID : { init : workScheduleDLG , kind : DataLoaderKind . OperationGlobal } as DLCfg , AccountID : { init : accountDL , kind : DataLoaderKind . FieldNode } as DLCfg , ContactID : { init : contactDL , kind : DataLoaderKind . FieldNode } as DLCfg , FolderID : { init : folderDL , kind : DataLoaderKind . FieldNode } as DLCfg , TaskID : { init : taskDL , kind : DataLoaderKind . FieldNode } as DLCfg , } ; type DataLoaderEntityNames = keyof typeof DataLoadersCfg ; /**
 * Get DataLoader instance, global o fieldNode specific
 */ function getDataLoader ( entityName , context , info ) { if ( ! context . dataLoaders ) context . dataLoaders = new WeakMap ( ) ; const { dataLoaders } = context ; const cfg = DataLoadersCfg [ entityName ] ; let contextKey ; if ( cfg . kind === DataLoaderKind . FieldNode ) { contextKey = info . fieldNodes ; } else { contextKey = cfg ; } let dl = dataLoaders . get ( contextKey ) ; if ( ! dl ) { dl = cfg . init ( context , info ) ; dataLoaders . set ( contextKey , dl ) ; } return dl ; } example export function resolveOneViaDL ( entityName , idGetter ) { return ( source , args , context , info ) => { const id = idGetter ( source , args , context , info ) ; if ( ! id ) return null ; return getDataLoader ( entityName , context , info ) . load ( id ) ; } ; } example export function resolveManyViaDL ( entityName , idsGetter ) { return ( source , args , context , info ) => { let ids = idsGetter ( source , args , context , info ) ; if ( ! ids ) return [ ] ; if ( ! Array . isArray ( ids ) ) ids = [ ids ] ; return getDataLoader ( entityName , context , info ) . loadMany ( ids ) ; } ; }