@@ -6,7 +6,6 @@ import { Container as container, ObjectType } from 'typedi';
66import { getCustomRepository , getRepository , Repository } from 'typeorm' ;
77
88import { getFromContainer } from './container' ;
9- import { ensureInputOrder } from './dataloader' ;
109import { getErrorCode , getErrorMessage , handlingErrors } from './graphql-error-handling' ;
1110import { GraphQLContext , GraphQLContextDataLoader } from './GraphQLContext' ;
1211import { importClassesFromDirectories } from './importClassesFromDirectories' ;
@@ -29,10 +28,16 @@ export * from './container';
2928// Main Functions
3029// -------------------------------------------------------------------------
3130
31+ export interface CreateDataLoaderOptions {
32+ method ?: string ;
33+ key ?: string ;
34+ batch ?: boolean ;
35+ }
36+
3237/**
3338 * Creates a new dataloader with the typorm repository
3439 */
35- export function createDataLoader < T > ( obj : ObjectType < T > , method ?: string , key ?: string ) : DataLoader < any , any > {
40+ export function createDataLoader < T > ( obj : ObjectType < T > , options : CreateDataLoaderOptions = { } ) : DataLoader < any , any > {
3641 let repository ;
3742 try {
3843 repository = getCustomRepository < Repository < any > > ( obj ) ;
@@ -46,13 +51,14 @@ export function createDataLoader<T>(obj: ObjectType<T>, method?: string, key?: s
4651
4752 return new DataLoader ( async ( ids : number [ ] ) => {
4853 let items = [ ] ;
49- if ( method ) {
50- items = await repository [ method ] ( ids ) ;
54+ if ( options . method ) {
55+ items = await repository [ options . method ] ( ids ) ;
5156 } else {
5257 items = await repository . findByIds ( ids ) ;
5358 }
5459
55- return ensureInputOrder ( ids , items , key || 'id' ) ;
60+ const handleBatch = ( arr : any [ ] ) => options . batch === true ? arr : arr [ 0 ] ;
61+ return ids . map ( id => handleBatch ( items . filter ( item => item [ options . key || 'id' ] === id ) ) ) ;
5662 } ) ;
5763}
5864
0 commit comments