What is the $condition input parameter for in a GraphQL mutation generated by AWS Amplify CLI?
Asked Answered
P

1

6

I have generated a simple GraphQL API on AWS AppSync (using CLI) from this model:

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

This generated a CreateWalletProperty, UpdateWalletProperty and DeleteWalletProperty mutations all similar to this:

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

and the schema for the condition being:

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

Given that I always have to supply the mandatory $input, what is the $condition parameter for?

Pee answered 2/9, 2020 at 9:23 Comment(3)
not marked wit ! then not mandatory/not required ... optional parameter to filter affected rows/itemsFlesher
@xadm, thanks. I've just tested it and it appears it gets applied together with the input parameter. Given the $input is already mandatory I cannot think of a use-case where this actually makes sense or could be applied?Pee
for create not, but for update ...Flesher
P
8

In my case above the GraphQL is backed by a DynamoDB table;

Behind the scenes, the GraphQL operations translate to PutItem, UpdateItem, and DeleteItem DynamoDB operations.

For these data manipulation operations, DynamoDB API allows you to specify a condition expression to determine which items should be modified. If the condition expression evaluates to true, the operation succeeds; otherwise, the operation fails.

You can read more about use cases for each of these conditions on the AWS Condition Expressions DynamoDB dev guide

At GraphQL mutation level, only if the record meets the condition, will the mutation go ahead. Otherwise the mutation is not allowed and a ConditionalCheckFailedException is returned:

"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)"
    }
  ]
Pee answered 3/9, 2020 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.