applyResolversEnhanceMap 不向解析器添加自定义装饰器?
applyResolversEnhanceMap not adding custom decorators to resolvers?
我正在使用 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
中的任何一个。我错过了什么?
对于遇到同样问题的任何人:答案是在 buildSchema
之前致电 applyResolversEnhanceMap
。
我正在使用 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
中的任何一个。我错过了什么?
对于遇到同样问题的任何人:答案是在 buildSchema
之前致电 applyResolversEnhanceMap
。