Amplify-cli: Update mutations GraphQL for new created models not working.

Created on 22 Jan 2020  路  9Comments  路  Source: aws-amplify/amplify-cli

Describe the bug
Update mutations GraphQL for new created models is not working.
Fields that already have a (String) value are not overwritten/updated

Amplify CLI Version
4.12.0

To Reproduce
schema.graphql

type WidgetDef @model @auth(rules: [{ allow: owner, ownerField: "owner" }]) {
  id: ID
  owner: String
  title: String
}

AppSync Queries console

Create mutation

mutation CreateWidgetDef {
  createWidgetDef(input: {
    title: "this is a title"
  }) {
    id
    title
  }
}

# {
#   "data": {
#     "createWidgetDef": {
#       "id": "7bbb9710-034c-4eba-986c-72e4f73a1be3",
#       "title": "this is a title"
#     }
#   }
# }

Update mutation

mutation UpdateWidgetDef{
  updateWidgetDef(input: {
    id: "7bbb9710-034c-4eba-986c-72e4f73a1be3",
    title: "new title"
  }) {
    id
    owner
    title
    _version
    _deleted
    _lastChangedAt
  }
}

# {
#   "data": {
#     "updateWidgetDef": {
#       "id": "7bbb9710-034c-4eba-986c-72e4f73a1be3",
#       "owner": "a3cd1400-3870-4332-807d-f9ad36daf327",
#       "title": "this is a title",
#       "_version": 2,
#       "_deleted": null,
#       "_lastChangedAt": 1579689205692
#     }
#   }
# }

Expected behavior
title field is updated to "new title", but it is still the old title now

Desktop (please complete the following information):

  • OS: Ubuntu
  • Node Version: v10.15.3

Additional context

  • I did not receive any error messages.
  • I tried it several times with different models and field names
  • Update mutations for already created models still works
graphql-transformer not-reproducable

Most helpful comment

Hi there. So i had the exact same issue and was able to solve it quite simple... sadly it was nowhere in the documentation of AWS. Let's take the ex
ample from @daannijkamp:

mutation UpdateWidgetDef{
  updateWidgetDef(input: {
    id: "7bbb9710-034c-4eba-986c-72e4f73a1be3",
    title: "new title"
  }) {
    id
    owner
    title
    _version
    _deleted
    _lastChangedAt
  }
}

Results in no updates at all and just returns:

 {
   "data": {
     "updateWidgetDef": {
       "id": "7bbb9710-034c-4eba-986c-72e4f73a1be3",
       "owner": "a3cd1400-3870-4332-807d-f9ad36daf327",
       "title": "this is a title",
       "_version": 2,
       "_deleted": null,
       "_lastChangedAt": 1579689205692
     }
   }
 }

The key is to always provide the current _version of the updated item. The version field seems to be a serial which gets incrementally increased on every mutation. In this example the current version would be _version: 2 as you see in the output of the unsuccessful mutation. I guess that is AWS's approach to takle race conditions with NoSQL DBs like DynamoDB.
So the following mutation call should result in a successful update:

mutation UpdateWidgetDef{
  updateWidgetDef(input: {
    id: "7bbb9710-034c-4eba-986c-72e4f73a1be3",
    title: "new title"
    _version: 2
  }) {
    id
    owner
    title
    _version
    _deleted
    _lastChangedAt
  }
}

And the next time you'd call a mutation you would provide _version: 3. Hope i was able to help :)
Stay healthy and save!

All 9 comments

After rebuilding the entire Amplify project (from scratch), it worked again.
(With extract the same code and schema.graphql)
The biggest difference is the addition of "$condition" in mutations.
I think there is a bug with projects created with an older version of the Amplify CI

Hello @daannijkamp it seems like you are using a sync enabled api.
It is possible that the mutation did not go through as the version was not provided in the mutation (createWidgetDef).

@SwaySway are you referring to @versioned?
I don't make use of that. I also do not use conflict detection.
Update mutations for old models (made with an earlier version of Amplify CI) did work (no version provided in the mutation).

@daannijkamp
I only mention this since the snippet you provided for mutation includes this fields relates to conflict detection

    _version
    _deleted
    _lastChangedAt

Closing this issue as there is no action item, should you run into a similar situation again please comment below.

I'm getting the same issue here, I'd really like not to have to recreate the whole project again, any idea what I can do to debug/fix. I've installed the latest Amplify Client and tried recreating the model (comment out, push, reinstate) but with no success. An initial create mutual works, but an update doesn't, same behaviour in the AWS AppSync console too.

My model looks like this:

type AccountPreference
  @model
  @key(fields: ["owner"])
  @auth(
    rules: [
      { allow: owner, ownerField: "owner" }
      { allow: public, provider: iam }
    ]
  )
  @aws_cognito_user_pools
  @aws_iam {
  owner: String!
  timezone: String!
}

Same problem here. Any update? my version is: 4.16.1

Hi there. So i had the exact same issue and was able to solve it quite simple... sadly it was nowhere in the documentation of AWS. Let's take the ex
ample from @daannijkamp:

mutation UpdateWidgetDef{
  updateWidgetDef(input: {
    id: "7bbb9710-034c-4eba-986c-72e4f73a1be3",
    title: "new title"
  }) {
    id
    owner
    title
    _version
    _deleted
    _lastChangedAt
  }
}

Results in no updates at all and just returns:

 {
   "data": {
     "updateWidgetDef": {
       "id": "7bbb9710-034c-4eba-986c-72e4f73a1be3",
       "owner": "a3cd1400-3870-4332-807d-f9ad36daf327",
       "title": "this is a title",
       "_version": 2,
       "_deleted": null,
       "_lastChangedAt": 1579689205692
     }
   }
 }

The key is to always provide the current _version of the updated item. The version field seems to be a serial which gets incrementally increased on every mutation. In this example the current version would be _version: 2 as you see in the output of the unsuccessful mutation. I guess that is AWS's approach to takle race conditions with NoSQL DBs like DynamoDB.
So the following mutation call should result in a successful update:

mutation UpdateWidgetDef{
  updateWidgetDef(input: {
    id: "7bbb9710-034c-4eba-986c-72e4f73a1be3",
    title: "new title"
    _version: 2
  }) {
    id
    owner
    title
    _version
    _deleted
    _lastChangedAt
  }
}

And the next time you'd call a mutation you would provide _version: 3. Hope i was able to help :)
Stay healthy and save!

So I can confirm that I was updating my records and only the _version was being updated but including that value in the migration works. Does anyone know if there's a way to automatically update the version? I hate having to include the _version in each of my migrations.

As an update to my question above I found this solution and it worked for me: https://github.com/aws-amplify/amplify-cli/issues/3796#issuecomment-607519187 Looks like it has to do with how the apps resolve conflicts. Removing that removed the _version number.

Was this page helpful?
0 / 5 - 0 ratings