如何在Nestjs+ GraphQl?

发布于 2025-02-09 11:28:54 字数 142 浏览 0 评论 0原文

我正在使用下面的Apollo尝试Nestjs + GraphQl。当我将Apollo“调试”选项设置为真时,我可以在响应中看到堆栈Trace,但是我找不到在应用程序日志中记录此stackTrace的方法。

我想在日志中将其列入生产中的问题。有办法这样做吗?

I'm trying out NestJS + GraphQL using Apollo underneath. When I set the Apollo 'debug' option to be true, I can see the stacktrace in the response but I cannot find a way to log this stacktrace in our application logs.

I would like to have it in the log to troubleshoot issues in production. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

海风掠过北极光 2025-02-16 11:28:54

这是我使用的apolloservlugin

import { Plugin } from '@nestjs/apollo';
import { Logger } from '@nestjs/common';
import {
  ApolloServerPlugin,
  GraphQLRequestListener,
} from 'apollo-server-plugin-base';
import {
  BaseContext,
  GraphQLRequestContext,
  GraphQLRequestContextWillSendResponse,
} from 'apollo-server-types';
import * as util from 'util';

@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
  constructor(private readonly logger: Logger) {}

  async requestDidStart(
    requestContext: GraphQLRequestContext,
  ): Promise<GraphQLRequestListener> {
    const thatLogger = this.logger;
    if (requestContext.request.operationName !== 'IntrospectionQuery') {
      thatLogger.log(
        `request query: ${requestContext.request.query || 'undefined'}`,
      );
    }
    return {
      async willSendResponse(
        requestContextWillSendResponse: GraphQLRequestContextWillSendResponse<BaseContext>,
      ): Promise<void> {
        if (
          requestContextWillSendResponse.request.operationName !==
          'IntrospectionQuery'
        ) {
          if (!requestContextWillSendResponse.errors) {
            thatLogger.log(`response without any errors`);
          } else {
            const errors = requestContextWillSendResponse.errors.concat();
            const responseErrors =
              requestContextWillSendResponse.response.errors?.concat();
            if (errors && responseErrors) {
              for (let i = 0; i < errors.length; i++) {
                const result = {
                  ...responseErrors[i],
                  stack: errors[i].stack,
                };
                if (result.extensions) {
                  delete result.extensions.exception;
                }
                if (
                  result.extensions &&
                  result.extensions.code !== 'INTERNAL_SERVER_ERROR'
                ) {
                  thatLogger.warn(
                    `response with errors: ${util.inspect(result, {
                      depth: 4,
                    })}`,
                  );
                } else {
                  thatLogger.error(
                    `response with errors: ${util.inspect(result, {
                      depth: 4,
                    })}`,
                  );
                }
              }
            }
          }
        }
      },
    };
  }
}

Here's the ApolloServerPlugin I use

import { Plugin } from '@nestjs/apollo';
import { Logger } from '@nestjs/common';
import {
  ApolloServerPlugin,
  GraphQLRequestListener,
} from 'apollo-server-plugin-base';
import {
  BaseContext,
  GraphQLRequestContext,
  GraphQLRequestContextWillSendResponse,
} from 'apollo-server-types';
import * as util from 'util';

@Plugin()
export class LoggingPlugin implements ApolloServerPlugin {
  constructor(private readonly logger: Logger) {}

  async requestDidStart(
    requestContext: GraphQLRequestContext,
  ): Promise<GraphQLRequestListener> {
    const thatLogger = this.logger;
    if (requestContext.request.operationName !== 'IntrospectionQuery') {
      thatLogger.log(
        `request query: ${requestContext.request.query || 'undefined'}`,
      );
    }
    return {
      async willSendResponse(
        requestContextWillSendResponse: GraphQLRequestContextWillSendResponse<BaseContext>,
      ): Promise<void> {
        if (
          requestContextWillSendResponse.request.operationName !==
          'IntrospectionQuery'
        ) {
          if (!requestContextWillSendResponse.errors) {
            thatLogger.log(`response without any errors`);
          } else {
            const errors = requestContextWillSendResponse.errors.concat();
            const responseErrors =
              requestContextWillSendResponse.response.errors?.concat();
            if (errors && responseErrors) {
              for (let i = 0; i < errors.length; i++) {
                const result = {
                  ...responseErrors[i],
                  stack: errors[i].stack,
                };
                if (result.extensions) {
                  delete result.extensions.exception;
                }
                if (
                  result.extensions &&
                  result.extensions.code !== 'INTERNAL_SERVER_ERROR'
                ) {
                  thatLogger.warn(
                    `response with errors: ${util.inspect(result, {
                      depth: 4,
                    })}`,
                  );
                } else {
                  thatLogger.error(
                    `response with errors: ${util.inspect(result, {
                      depth: 4,
                    })}`,
                  );
                }
              }
            }
          }
        }
      },
    };
  }
}
心碎无痕… 2025-02-16 11:28:54

我能够使用Apolloserverplugin进行此操作。

I was able to do this using ApolloServerPlugin.

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