微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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)"
    }
  ]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。