I was trying onError for graphql mutations and realised that they don\'t work properly:
https://github.com/apollographql/apollo-client/issues/5708
What else can
You could use onError
callback instead of checking for addingContactError
property from the result directly on function body like shown below
const _onCreateUserRelationError = React.useCallback((error: ApolloError) => {
console.log('this is the error', error);
Alert.alert(error.message.includes('already exists') ? 'Contact Already Exists' : 'Unable to Add Contact');
}, []);
const [
createUserRelationMutation,
{
data: addingContactData,
loading: addingContactLoading,
called: isMutationCalled,
},
] = useCreateUserRelationMutation({
onCompleted: () => {
Alert.alert('Contact Added');
},
onError: _onCreateUserRelationError
});
Note: Memoize the component using React.memo
to avoid unnecessary re-rendering of this component