本文介绍了调用使用 apollo 缓存存储一次的提取 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在研究 Apollo GQL.我正在使用 apollo 缓存来减少不需要的 API 调用.我现在面临的问题是,当我更新数据时,我不应该重新获取数据,因为 API 已经被调用一次并存储在缓存中.
I have been working on Apollo GQL. I'm using apollo cache to reduce unwanted API calls. The probelm now I'm facing is when i have updated a data i should not re-fetch the data because the API is already called once and stored in cache.
- 我想做的是清除特定查询的缓存或从服务器重新获取数据.!!
- 我无法清除整个缓存,因为我调用了很多 API
- Thing i wanted to do is either clear cache for a particular query or refetch the datas from server.!!
- I can't clear the entire cache, cause i'm calling a lot of APIs
我必须在以下突变调用后重新获取数据.
i have to re-fetch the data after the following mutation call.
const [
reopenInvoice,
{ loading: reopenLoading, data: reopenData, error: reopenError },
] = useMutation<IReopenData, IReopenVariables>(INVOICE_CLONE, {
onCompleted: ({ invoiceClone: { errors, status } }) => {
if (!errors || !errors.length) {
message.success("Invoice Reopened");
} else {
message.error(errors.join(" "));
}
},
});
推荐答案
refetchQueries"是更新缓存的最简单方法.
"refetchQueries" is the simplest way of updating the cache.
https://www.apollographql.com/docs/角度/功能/缓存更新/#refetchqueries
const [reopenInvoice, { loading: reopenLoading, data: reopenData, error: reopenError }] = useMutation<IReopenData, IReopenVariables>(
INVOICE_CLONE,
{
onCompleted: ({ invoiceClone: { errors, status } }) => {
if (!errors || !errors.length) {
message.success("Invoice Reopened");
} else {
message.error(errors.join(" "));
}
},
refetchQueries: [
{
query: TO_REFETCH_QUERY,
variables: {
id: objectID,
},
},
],
}
);
这篇关于调用使用 apollo 缓存存储一次的提取 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!