2

I'm modeling a User object with graphql for a react native app using amplify and appsync. I need to update the name field after creating the User but I am getting a DynamoDB: ConditionalCheckFailedException. Creating users and querying the User table works fine, I just can't update the fields of the existing object.

I am defining the User type within my project directory (amplify/backend/api/schema.graphql) and letting the amplify CLI generate GraphQL statements (queries, mutations and subscription) based on my schema types.

The schema I'm using:

type User @model @auth(rules: [{allow: owner}]) {
   username: String!
   name: String
}


type Mutation {
    createUser(input: CreateUserInput!, condition: ModelUserConditionInput): User
    updateUser(input: UpdateUserInput!, condition: ModelUserConditionInput): User
    deleteUser(input: DeleteUserInput!, condition: ModelUserConditionInput): User
}

input UpdateUserInput {
    username: String
    name: String
}

I run a mutation to create a User with the credentials of the logged-in user. From the AppSync console, I log in as the Cognito user with same username (745ab477-5702-4f8d-b938-a987a3d9d192) and run this mutation:

mutation updateUser{
    updateUser(input: {
      name: "James"
    }
    condition:{
      username: {eq: "745ab477-5702-4f8d-b938-a987a3d9d192"}
    }){
    username
    name
  }
}

Which produces:

  {
  "data": {
    "updateUser": null
  },
  "errors": [
    {
      "path": [
        "updateUser"
      ],
      "data": null,
      "errorType": "DynamoDB:ConditionalCheckFailedException",
      "errorInfo": null,
      "locations": [
        {
          "line": 40,
          "column": 5,
          "sourceName": null
        }
      ],
      "message": "The conditional request failed (Service: AmazonDynamoDBv2; Status Code: 400; Error Code: ConditionalCheckFailedException; Request ID: HND4VSCK8HLM5NC08VDEV51CORVV4KQNSO5AEMVJF66Q9ASUAAJG)"
    }
  ]
}

This is the DynamoDB User table after adding the current user:

table

1 Answer 1

3

You should specify the primary key in your schema

type User @model @auth(rules: [{allow: owner}]) {
   id:ID!,
   username: String!
   name: String
}

and change your request like below

  mutation updateSampleUser{
      updateSampleUser(
               input:{name:"James",id:"9f4...."}
              )
         {
           name
         }
    }


It is equivalent to the following SQL statement

 update tbl1 set name = "James" where  id = "9f4...."


and with @auth(rules: [{allow: owner}]) just owner can do update.

Sign up to request clarification or add additional context in comments.

1 Comment

Your welcome, did you find your answer for your last question(add primary key)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.