什么是使用中处理错误的最佳方法Mutation hook [duplicate]

问题描述 投票:0回答:1
[我正在使用graphql进行工作,在我的一个用例中,我正在使用useMutation hook进行添加,插入或删除之类的突变。

所以这里我有输入字段和按钮,因此单击我想将数据保存在运行良好的数据库中,

我的主要观点是>在useMutation中处理react-apollo-graphql中的错误的最佳方法是什么?当前,我正在使用Promise处理错误,但我知道这不是最好的方法

我正在做的是

const [createDep, {loading,error}] = useMutation(ADD_DEPARTMENTS,{ // here this error I want to do something here ton handle error and loading update(_,result){ console.log(result) } }) const submitDepartment = (e) => { let dept_name = e.department console.log(dept_name) createDep({ variables: { dept_name }, }).catch((res) => { // I do not think This is the right way to do const errors = res.graphQLErrors.map((error) => { console.log(error.message); }); }); };
我正在使用graphql来完成我的工作,在我的一个用例中,我正在使用useMutation挂钩进行诸如添加,插入或删除之类的突变。所以在这里,我有输入字段和按钮,因此单击要保存数据...
reactjs graphql react-apollo
1个回答
1
投票
我只是将其包装在try / catch范围内:

const [createDep] = useMutation(ADD_DEPARTMENTS, { update(_,result){ console.log(result) } }) const submitDepartment = async (e) => { let dept_name = e.department try { await createDep({variables: {dept_name}}) } catch (err) { // handle error here } };

© www.soinside.com 2019 - 2024. All rights reserved.