为什么在 DynamoDB UpdateItem 操作中使用 ReturnValues: 'ALL_NEW' return 一个空 ID?

Why does using ReturnValues: 'ALL_NEW' in a DynamoDB UpdateItem operation return a null ID?

我正在使用 AWS AppSync GraphQL 突变对 DynamoDB 项目执行更新操作,我希望在更新操作 return 成功更新后通过 API 编辑项目的所有值.

该项目更新得很好,我还用 ALL_NEW 指定了 ReturnedValues 属性,这应该 return 所有属性但是 Item.Idnull 出于某种原因?

GraphQL:

type User {
    id: ID!
    name: String
    avatarUrl: String
    createdAt: String
    updatedAt: String
}

type Mutation {
    updateUser(userId: ID!, name: String, avatarUrl: String): User
}

mutation updateUser {
  updateUser(userId:"USER-14160000000", name:"Test Test", avatarUrl:"www.test.com") {
    id
    name
    avatarUrl
    createdAt
    updatedAt
  }
}

更新项目的 Lambda 函数:

const AWS = require("aws-sdk")
AWS.config.update({ region: "ca-central-1" })
const dynamoDB = new AWS.DynamoDB.DocumentClient()

async function updateUser(userId, name, avatarUrl) {
  var params = {
    TableName: "Bol-Table",
    Key: {
      "pk": userId,
      "sk": 'USER',
    },
    UpdateExpression: 'SET details.displayName = :name, details.avatarUrl = :avatarUrl, details.updatedAt = :updateDate',
    ExpressionAttributeValues: {
      ':name': name,
      ':avatarUrl':avatarUrl,
      ':updateDate': new Date().toISOString()
    },

    ReturnValues: 'ALL_NEW'
  };
  const Item = await dynamoDB.update(params).promise();
  console.log(Item);
  return { 
      id: Item.pk, 
      name: Item.displayName,
      avatarUrl: Item.avatarUrl,
      createdAt: Item.createdAt,
      updatedAt: Item.updatedAt
  };
}
module.exports = updateUser;

updateUser 调用的 GraphQL 输出:

{
  "data": {
    "updateUser": null
  },
  "errors": [
    {
      "path": [
        "updateUser",
        "id"
      ],
      "locations": null,
      "message": "Cannot return null for non-nullable type: 'ID' within parent 'User' (/updateUser/id)"
    }
  ]
}

来自 Amazon CloudWatch 的 console.log(Item) 的输出:

2021-10-10T17:47:39.338Z    d06b7a58-541e-456f-b830-1a50eea7c35a    INFO    {
  Attributes: {
    sk: 'USER',
    details: {
      avatarUrl: 'www.test.com',
      createdAt: '2021-10-10T16:40:25.455Z',
      displayName: 'Test',
      updatedAt: '2021-10-10T17:47:38.586Z'
    },
    pk: 'USER-16040000000'
  }
}

为什么我会收到错误消息,Cannot return null for non-nullable type: 'ID' within parent 'User' (/updateUser/id)

UpdateItem 的 AWS 文档显示 the attribute values are returned in a nested Attributes object within the response

{
   "Attributes": { 
      "string" : { 
         "B": blob,
         "BOOL": boolean,
         "BS": [ blob ],
         "L": [ 
            "AttributeValue"
         ],
         "M": { 
            "string" : "AttributeValue"
         },
         "N": "string",
         "NS": [ "string" ],
         "NULL": boolean,
         "S": "string",
         "SS": [ "string" ]
      }
   },
   ...
}

因此您需要使用 Item.Attributes 访问它们,而不是尝试在 Item 上访问它们,因为它们不存在于该级别。

这应该有效:

return { 
  id: Item.Attributes.pk, 
  name: Item.Attributes.details.displayName,
  avatarUrl: Item.Attributes.details.avatarUrl,
  createdAt: Item.Attributes.details.createdAt,
  updatedAt: Item.Attributes.details.updatedAt
};