将appolo-server-express从v2升级到v3后的插件错误
我正在使用Sentry的一个帮助页面中的插件示例。它工作正常 - 但是在升级到Apollo-Server-Express 3后,我会遇到以下打字稿错误。我不明白我要做什么才能解决错误。
ts2322:type'{requestDidstart(:any):{didencountererrors(ctx:any):void; }; } []'不能分配给'PlugindeFinition []'。    type'{requestDidStart(:any):{didenCounterErrors(ctx:any):void; }; }'不能分配“ PlugindeFinition”。      type'{requestDidStart(_:any):{didencountererrors(ctx:any):void; }; }'不能分配给'apolloserverplugin< string,any gt;>''。       'requestDidstart(...)返回的类型在这些类型之间是不兼容的。         nbsp; type'{didencountererrors(ctx:any):void; }'缺少类型'Promise< void |的以下属性| graphqlrequestlistener< record< string,any ny>>>':然后,捕获,[symber.tostringtag],最后type.d.ts.d.ts(45,5):预期类型来自属性的'插入插件键入“配置”
这是插件:
const plugins = [
{
requestDidStart(_:any) {
/* Within this returned object, define functions that respond
to request-specific lifecycle events. */
return {
didEncounterErrors(ctx:any ) {
// If we couldn't parse the operation, don't
// do anything here
if (!ctx.operation) {
return
}
for (const err of ctx.errors) {
// Only report internal server errors,
// all errors extending ApolloError should be user-facing
if (err instanceof ApolloError) {
continue;
}
// Add scoped report details and send to Sentry
Sentry.withScope(scope => {
// Annotate whether failing operation was query/mutation/subscription
scope.setTag("kind", ctx.operation.operation);
// Log query and variables as extras (make sure to strip out sensitive data!)
scope.setExtra("query", ctx.request.query);
scope.setExtra("variables", ctx.request.variables);
if (err.path) {
// We can also add the path as breadcrumb
scope.addBreadcrumb({
category: "query-path",
message: err.path.join(" > "),
level: Sentry.Severity.Debug
});
}
const transactionId = ctx.request.http.headers.get(
"x-transaction-id"
);
if (transactionId) {
scope.setTransaction(transactionId);
}
Sentry.captureException(err);
});
}
}
};
}
}
]
I'm using the plugin example from one of Sentry's help pages. It was working fine - but after upgrading to apollo-server-express 3 I'm getting the following typescript error. I don't understand what I have to do in order to fix the error.
TS2322: Type '{ requestDidStart(: any): { didEncounterErrors(ctx: any): void; }; }[]' is not assignable to type 'PluginDefinition[]'. Type '{ requestDidStart(: any): { didEncounterErrors(ctx: any): void; }; }' is not assignable to type 'PluginDefinition'. Type '{ requestDidStart(_: any): { didEncounterErrors(ctx: any): void; }; }' is not assignable to type 'ApolloServerPlugin<Record<string, any>>'. The types returned by 'requestDidStart(...)' are incompatible between these types. Type '{ didEncounterErrors(ctx: any): void; }' is missing the following properties from type 'Promise<void | GraphQLRequestListener<Record<string, any>>>': then, catch, [Symbol.toStringTag], finally types.d.ts(45, 5): The expected type comes from property 'plugins' which is declared here on type 'Config'
Here's the plugin:
const plugins = [
{
requestDidStart(_:any) {
/* Within this returned object, define functions that respond
to request-specific lifecycle events. */
return {
didEncounterErrors(ctx:any ) {
// If we couldn't parse the operation, don't
// do anything here
if (!ctx.operation) {
return
}
for (const err of ctx.errors) {
// Only report internal server errors,
// all errors extending ApolloError should be user-facing
if (err instanceof ApolloError) {
continue;
}
// Add scoped report details and send to Sentry
Sentry.withScope(scope => {
// Annotate whether failing operation was query/mutation/subscription
scope.setTag("kind", ctx.operation.operation);
// Log query and variables as extras (make sure to strip out sensitive data!)
scope.setExtra("query", ctx.request.query);
scope.setExtra("variables", ctx.request.variables);
if (err.path) {
// We can also add the path as breadcrumb
scope.addBreadcrumb({
category: "query-path",
message: err.path.join(" > "),
level: Sentry.Severity.Debug
});
}
const transactionId = ctx.request.http.headers.get(
"x-transaction-id"
);
if (transactionId) {
scope.setTransaction(transactionId);
}
Sentry.captureException(err);
});
}
}
};
}
}
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
requess> requestdidstart
之前和didencountererrors
之前添加async
关键字,看来预期的返回结果必须是一个承诺。
Add the
async
keyword beforerequestDidStart
and beforedidEncounterErrors
Looks like the expected return result has to be a Promise now.