66 InternalServerErrorException ,
77 NestInterceptor ,
88} from '@nestjs/common' ;
9- import { APP_INTERCEPTOR , ModuleRef } from '@nestjs/core' ;
9+ import { APP_INTERCEPTOR , ModuleRef , ContextIdFactory } from '@nestjs/core' ;
1010import { GqlExecutionContext } from '@nestjs/graphql' ;
1111import DataLoader from 'dataloader' ;
1212import { Observable } from 'rxjs' ;
@@ -35,7 +35,6 @@ const NEST_LOADER_CONTEXT_KEY: string = 'NEST_LOADER_CONTEXT_KEY';
3535@Injectable ( )
3636export class DataLoaderInterceptor implements NestInterceptor {
3737 constructor ( private readonly moduleRef : ModuleRef ) { }
38-
3938 /**
4039 * @inheritdoc
4140 */
@@ -44,14 +43,15 @@ export class DataLoaderInterceptor implements NestInterceptor {
4443 const ctx : any = graphqlExecutionContext . getContext ( ) ;
4544
4645 if ( ctx [ NEST_LOADER_CONTEXT_KEY ] === undefined ) {
47- ctx [ NEST_LOADER_CONTEXT_KEY ] = ( type : string ) : NestDataLoader < any , any > => {
46+ ctx [ NEST_LOADER_CONTEXT_KEY ] = async ( type : string ) : Promise < NestDataLoader < any , any > > => {
4847 if ( ctx [ type ] === undefined ) {
49- try {
50- ctx [ type ] = this . moduleRef
51- . get < NestDataLoader < any , any > > ( type , { strict : false } )
48+ try {
49+ const contextId = ContextIdFactory . getByRequest ( ctx . req ) ;
50+ ctx [ type ] = ( await this . moduleRef
51+ . resolve < NestDataLoader < any , any > > ( type , contextId , { strict : false } ) )
5252 . generateDataLoader ( ) ;
5353 } catch ( e ) {
54- throw new InternalServerErrorException ( `The loader ${ type } is not provided` ) ;
54+ throw new InternalServerErrorException ( `The loader ${ type } is not provided` + e ) ;
5555 }
5656 }
5757
@@ -65,12 +65,12 @@ export class DataLoaderInterceptor implements NestInterceptor {
6565/**
6666 * The decorator to be used within your graphql method.
6767 */
68- export const Loader = createParamDecorator ( ( data : string , [ _ , __ , ctx ] ) => {
68+ export const Loader = createParamDecorator ( async ( data : string , [ _ , __ , ctx ] ) => {
6969 if ( ctx [ NEST_LOADER_CONTEXT_KEY ] === undefined ) {
7070 throw new InternalServerErrorException ( `
71- You should provide interceptor ${ DataLoaderInterceptor . name } globaly with ${ APP_INTERCEPTOR }
71+ You should provide interceptor ${ DataLoaderInterceptor . name } globally with ${ APP_INTERCEPTOR }
7272 ` ) ;
7373 }
7474
75- return ctx [ NEST_LOADER_CONTEXT_KEY ] ( data ) ;
75+ return await ctx [ NEST_LOADER_CONTEXT_KEY ] ( data ) ;
7676} ) ;
0 commit comments