AWS Amplify CLI生成的GraphQL突变中的$ condition输入参数是什么?

问题描述

我已通过此模型在AWS AppSync(使用CLI)上生成一个简单的GraphQL API:

type WalletProperty @model {
    id: ID!
    title: String!
}

生成一个CreateWalletProperty,UpdateWalletProperty和DeleteWalletProperty突变,它们都与此类似:

  mutation CreateWalletProperty(
    $input: CreateWalletPropertyInput!
    $condition: ModelWalletPropertyConditionInput    <<<<<<<<<<<<  what is this for?
  ) {
    createWalletProperty(input: $input,condition: $condition) {
      id
      title
      createdAt
      updatedAt
    }
  }

,条件的模式为:

input ModelWalletPropertyConditionInput {
  title: ModelStringInput
  and: [ModelWalletPropertyConditionInput]
  or: [ModelWalletPropertyConditionInput]
  not: ModelWalletPropertyConditionInput
}

鉴于我总是必须提供强制性的$ input,$ condition参数是什么?

解决方法

在我上面的例子中,GraphQL由DynamoDB表支持;

在后台,GraphQL操作转换为PutItem,UpdateItem和DeleteItem DynamoDB操作。

对于这些数据操作操作,DynamoDB API允许您指定条件表达式来确定应修改的项目。如果条件表达式的计算结果为true,则操作成功;否则,操作成功。否则,操作将失败。

您可以在AWS Condition Expressions DynamoDB dev guide

上阅读有关每种条件的用例的更多信息。

在GraphQL突变级别,仅当记录满足条件时,突变才会继续进行。否则,不允许突变,并返回ConditionalCheckFailedException:

"errors": [
    {
      "path": [
        "deleteWalletProperty"
      ],"data": null,"errorType": "DynamoDB:ConditionalCheckFailedException","errorInfo": null,"locations": [
        {
          "line": 12,"column": 3,"sourceName": null
        }
      ],"message": "The conditional request failed (Service: DynamoDb,Status Code: 400,Request ID: E3PR9OM6M5J1QBHKNT8E4SM1DJVV4KQNSO5AEMVJF66Q9ASUAAJG,Extended Request ID: null)"
    }
  ]