【问题标题】:Aws Appsync $util.error: data and errorInfo always nullAws Appsync $util.error:数据和 errorInfo 始终为空
【发布时间】:2019-01-14 23:16:27
【问题描述】:

我正在使用 AWS AppSync。当请求失败时,我试图在解析器的响应映射模板中使用 $util.error() 帮助器 (Documented here) 输出一些错误详细信息。无论我做什么,我都无法让 AppSync 在error 输出中输出dataerrorInfo 字段。

这是我拥有的 Lambda。

exports.handler = (event, context, callback) => {

  callback(null, {
    data: {
      name: "Test",
    },
    errorMessage: "Some error Message",
    errorType: "SomeErrorType",
    errors: {
      "foo": "bar",
      "bazz": "buzz",
    }
  })
};

如您所见,它非常简单。我只是返回一个具有dataerrorserrorMessageerrorType 属性的对象。

这是我的响应映射模板

$utils.error($context.result.errorMessage, $context.result.errorType, $context.result.data, $context.result.errors)

再次,非常简单。我只是使用来自 Lambda 的字段直接抛出错误。

但是当我执行查询时,我得到了这个:

{
  "data": {
    "myField": null
  },
  "errors": [
    {
      "path": [
        "myField"
      ],
      "data": null,
      "errorType": "SomeErrorType",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Some error Message"
    }
  ]
}

如您所见,errorTypemessage 字段被正确填充,但 errorInfodata 字段不正确。

我错过了什么吗?为什么这不起作用?

我还尝试在模板中硬编码$util.error 的参数。我得到了同样的结果......

【问题讨论】:

    标签: amazon-web-services aws-appsync


    【解决方案1】:

    如文档所述,Note: data will be filtered based on the query selection set。所以需要返回匹配选择集的数据。

    因此,对于如下所示的基本架构:

    type Post {
        id: ID!
        title: String! 
    }
    
    type Query {
        simpleQuery: Post
    }
    
    schema {
        query: Query
    }
    

    还有一个查询:

    query {
      simpleQuery {
        title   // Note this selection set
      }
    }
    

    还有一个响应映射模板:

    $utils.error($context.result.errorMessage, $context.result.errorType, $context.result.data, $context.result.errors)
    

    使用 Lambda 代码:

    exports.handler = (event, context, callback) => {
    
      callback(null, {
        data: {
          title: "Test",   // The same selection set
        },
        errorMessage: "Some error Message",
        errorType: "SomeErrorType",
        errors: {
          "foo": "bar",
          "bazz": "buzz",
        }
      })
    };
    

    它会返回:

    {
      "data": {
        "badOne": null
      },
      "errors": [
        {
          "path": [
            "badOne"
          ],
          "data": {
            "title": "Test"
          },
          "errorType": "SomeErrorType",
          "errorInfo": null,
          "locations": [
            {
              "line": 8,
              "column": 3,
              "sourceName": null
            }
          ],
          "message": "Some error Message"
        }
      ]
    }
    

    【讨论】:

    • 谢谢丽莎,我试过了,我终于让它工作了。我已经在data 部分返回了一个与我的查询匹配的对象,但我的问题是我缺少一些必填字段。发生这种情况时,data 部分将返回 null。所以,这解决了我与data 的问题。但是,errorInfo 部分呢?在您的示例中,当它应该在我的 lambda 响应中返回 errors 属性时,它也会返回 null。
    • 但是errorInfo仍然为空。有没有办法把它送到客户那里?
    【解决方案2】:

    对于errorInfo,您需要将模板版本更新为2018-05-29。 在这里查看我的答案:https://stackoverflow.com/a/53495843/2724342

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-05
      • 2019-12-27
      • 2022-09-23
      • 1970-01-01
      • 2017-05-07
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多