【问题标题】:applyResolversEnhanceMap not adding custom decorators to resolvers?applyResolversEnhanceMap 没有向解析器添加自定义装饰器?
【发布时间】:2021-12-25 21:59:27
【问题描述】:

我正在使用typegraphql-prisma 包,并试图将自定义装饰器应用于我生成的解析器,但无济于事。我试图绕过瓶颈,但似乎无法识别它。

// src/lib/enhance.ts
export const mapped: ResolversEnhanceMap = {
  Like: {
    deleteLike: [RateLimitMiddleware()],
    createLike: [RateLimitMiddleware()],
  },
};
// src/lib/middleware.ts
const duration = 60 * 60;
const limit = 1;

export function RateLimitMiddleware() {
  console.log("Middleware is running...");
  // nothing below this line runs
  return createMethodDecorator<Context>(async ({ context, info }, next) => {
    const key = `rate-limit:${info.fieldName}:${context.user.user.id}`;
    console.log(key);
    const total = await redis.incr(key);
    if (total > limit) {
      throw new Error("You are being rate limited.");
    } else if (total === 1) {
      await redis.expire(key, duration);
    }
    return next();
  });
}
// src/index.ts
const app = express();
const http = createServer(app);
const schema = await buildSchema({
  resolvers: [...resolvers, AuthResolver, ImageResolver, CardResolver],
  validate: false,
});

applyResolversEnhanceMap(mapped);
// ... more code here

似乎根本没有应用我的 RateLimit 装饰器?每个请求都会触发任何console.log。我错过了什么?

【问题讨论】:

    标签: typescript typegraphql


    【解决方案1】:

    对于遇到相同问题的任何人:答案是在buildSchema 之前致电applyResolversEnhanceMap

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 2013-09-28
      • 2010-12-29
      • 2010-12-23
      • 2013-10-25
      • 2011-10-16
      • 1970-01-01
      相关资源
      最近更新 更多