代码之家  ›  专栏  ›  技术社区  ›  forJ

如何使用appsync+dynamodb自动生成全局id

  •  4
  • forJ  · 技术社区  · 6 年前

    如何在使用时自动生成ID AppSync 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 或任何随机的 身份证件 发电机 破解补丁 在插入表之前自动创建下一个索引?

    当我更新 input CreatePostInput 不 接受任何 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)"
        }
      ]
    }
    

    我的解析器模板

    {
        "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)
    }
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   vahdet    6 年前

    首先,更新 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 是的。

        2
  •  0
  •   mparis    6 年前

    您可以使用$util.autoid()Velocity帮助器函数自动生成Uuidv4。例如,您可以编写一个createpost解析器,它使用这样一个服务器端生成的id。

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