亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

圖QL突變中的錯誤

圖QL突變中的錯誤

森林海 2022-09-23 16:29:44
我正在嘗試圖形ql突變的錯誤,并意識到它們無法正常工作:https://github.com/apollographql/apollo-client/issues/5708那么,對于捕獲錯誤,還可以做些什么呢?在上一個問題中,我被告知使用嘗試捕獲塊進行突變不是一個好主意。我正在嘗試執行類似操作,并可能有一個解決方法:我要求用戶輸入,然后運行查詢。根據查詢結果,我呈現一些組件。在用戶組件中,我使用按鈕來運行突變。Userexport 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],  );
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

您可以使用回調,而不是直接從函數體上的結果檢查屬性,如下所示onErroraddingContactError


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


查看完整回答
反對 回復 2022-09-23
  • 1 回答
  • 0 關注
  • 93 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號