调用apollo缓存中存储一次的fetch API?[英] Call a fetch API which is stored once in using apollo cache?

本文是小编为大家收集整理的关于调用apollo缓存中存储一次的fetch API?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我一直在研究Apollo GQL.我正在使用Apollo缓存来减少不需要的API调用.我现在面临的概率是当我更新数据时,我不应该重新填写数据,因为API已被调用一次并存储在缓存中.

  • 我想做的是要么为特定查询的清晰缓存,要么从服务器中解救数据.!!
  • 我无法清除整个缓存,因为我打电话了很多API

我必须在以下突变调用后重新填写数据.

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"是更新缓存的最简单方法.

Angular/功能/CACHE-UPDATES/#REFCTERIES

 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,
                    },
                },
            ],
        }
    );

本文地址:https://www.itbaoku.cn/post/1938116.html

问题描述

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.

  • 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" 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,
                    },
                },
            ],
        }
    );