首页 > 解决方案 > NestJS/graphql 拦截 graphql 响应

问题描述

在将响应发送回nestjs/graphql之前拦截响应的最佳方法是什么?

我试图拦截查询结果的响应,将一些数据提取到 context.extensions 中,然后再将其发回。我尝试使用 graphql-middleware 应用中间件,但它不起作用。

这就是我尝试做的

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

这就是我要放置的地方(resolver.ts)

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

标签: typescriptgraphqlnestjsmiddleware

解决方案


为什么不使用专门用于在业务逻辑和后业务逻辑之前拦截响应的拦截器?文档网站上有几个很好的例子(上面链接)。


推荐阅读