1
1
import { Injectable } from '@nestjs/common' ;
2
- import { type DataLoaderOptions } from '@seedcompany/data-loader' ;
2
+ import {
3
+ type DataLoaderOptions ,
4
+ lifetimeIdFromExecutionContext ,
5
+ } from '@seedcompany/data-loader' ;
3
6
import { NotFoundException } from '~/common' ;
7
+ import { SessionHost } from '../../components/authentication' ;
4
8
import { ConfigService } from '../config/config.service' ;
5
9
6
10
@Injectable ( )
7
11
export class DataLoaderConfig {
8
- constructor ( private readonly config : ConfigService ) { }
12
+ constructor (
13
+ private readonly config : ConfigService ,
14
+ private readonly sessionHost : SessionHost ,
15
+ ) { }
9
16
10
17
create ( ) : DataLoaderOptions < any , any > {
11
18
return {
@@ -16,6 +23,15 @@ export class DataLoaderConfig {
16
23
new NotFoundException (
17
24
`Could not find ${ String ( typeName ) } (${ String ( cacheKey ) } )` ,
18
25
) ,
26
+ getLifetimeId : ( context ) => {
27
+ // If we have a session, use that as the cache key.
28
+ // It will always be created / scoped within the GQL operation.
29
+ // This ensures the cached data isn't shared between users.
30
+ const session = this . sessionHost . current$ . value ;
31
+ if ( session ) return session ;
32
+
33
+ return lifetimeIdFromExecutionContext ( context ) ;
34
+ } ,
19
35
cache : ! this . config . isCli ,
20
36
} ;
21
37
}
0 commit comments