调用使用阿波罗缓存一次存储的提取API? [英] Call a fetch API which is stored once in using apollo cache?

查看:124
本文介绍了调用使用阿波罗缓存一次存储的提取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/angular/features/cache-updates/#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,
                    },
                },
            ],
        }
    );

这篇关于调用使用阿波罗缓存一次存储的提取API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆