【问题标题】:NestJS/graphql intercepting graphql responseNestJS/graphql 拦截 graphql 响应
【发布时间】:2021-04-10 09:59:34
【问题描述】:

在将响应发送回 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);
}

【问题讨论】:

    标签: typescript graphql nestjs middleware


    【解决方案1】:

    你可以通过实现拦截器来做到这一点,你也可以在执行上下文中播放,这里是关于 Interceptors 的完整文档,如果你需要更多执行上下文,你可以访问Execution context

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2018-11-02
        • 2022-10-14
        • 2019-11-17
        • 2023-04-02
        • 2021-11-22
        • 2020-12-21
        • 2020-02-11
        • 2019-06-04
        • 2021-11-09
        相关资源
        最近更新 更多