Reactjs Apollo客户端-变异检测变异完成,然后运行代码
我试图实现一个更新函数,因此,我使用GraphQL变异。但是,我需要能够检测突变何时完成,以便运行刷新功能 到目前为止,对于突变,我有:Reactjs Apollo客户端-变异检测变异完成,然后运行代码,reactjs,graph,apollo-client,react-apollo,Reactjs,Graph,Apollo Client,React Apollo,我试图实现一个更新函数,因此,我使用GraphQL变异。但是,我需要能够检测突变何时完成,以便运行刷新功能 到目前为止,对于突变,我有: const handleParameterUpdate = () => { let active = true; (async() => { await updateParameterMutation({variables: { parameterId: props.parameter.i
const handleParameterUpdate = () => {
let active = true;
(async() => {
await updateParameterMutation({variables: {
parameterId: props.parameter.id,
newField: newFieldValue;
}});
if (!active) {
return;
}
})().then(props.updateParams());
}
在父组件中,我有:
const handleRefresh = () => {
refetchJobParams({
id: parent.id
});
}
我的目的是在变异完成后运行
handleRefresh
函数。到目前为止,它会在handleParametersUpdate之后立即运行,因此,我需要刷新以获取新参数。我在这里看不到您的用法。但是,usemotation
hook有一个选项,可以添加一个onCompleted
回调,成功后将调用该回调。如果您也在从apollo查询中重新提取某些内容,那么我建议您也使用refetchQueries
选项。这使您可以编写更少的代码,并让Apollo处理重新蚀刻和完成回调
但是,在代码中,如果希望在handleParameterUpdate
内部运行handleRefresh
,则可以提取突变返回的任何值
const handleParameterUpdate = async () => {
const returnValue = await updateParameterMutation({variables: {
parameterId: props.parameter.id,
newField: newFieldValue;
}});
// whatever check you need to do on your mutation,
if (returnValue){
handleRefresh();
}
}