将appolo-server-express从v2升级到v3后的插件错误

发布于 2025-01-18 19:57:25 字数 2653 浏览 1 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2025-01-25 19:57:25

requess> requestdidstart之前和didencountererrors之前添加async关键字,

看来预期的返回结果必须是一个承诺。

Add the async keyword before requestDidStart and before didEncounterErrors

Looks like the expected return result has to be a Promise now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文