I'm not able to update the existing data on AWS Graphql API. Here are my files.
Schema
type PersonalInfo @model @auth(rules: [{allow: private}]) {
id: ID!
firstName: String!
middleName: String
lastName: String!
dateOfBirth: String!
primaryLanguage: String!
countryOfBirth: String!
gender: String!
maritalStatus: String!
}
mutation
export const updatePersonalInfo = /* GraphQL */ `
mutation UpdatePersonalInfo(
$input: UpdatePersonalInfoInput!
$condition: ModelPersonalInfoConditionInput
) {
updatePersonalInfo(input: $input, condition: $condition) {
id
firstName
middleName
lastName
dateOfBirth
primaryLanguage
countryOfBirth
gender
maritalStatus
createdAt
updatedAt
_version
_deleted
_lastChangedAt
}
}
`;
My update function
const currentUser = await Auth.currentAuthenticatedUser();
try {
const result = await API.graphql({
query: updatePersonalInfo,
variables: {
input: {
id: currentUser.attributes.sub,
firstName: data.firstName,
middleName: data.middleName,
lastName: data.lastName,
dateOfBirth: data.dateOfBirth,
primaryLanguage: data.primaryLanguage,
countryOfBirth: data.countryOfBirth,
gender: data.gender,
maritalStatus: data.maritalStatus
}
}
});
I'm able to create a new model, but not able to update it. From network tab I see that I send the new first name for example but in the successful response but with the previous name that was on dataStore.
I'm new to AWS Amplify, any help would be appreciated.
Response Preview:
I contacted the contributors and they gave me the solution on github which is not currently in their documentations. Basically we have to pass the _version
in the request. We first have to query to get the _version
and the send it back when we want to update the database.
try {
const result = await API.graphql({
query: updatePersonalInfo,
variables: {
input: {
...
_version: data._version
}
}
});
Just in case if you need to know how to get the _version value, here is the sample code:
const _getPersonalInfo = async () => {
try {
const currentUser = await Auth.currentAuthenticatedUser();
return await API.graphql({
query: getPersonalInfo,
variables: {id: currentUser.attributes.sub}
});
} catch (err) {
console.log(err);
return err;
}
};
_getPersonalInfo().then((response: any) => {
const _version = response.data.getPersonalInfo._version;
}