@@ -210,7 +210,7 @@ describe("Test Service", () => {
210210
211211 it ( "should not invalidate schema when autoUpdateSchema is false" , async ( ) => {
212212 const { broker, svc, stop } = await startService ( {
213- autoUpdateSchema : false
213+ autoUpdateSchema : false ,
214214 } ) ;
215215 svc . invalidateGraphQLSchema = jest . fn ( ) ;
216216
@@ -223,7 +223,7 @@ describe("Test Service", () => {
223223
224224 it ( "should not invalidate schema when autoUpdateSchema is true" , async ( ) => {
225225 const { broker, svc, stop } = await startService ( {
226- autoUpdateSchema : true
226+ autoUpdateSchema : true ,
227227 } ) ;
228228 svc . invalidateGraphQLSchema = jest . fn ( ) ;
229229
@@ -548,6 +548,7 @@ describe("Test Service", () => {
548548
549549 beforeEach ( ( ) => {
550550 svc . dataLoaderOptions . clear ( ) ;
551+ svc . dataLoaderBatchParams . clear ( ) ;
551552 } ) ;
552553
553554 it ( "should return null if no rootValue" , async ( ) => {
@@ -637,6 +638,79 @@ describe("Test Service", () => {
637638 expect ( ctx . call ) . toHaveBeenNthCalledWith ( 2 , "users.resolve" , { a : 5 , id : [ 5 ] } ) ;
638639 } ) ;
639640
641+ it ( "should call the action via the loader using all root params" , async ( ) => {
642+ svc . dataLoaderBatchParams . set ( "users.resolve" , "testBatchParam" ) ;
643+ const resolver = svc . createActionResolver ( "users.resolve" , {
644+ rootParams : {
645+ authorId : "authorIdParam" ,
646+ testId : "testIdParam" ,
647+ } ,
648+
649+ dataLoader : true ,
650+ } ) ;
651+
652+ const ctx = new Context ( broker ) ;
653+ ctx . call = jest . fn ( ) . mockResolvedValue ( [ "res1" , "res2" , "res3" ] ) ;
654+
655+ const fakeRoot1 = { authorId : 1 , testId : "foo" } ;
656+ const fakeRoot2 = { authorId : 2 , testId : "bar" } ;
657+ const fakeRoot3 = { authorId : 5 , testId : "baz" } ;
658+
659+ const fakeContext = { ctx, dataLoaders : new Map ( ) } ;
660+ const res = await Promise . all ( [
661+ resolver ( fakeRoot1 , { } , fakeContext ) ,
662+ resolver ( fakeRoot2 , { } , fakeContext ) ,
663+ resolver ( fakeRoot3 , { } , fakeContext ) ,
664+ ] ) ;
665+
666+ expect ( res ) . toEqual ( [ "res1" , "res2" , "res3" ] ) ;
667+
668+ expect ( ctx . call ) . toHaveBeenCalledTimes ( 1 ) ;
669+ expect ( ctx . call ) . toHaveBeenNthCalledWith ( 1 , "users.resolve" , {
670+ testBatchParam : [
671+ { authorIdParam : 1 , testIdParam : "foo" } ,
672+ { authorIdParam : 2 , testIdParam : "bar" } ,
673+ { authorIdParam : 5 , testIdParam : "baz" } ,
674+ ] ,
675+ } ) ;
676+ } ) ;
677+
678+ it ( "should call the action via the loader using all root params while leveraging cache" , async ( ) => {
679+ svc . dataLoaderBatchParams . set ( "users.resolve" , "testBatchParam" ) ;
680+ const resolver = svc . createActionResolver ( "users.resolve" , {
681+ rootParams : {
682+ authorId : "authorIdParam" ,
683+ testId : "testIdParam" ,
684+ } ,
685+
686+ dataLoader : true ,
687+ } ) ;
688+
689+ const ctx = new Context ( broker ) ;
690+ ctx . call = jest . fn ( ) . mockResolvedValue ( [ "res1" , "res2" ] ) ;
691+
692+ const fakeRoot1 = { authorId : 1 , testId : "foo" } ;
693+ const fakeRoot2 = { authorId : 2 , testId : "bar" } ;
694+ const fakeRoot3 = { authorId : 1 , testId : "foo" } ; // same as fakeRoot1
695+
696+ const fakeContext = { ctx, dataLoaders : new Map ( ) } ;
697+ const res = await Promise . all ( [
698+ resolver ( fakeRoot1 , { } , fakeContext ) ,
699+ resolver ( fakeRoot2 , { } , fakeContext ) ,
700+ resolver ( fakeRoot3 , { } , fakeContext ) ,
701+ ] ) ;
702+
703+ expect ( res ) . toEqual ( [ "res1" , "res2" , "res1" ] ) ;
704+
705+ expect ( ctx . call ) . toHaveBeenCalledTimes ( 1 ) ;
706+ expect ( ctx . call ) . toHaveBeenNthCalledWith ( 1 , "users.resolve" , {
707+ testBatchParam : [
708+ { authorIdParam : 1 , testIdParam : "foo" } ,
709+ { authorIdParam : 2 , testIdParam : "bar" } ,
710+ ] ,
711+ } ) ;
712+ } ) ;
713+
640714 it ( "should reuse the loader for multiple calls with the same context" , async ( ) => {
641715 const resolver = svc . createActionResolver ( "users.resolve" , {
642716 rootParams : {
@@ -1171,7 +1245,10 @@ describe("Test Service", () => {
11711245 actions : [
11721246 {
11731247 name : "test-action-1" ,
1174- graphql : { dataLoaderOptions : { option1 : "option-value-1" } } ,
1248+ graphql : {
1249+ dataLoaderOptions : { option1 : "option-value-1" } ,
1250+ dataLoaderBatchParam : "batch-param-1" ,
1251+ } ,
11751252 } ,
11761253 { name : "test-action-2" } ,
11771254 ] ,
@@ -1182,7 +1259,10 @@ describe("Test Service", () => {
11821259 actions : [
11831260 {
11841261 name : "test-action-3" ,
1185- graphql : { dataLoaderOptions : { option2 : "option-value-2" } } ,
1262+ graphql : {
1263+ dataLoaderOptions : { option2 : "option-value-2" } ,
1264+ dataLoaderBatchParam : "batch-param-2" ,
1265+ } ,
11861266 } ,
11871267 { name : "test-action-4" } ,
11881268 ] ,
@@ -1250,6 +1330,13 @@ describe("Test Service", () => {
12501330 ] )
12511331 ) ;
12521332
1333+ expect ( svc . dataLoaderBatchParams ) . toEqual (
1334+ new Map ( [
1335+ [ "test-svc-1.test-action-1" , "batch-param-1" ] ,
1336+ [ "v1.test-svc-2.test-action-3" , "batch-param-2" ] ,
1337+ ] )
1338+ ) ;
1339+
12531340 // Test `context` method
12541341 const contextFn = ApolloServer . mock . calls [ 0 ] [ 0 ] . context ;
12551342
0 commit comments