我正在尝试onError处理graphql突变,并意识到它们无法正常工作:
https://github.com/apollographql/apollo-client/issues/5708
然后可以采取什么其他措施来捕获错误?在上一个问题中,有人告诉我,使用try catch块进行突变不是一个好主意。
我正在尝试做这样的事情,可能的解决方法是:
我要求用户输入,然后运行查询。根据查询的结果,我渲染了一些User
组件。从用户组件中,我使用按钮来运行突变。
export const AddContact: React.FunctionComponent = () => {
const initialValues: FormValues = {
phoneNumber: '',
};
const [isSubmitted, setIsSubmitted] = useState(false);
const [userData, setUserData] = useState<UsersLazyQueryHookResult>('');
const navigation = useNavigation();
const validationSchema = phoneNumberValidationSchema;
const _onLoadUserError = React.useCallback((error: ApolloError) => {
Alert.alert('Unable to Add Contact');
}, []);
const [
createUserRelationMutation,
{
data: addingContactData,
loading: addingContactLoading,
error: addingContactError,
called: isMutationCalled,
},
] = useCreateUserRelationMutation({
onCompleted: () => {
Alert.alert('Contact Added');
},
});
const onAddContact = (id: number) => {
setIsSubmitted(false);
setUserData(null);
createUserRelationMutation({
variables: {
input: { relatedUserId: id, type: RelationType.Contact, userId: 1 },
},
});
}
const getContactId = React.useCallback(
(data: UsersLazyQueryHookResult) => {
if (data) {
if (data.users.nodes.length == 0) {
Alert.alert('No User Found');
} else {
setUserData(data);
}
}
},
[onAddContact],
);
const [loadUsers] = useUsersLazyQuery({
onCompleted: getContactId,
onError: _onLoadUserError,
});
const handleSubmitForm = React.useCallback(
(values: FormValues, helpers: FormikHelpers<FormValues>) => {
setIsSubmitted(true);
const plusSign = '+';
const newPhoneNumber = plusSign.concat(values.phoneNumber);
console.log('Submitted');
loadUsers({
variables: {
where: { phoneNumber: newPhoneNumber },
},
});
helpers.resetForm();
},
[loadUsers],
);
if (!addingContactLoading && isMutationCalled) {
if (addingContactError) {
console.log('this is the error', addingContactError);
if ((addingContactError.toString()).includes('already exists')){
Alert.alert('Contact Already Exists');
}
else{
Alert.alert('Unable to Add Contact');
}
}
}
return (
...
)
<User onAddContact={onAddContact} data={userData}></User>
...
export const User: React.FunctionComponent<UserProps> = ({
data,
onAddContact,
}) => {
if (!data) return null;
return (
<Button
onPress={() => onAddContact(Number(item.id))}
>
</Button>
通常,该过程可以正常工作,但是当突变中出现Alert.alert('Contact Already Exists');
错误时,就会产生问题。例如,关闭错误警报并运行新查询之后,应该只获取新的User组件(即使我现在仅运行查询,而不是突变)。但是,我也得到了Contact Already Added
警报。实际上,它会弹出两次。
也许问题出在回调中。
使用这样的.catch可以工作,但是没有其他方法可以这样做吗?由于我没有在查询中使用catch,因此代码将变得不一致。
.catch((err: any) => {
console.log('errror', err)
if ((err.toString()).includes('already exists')){
console.log('working')
Alert.alert('Contact Already Exists');
}
else{
Alert.alert('Unable to Add Contact');
}
});
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
});
注意:使用React.memo
记住组件,以避免不必要地重新渲染此组件