0
votes

What's the best way to intercept a response before sending it back in nestjs/graphql?

I'm trying to intercept a response of a query result, extracting some of the data into the context.extensions before sending it back. I've tried applying a middleware using graphql-middleware but it does not work.

this is what i've tried doing

export const Pagination: MiddlewareFn = async ({ info }, next) => {
// here i'm trying to extract the data
};

and this is where i'm trying to put it (resolver.ts)

@UseMiddleware(Pagination)
  findAll(options: PaginationInput): Promise<PaginatedReport> {
    return applyPagination(this.reportRepository, options);
}
2

2 Answers

0
votes

Why not use an interceptor which was made specifically for intercepting responses before business logic and post business logic? There are several great examples on the docs site (linked above).

0
votes

You can do that by implementing interceptors, you can play within the Execution context too, here's full documentation about that Interceptors, if you need more Execution context you can visit Execution context