【问题标题】:What is the $condition input parameter for in a GraphQL mutation generated by AWS Amplify CLI?AWS Amplify CLI 生成的 GraphQL 突变中的 $condition 输入参数是什么?
【发布时间】:2020-12-21 10:45:15
【问题描述】:

我已经从这个模型在 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 参数的用途是什么?

【问题讨论】:

  • 未标记为!,则不是强制性/不需要...用于过滤受影响的行/项目的可选参数
  • @xadm,谢谢。我刚刚对其进行了测试,它似乎与输入参数一起应用。鉴于 $input 已经是强制性的,我想不出一个真正有意义或可以应用的用例?
  • 不是为了创建,而是为了更新……

标签: graphql aws-appsync graphql-mutation graphql-codegen


【解决方案1】:

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

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

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

您可以在 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)"
    }
  ]

【讨论】:

    猜你喜欢
    • 2019-10-07
    • 2020-11-23
    • 2021-04-24
    • 2021-05-30
    • 2021-11-06
    • 2021-09-23
    • 1970-01-01
    • 2019-06-28
    • 2021-08-17
    相关资源
    最近更新 更多