数组类型中的图QL和DynamoDb更新项

时间:2018-02-20 15:27:03

标签: amazon-dynamodb graphql

我有一个架构:

type Profile {
    id: ID
    email : String
    awards: [Award]
}

type Award {
    id: ID!
    title: String!
    description: String!
}

我需要创建一个功能来更新具有个人资料ID和奖励ID的特定奖励,我想修改奖励中的标题和描述。

1 个答案:

答案 0 :(得分:0)

可以插入特定位置:

let i = AwardInput['position'];

创建更新表达式:

let expression = "set awards[" + i +"].title = :title, "
    expression = expression + "awards[" + i +"].description = :description, " 
    expression = expression + "awards[" + i +"].initialDate = :initialDate, " 
    expression = expression + "awards[" + i +"].updated_at = :updated_at "

更新表:

    docClient.update(
      {
        TableName: process.env.USER_TABLE,
        Key: { 'id': AwardInput.id, },
        UpdateExpression: expression,
        ExpressionAttributeValues: {
                                      ":title": AwardInput['title'],
                                      ":description": AwardInput['description'],
                                      ":initialDate": AwardInput['initialDate'],
                                      ":updated_at":  AwardInput['updated_at'] ,

                                   },
        ReturnValues: 'UPDATED_NEW',
      },