gpt4 book ai didi

amazon-web-services - 如何使用 AppSync + DynamoDB 自动生成全局 ID

转载 作者:行者123 更新时间:2023-12-04 15:29:52 25 4
gpt4 key购买 nike

使用AppSync时如何自动生成ID和 DynamoDB作为数据库源?

我有一个看起来像下面的类型

type Post {
id: ID!
creator: String!
createdAt: String!
like: Int!
dislike: Int!
frozen: Boolean!
}

和输入看起来像下面
input CreatePostInput {
id: ID!
creator: String!
createdAt: String!
like: Int!
dislike: Int!
frozen: Boolean!
}

而我的突变显然是两者的结合
createPost(input: CreatePostInput!): Post

但是,当我插入时,我必须执行以下操作
mutation createPost{
createPost(input:{
id:2
creator:"some creator"
createdAt:"some date"
like:0
dislike:0
frozen:false
}){
id
creator
createdAt
like
dislike
frozen
}
}

我无法在不需要知道什么的情况下插入帖子 id是现在。有什么办法可以提供 null或任何随机 idDynamoDBAppSync在插入表之前自动创建下一个索引?

Error generated when I update the input CreatePostInput to not accept any ID


{
"data": {
"createPost": null
},
"errors": [
{
"path": [
"createPost"
],
"data": null,
"errorType": "DynamoDB:AmazonDynamoDBException",
"errorInfo": null,
"locations": [
{
"line": 2,
"column": 3,
"sourceName": null
}
],
"message": "One or more parameter values were invalid: Type mismatch for key id expected: S actual: NULL (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ValidationException; Request ID: 629QEM7MH9BRAJ9MHU3FM1S0U3VV4KQNSO5AEMVJF66Q9ASUAAJG)"
}
]
}

My Resolver Template


{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
## If object "id" should come from GraphQL arguments, change to $util.dynamodb.toDynamoDBJson($ctx.args.id)
"id": $util.dynamodb.toDynamoDBJson($util.autoId()),
},
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args)
}

最佳答案

首先更新createPost解析器为:

{
"version": "2017-02-28",
"operation": "PutItem",
"key": {
"id": $util.dynamodb.toDynamoDBJson($util.autoId()),
},
"attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),
"condition": {
"expression": "attribute_not_exists(#id)",
"expressionNames": {
"#id": "id",
},
},
}

然后,删除 id来自您的字段 CreatePostInput输入类型:
input CreatePostInput {
creator: String!
createdAt: String!
like: Int!
dislike: Int!
frozen: Boolean!
}

保存两个更改,您应该完成。

如果你是像我一样眼见为实的人,看看这个 egghead.io video .

关于amazon-web-services - 如何使用 AppSync + DynamoDB 自动生成全局 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51299682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com