@@ -10,21 +10,30 @@ import { Observable } from 'rxjs';
1010import { tap } from 'rxjs/operators' ;
1111import { GqlExecutionContext } from '@nestjs/graphql' ;
1212import { TelemetryLogService } from './telemetry-log.service' ;
13- import { GetUserIdFromToken } from 'src/decorator/get-auth-token.decorator' ;
13+ import { InjectRepository } from '@nestjs/typeorm' ;
14+ import { User } from 'src/user/user.model' ;
15+ import { Repository } from 'typeorm' ;
1416
1517@Injectable ( )
1618export class LoggingInterceptor implements NestInterceptor {
1719 private readonly logger = new Logger ( 'RequestLogger' ) ;
1820 private startTime : number ;
1921
20- constructor ( private telemetryLogService : TelemetryLogService ) { }
22+ constructor (
23+ private telemetryLogService : TelemetryLogService ,
24+ @InjectRepository ( User )
25+ private readonly userRepository : Repository < User > ,
26+ ) { }
2127
22- intercept ( context : ExecutionContext , next : CallHandler ) : Observable < any > {
28+ async intercept (
29+ context : ExecutionContext ,
30+ next : CallHandler ,
31+ ) : Promise < Observable < any > > {
2332 const contextType = context . getType ( ) ;
2433 this . logger . debug ( `Intercepting request, Context Type: ${ contextType } ` ) ;
2534
2635 if ( contextType === ( 'graphql' as ContextType ) ) {
27- return this . handleGraphQLRequest ( context , next ) ;
36+ return await this . handleGraphQLRequest ( context , next ) ;
2837 } else if ( contextType === 'http' ) {
2938 return this . handleRestRequest ( context , next ) ;
3039 } else {
@@ -33,13 +42,18 @@ export class LoggingInterceptor implements NestInterceptor {
3342 }
3443 }
3544
36- private handleGraphQLRequest (
45+ private async handleGraphQLRequest (
3746 context : ExecutionContext ,
3847 next : CallHandler ,
39- ) : Observable < any > {
48+ ) : Promise < Observable < any > > {
4049 const ctx = GqlExecutionContext . create ( context ) ;
4150 const info = ctx . getInfo ( ) ;
4251 const userId = ctx . getContext ( ) . req . user ?. userId ;
52+ const user = await this . userRepository . findOne ( {
53+ where : { id : userId } ,
54+ select : { email : true } ,
55+ } ) ;
56+ const email = user ?. email ;
4357 if ( ! info ) {
4458 this . logger . warn (
4559 'GraphQL request detected, but ctx.getInfo() is undefined.' ,
@@ -76,6 +90,7 @@ export class LoggingInterceptor implements NestInterceptor {
7690 output : JSON . stringify ( value ) ,
7791 timeConsumed,
7892 userId,
93+ email,
7994 handler : 'GraphQL' ,
8095 } ) ;
8196 } ,
0 commit comments