Reputation: 9699
I'm trying to create a logging interceptor with graphql, apollo federation and nestjs. I'm following this guide and modified my code to
import {
Injectable, NestInterceptor, ExecutionContext, CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
public intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
console.log('Before...');
const now = Date.now();
return next
.handle()
.pipe(
tap(() => console.log(`After... ${Date.now() - now}ms`)),
);
}
}
And apply it like this:
const app: INestApplication = await NestFactory.create(AppModule, new ExpressAdapter(expressApp), {
logger,
});
app.useGlobalInterceptors(new LoggingInterceptor());
In tcpdump an http request is present between apollo-federation and this service when I post graphql request to federation. Despite it there're no logs from my interceptor. But If i make a direct graphql request to this ms (w/o federation), logs are there.
Also another question, I would like to catch response headers and string body. I think this could be done on express level only, but it somehow doesn't work at all, I tried everything in this answer.
Upvotes: 2
Views: 1104
Reputation: 242686
In order to make interceptors work on federated field resolvers you need to enable them when configuring GraphQLFederationModule
using fieldResolverEnhancers
property:
GraphQLFederationModule.forRoot({
...
fieldResolverEnhancers: ['interceptors'],
}),
Upvotes: 3