You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法将已处理的GraphQL错误从Apollo Server微服务API传递到网关API

Gateway loses custom GraphQL error details (field-specific validation errors) with Apollo Server Express + Schema Stitching + Passport JWT

我正在用Apollo Server Express和Passport JWT构建一个基于GraphQL的微服务Web应用,目前包含User、Blog、Project、Profile四个独立微服务,还有一个网关API通过Schema Stitching(用片段实现跨服务关联,比如Blog.author、User.projects)整合所有服务。之前所有CRUD功能都正常,但在实现认证后遇到了错误处理的问题:认证流程本身没有异常,但远程微服务返回的GraphQL错误详情(比如字段级的{password: 'password incorrect'})无法传递到网关——网关只能感知到错误存在,具体的错误细节被吞掉了。

错误示例

User微服务返回的原始错误

{
  "errors": [
    {
      "message": "The request is invalid.",
      "type": "ValidationError",
      "state": { "password": ["password incorrect"] },
      "path": ["loginUser"],
      "stack": [...]
    }
  ],
  "data": { "loginUser": null }
}

网关返回的错误(丢失了state等细节)

{
  "errors": [
    {
      "message": "The request is invalid.",
      "locations": [{ "line": 2, "column": 3 }],
      "path": ["loginUser"],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "errors": [{ "message": "The request is invalid.", "locations": [], "path": ["loginUser"] }],
          "stacktrace": ["Error: The request is invalid.", ...],
          "data": { "loginUser": null }
        }
      }
    }
  ]
}

相关技术细节

  • 网关通过makeRemoteExecutableSchema和Apollo Link实现远程Schema整合,我尝试过在网关侧自定义formatError函数来解析错误,但始终无法获取到state这类自定义错误字段
  • User微服务中我自定义了GraphQlValidationError类,抛出的异常包含state字段(用于存储字段级的验证错误),但网关无法获取到这个字段的信息

如果需要更多代码细节(比如网关的Apollo Link配置、自定义错误类的实现、formatError函数代码),我可以随时补充。恳请各位大佬帮忙指点下解决方案,怎么让网关完整传递这些自定义的错误信息?

内容的提问来源于stack exchange,提问作者JJ McGregor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 06:33:53