如何配置 Appsync 以从 HTTP 端点检索数据以填充列表?

How to configure Appsync to retrieve data from a HTTP endpoint to populate a list?

我有两个 http 端点设置:

GET /users/{userId}
GET /users/{userId}/notes/{noteId}

GET 用户 returns 包含多个 noteId 列表的有效负载,可用于向 GET Note 端点发出多个请求。

我正在尝试将 Appsync 配置为能够在单个查询中获取所有这些数据,但我无法获取列表以填充对象。

架构:

type Query {
  getUser(userId: String!): User
  getNote(userId: String!, noteId: String!): Note
}

type User {
  userId: ID!
  firstName: String!
  lastName: String!
  notes: [Note]
}

type Note {
  noteId: ID!
  noteText: String!
  createdDatetime: Int!
}

我为每个端点设置了一个数据源,我有一个 getUsergetNote 的解析器 - 我还有一个相同的 User.notes 解析器作为 getNote。这些解析器具有此响应映射:

#if($ctx.error)
  $util.error($ctx.error.message, $ctx.error.type)
#end
#if($ctx.result.statusCode == 200)
    $ctx.result.body
#else
    $utils.appendError($ctx.result.body, "$ctx.result.statusCode")
#end

我的 GET Note 解析器(包括 User.note 字段解析器)端点如下所示:

{
    "version": "2018-05-29",
    "method": "GET",
    "resourcePath": $util.toJson("/prod/users/$ctx.args.userId/notes/$ctx.args.noteId"),
    "params":{
        "headers":{
            "Content-Type": "application/json",
        }
    }
}

我可以从日志中看到,Appsync 尝试 运行 GET Note 解析器,但资源路径似乎没有填充任何 ID? (我可以在端点上的自定义授权器中看到这一点,它会注销仍然包含 $ctx.args...

的方法 ARN

感觉这是一个常见的用例,但我在任何地方都找不到解决方案或示例。我的方法是否正确,还是需要不同的解决方案?

我认为第一个问题是您的 User.notes 解析器以及您访问 userIdnoteId 的方式。当你有字段解析器时,你应该使用 ctx.source 来访问父字段 [Ref.]。例如,您应该在 User.notes 字段解析器中使用 ctx.source.userId

其次,当您要从 getNote HTTP 端点获取单个注释时,AppSync 在使用 BatchInvoke 通过 AWS Lambda 代理时支持此类行为。请参阅 this link to get better idea. Also, I think this SO post 上的“高级用例:批处理”与您的用例相关。

另一种可能性是让另一个 HTTP 端点一次获取所有用户的注释,但我不确定这对您的情况是否可行。