【问题标题】:AWS Appsync resolvers for updated related tables用于更新相关表的 AWS Appsync 解析器
【发布时间】:2018-11-15 07:59:47
【问题描述】:

我正在尝试为相关实体编写解析器。

这是我的架构的外观。

type User{
id:ID!
name: String!
posts:[Post] #Resolver 1
}

type Post{
id:ID!,
title: String!
body: String!
}

type CreatePostInput{
id:ID!,
title: String!
body: String!
}
type mutation{
addUserPost(userid:ID!, input:CreatePostInput!): Post
}

现在我为帖子添加了一个解析器(参见#resolver 1)

{
"version" : "2017-02-28",
"operation" : "Scan",
 "key": {
    "userid" : { "S" : "${context.source.id}" }
},
}

现在我为突变 addUserPost 添加了一个解析器

{
  "version" : "2017-02-28",
  "operation" : "PutItem",
   "key": {
      "userid" : { "S" : "${context.arguments.userid}" },
     "input" : $util.dynamodb.toDynamoDBJson(${context.arguments.input})     
     }    
}

现在当我运行查询时

mutation addnewposttest{
  addChapterToCourse(addUserPost:"c85a0508-ee0e-4ad8-8629-34880e1c6d74",
    input:{
      title:"Demo",
      body:"Test Body",
      id: "c85a0508-c85a0-508c-85a-0508"
    }){
      id
    }
}

I get DynamoDB:AmazonDynamoDBException as One or more parameter values were invalid: Missing the key id in the item (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: XXXXXXXXXXXX

我尝试更改第二个解析器的数据源,但没有成功。除了 this 之外,我没有找到 AWS 的任何好的文档,但是这里讨论的是简单的字符串数据类型,而不是对象类型集合。

有人可以帮助我理解如何处理解析器中的关系吗?谢谢

【问题讨论】:

  • 您在 addUserPost 的解析器中有 courseid。应该是userid
  • 感谢@LisaMShon 指出,这里发帖时出现了一些复制粘贴错误。
  • 我认为您的 #resolver1 模板也有误。 Scan 操作不支持 key 元素

标签: amazon-web-services aws-appsync


【解决方案1】:

我指的是两个单独的数据表并将数据存储在两个表中,我需要使用 BatchUpdate 项并指定表名。但是,我不能像在我的用例中那样做,我必须在一个表中插入并在另一个表中更新。

最后,我最终将 Id 复制到另一个集合中并在其上建立索引。

这就是我的最终架构的样子

type User{
id:ID!
name: String!
posts:[Post] #Resolver 1 -
}

type Post{
id:ID!,
userId:ID!# I will look for source.id on this field
title: String!
body: String!
}

感谢大家的参与!

【讨论】:

    【解决方案2】:

    正如我所评论的,您需要进行一些更新。

    但主要问题是您的 addUserPost 解析器模板包含 userid,但看起来您需要将其更改为 id。您的 User 类型或 Post 类型似乎没有名为 userid 的字段

    【讨论】:

    • 感谢您的回复。看到这篇文章,docs.aws.amazon.com/appsync/latest/devguide/…,我只是跟着这个。 userid 对应于突变的参数(键)。现在我学到了只有在这两个实体都位于同一个表中并且具有相同数据源的情况下才有效。
    猜你喜欢
    • 2019-01-06
    • 2018-10-11
    • 2019-02-04
    • 1970-01-01
    • 2019-10-14
    • 2021-05-02
    • 2019-08-11
    • 2019-04-16
    • 2019-02-03
    相关资源
    最近更新 更多