In my android kotlin project I'm trying to delete data using graphQL mutation
fun deleteAddressGraphQL(addressId: String) {
// Query the Address item to get the current version
Amplify.API.query(
ModelQuery.get(Address::class.java, addressId),
{ response ->
if (response.hasData()) {
val address = response.data
val mutation = ModelMutation.delete(address)
Amplify.API.mutate(mutation,
{ deleteResponse ->
if (deleteResponse.hasData()) {
Log.i("AmplifyAddressDeletion", "Successfully Deleted Address with id: ${deleteResponse.data.id}")
} else if (deleteResponse.hasErrors()) {
Log.e("AmplifyAddressDeletion", "Response contains errors: ${deleteResponse.errors}")
} else {
Log.e("AmplifyAddressDeletion", "Response data is null")
}
},
{ error -> Log.e("AmplifyAddressDeletion", "Delete failed", error) }
)
} else if (response.hasErrors()) {
Log.e("AmplifyAddressDeletion", "Response contains errors: ${response.errors}")
} else {
Log.e("AmplifyAddressDeletion", "Response data is null")
}
},
{ error -> Log.e("AmplifyAddressDeletion", "Query failed", error) }
)
}
When this mutation is sent, my logcat show that every field of the database (_version field included with the right _version number), I get the response:
response contains errors: [GraphQLResponse.Error{message='Conflict resolver rejects
mutation.', locations='[GraphQLLocation{line='2', column='3'}]', path='[GraphQLPathSegment{value='deleteAddress'}]', extensions='{errorInfo=null, data={country=null, city=null, streetNumber=null, latitude=-0.0, postalCode=null, placeID=null, label=Home, userID=xxxxxxxx-06f1-4324-xxxx-7b71fe5fd879, createdAt=2023-08-02T13:52:11.134Z, isDefault=null, streetName=null, formattedAddress=3 street example, CA, USA, province=null, id=fdafea2a-d098-4e9b-bad2-2b297aa59d10, _version=1, longitude=0.0, updatedAt=2023-08-02T13:52:11.134Z}, errorType=ConflictUnhandled}'}]
When I watch the cloudwatch log, I get this :
xxxxxxxx-dd12-46bc-xxxx-ff51896f4881 GraphQL Query: mutation DeleteAddress($input: DeleteAddressInput!) {
deleteAddress(input: $input) {
_version
city
country
createdAt
formattedAddress
id
isDefault
label
latitude
longitude
placeID
postalCode
province
streetName
streetNumber
updatedAt
userID
}
}
, Operation: null, Variables:
{
"input": {
"id": "8371155a-fc95-485f-9f0e-72920f620453"
}
}
We can see that the _version
number is not present in the cloudwatch log but it's here in the mutation that is being sent. This is a debug log of the mutation variable that I'm sending:
_version
city
country
createdAt
formattedAddress
id
isDefault
label
latitude
longitude
placeID
postalCode
province
streetName
streetNumber
updatedAt
userID
}, variables={input={id=8371155a-fc95-485f-9f0e-72920f620453}}, variableTypes={input=DeleteAddressInput!}, responseType=class
com.amplifyframework.datastore.generated.model.Address, variablesSerializer=com.amplifyframework.api.aws.GsonVariablesSerializer@0}
So the problem is located at the moment when the mutation is made, it's really weird.
I've tried with datastore and it's working :
fun deleteAddress(address: Address) {
Amplify.DataStore.query(
Address::class.java,
Address.ID.eq(address.id),
{ matches ->
if (matches.hasNext()) {
val matchedAddress = matches.next()
Amplify.DataStore.delete(
matchedAddress,
{ success -> Log.i("AmplifyDataStore", "Deleted item: " + success.item().id) },
{ error -> Log.e("AmplifyDataStore", "Could not delete item", error) }
)
} else {
Log.e("AmplifyDataStore", "No address found with the id: ${address.id}")
}
},
{ error -> Log.e("AmplifyDataStore", "Failed to get address", error) }
)
}
However, with datastore, the _deleted
field is set to True but the data is not deleted from the dynamoDB. The graphql however will delete the line in the dynamoDB, that's why I prefer using it.