Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mutations GraphQL for new created models not working. #3237

Closed
daannijkamp opened this issue Jan 22, 2020 · 11 comments
Closed

Update mutations GraphQL for new created models not working. #3237

daannijkamp opened this issue Jan 22, 2020 · 11 comments
Assignees
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 not-reproducible Not able to reproduce the issue

Comments

@daannijkamp
Copy link

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
@SwaySway SwaySway added graphql-transformer-v1 Issue related to GraphQL Transformer v1 pending-triage Issue is pending triage labels Jan 22, 2020
@daannijkamp
Copy link
Author

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

@SwaySway SwaySway self-assigned this Jan 23, 2020
@SwaySway SwaySway added this to Josue bugs in Issue tracking Jan 23, 2020
@SwaySway SwaySway moved this from Josue bugs to Weimin bugs in Issue tracking Jan 23, 2020
@SwaySway SwaySway moved this from Weimin bugs to Josue bugs in Issue tracking Jan 23, 2020
@SwaySway
Copy link
Contributor

SwaySway commented Jan 23, 2020

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 SwaySway removed this from Josue bugs in Issue tracking Jan 23, 2020
@daannijkamp
Copy link
Author

@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).

@kaustavghosh06 kaustavghosh06 added this to Josue bugs in Issue tracking Feb 10, 2020
@SwaySway
Copy link
Contributor

@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.

@SwaySway SwaySway added not-reproducible Not able to reproduce the issue and removed pending-triage Issue is pending triage labels Feb 10, 2020
@SwaySway SwaySway moved this from Josue to Merged in Issue tracking Feb 10, 2020
@danny-waite
Copy link

danny-waite commented Feb 25, 2020

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!
}

@rush86999
Copy link

rush86999 commented Mar 21, 2020

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

@VincentDevelopment
Copy link

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!

@danibrear
Copy link

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.

@danibrear
Copy link

As an update to my question above I found this solution and it worked for me: #3796 (comment) Looks like it has to do with how the apps resolve conflicts. Removing that removed the _version number.

@xloup
Copy link

xloup commented Mar 24, 2021

Had the same problem. Solved it by runing "amplify update api" and Answering "NO" to the question => "enable conflict detection"
Thanks @SwaySway

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
graphql-transformer-v1 Issue related to GraphQL Transformer v1 not-reproducible Not able to reproduce the issue
Projects
No open projects
Issue tracking
Merged/Closed
Development

No branches or pull requests

7 participants