1
votes

I'm having trouble with update mutations with react-native and amplify.

addProfileDetails = async (sub) => {
    let UserDetails = await API.graphql(graphqlOperation(mutations.updatePeople, {updatePeopleInput: 
         {
            UserID: sub,
            Organization: "Organization"
            Name: "name"
        }
    }));
    console.log(UserDetails);
  };

This code doesn't generate any errors, and it's pretty much the same as the code I used to create the record in the first place which works.

but it returns this data and doesn't actually update the record as intended.

Object {
  "data": Object {
    "updatePeople": null,
  },
}

I tested this query on the appsync query editor:

mutation update {
  updatePeople (updatePeopleInput: {
    PersonID: "XXXXXXXXXXXXXXXXXXXXXXXXXX",
    Gender: "Male"
  })
    {
    PersonID
    Gender
  }
}

And it returns the same. Create mutations work, Queries work. I don't understand what is happening here.

Any insights of what I'm doing wrong would be helpful. Also, if you need to the schema or the mutation code, I can provide that.

1

1 Answers

1
votes

I figured this out.

Turns out the default resolvers created by Appsync just don't work update mutations to my RDS.

I solved this by changing the request resolvers:

#set( $updateList = {} )
#foreach( $entry in $ctx.args.updatePeopleInput.keySet() )
  #set( $discard = $updateList.put($entry, "'$ctx.args.updatePeopleInput[$entry]'") )
#end
#set( $update = $updateList.toString().replace("{","").replace("}","") )
{
  "version": "2018-05-29",
  "statements":   [
    $util.toJson("UPDATE People SET Gender='$ctx.args.updatePeopleInput.Gender', Username='$ctx.args.updatePeopleInput.Username' WHERE PersonID='$ctx.args.updatePeopleInput.PersonID'"),
    $util.toJson("SELECT * FROM People WHERE PersonID='$ctx.args.updatePeopleInput.PersonID'")
    ]
}

before it was this:

#set( $updateList = {} )
#foreach( $entry in $ctx.args.updatePeopleInput.keySet() )
  #set( $discard = $updateList.put($entry, "'$ctx.args.updatePeopleInput[$entry]'") )
#end
#set( $update = $updateList.toString().replace("{","").replace("}","") )
{
  "version": "2018-05-29",
  "statements":   [
    $util.toJson("UPDATE People SET $update WHERE PersonID='$ctx.args.updatePeopleInput.PersonID'"),
    $util.toJson("SELECT * FROM People WHERE PersonID='$ctx.args.updatePeopleInput.PersonID'")
    ]
}

Apparently the $update doesn't work and you must explicitly name the variables to be set.