Aws Appsync $util.error: data and errorInfo always null

Aws Appsync $util.error: data and errorInfo always null

我正在玩 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",
    }
  })
};

如您所见,它非常简单。我只是 return 一个具有 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 的参数。我得到了相同的结果...

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

因此,对于如下所示的基本模式:

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",
    }
  })
};

会return:

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

对于 errorInfo,您需要将模板版本更新为 2018-05-29。 在这里查看我的回答: