Caching 利用Apollo缓存

Caching 利用Apollo缓存,caching,react-hooks,apollo-client,apollo-cache-inmemory,Caching,React Hooks,Apollo Client,Apollo Cache Inmemory,升级到Apollo client 3.3时,我们决定启用缓存 为了减少网络流量和节省计算能力。 我们试图实现如下内容: cache: new InMemoryCache({ typePolicies: { VegaSpecification: { keyFields: ["reportID"] }, Query: { fields: { report: { merge: true

升级到Apollo client 3.3时,我们决定启用缓存 为了减少网络流量和节省计算能力。 我们试图实现如下内容:

cache: new InMemoryCache({   typePolicies: {
    VegaSpecification: {
      keyFields: ["reportID"]
    },
    Query: {
      fields: {
        report: {
          merge: true
        }
      }
    }   } })
但是,Apollo中实现的缓存不适合我们的数据结构 因此我们得到了错误的结果

首先,我们使用过滤器a查询报告。 我们在缓存中没有该查询的任何数据,因此我们点击后端并接收数据

{ 
   _id: "initialReportId",
   vegaspecification: {
     reportID: "initialReportId",
     data: "data for a"
   }
}
{
  _id: "initialReportId",
  vegaspecification: {
    reportID: "initialReportId",
    data: "data for b"
  }
}
在缓存中,apollo会自动存储:

Report: {
  _id: "initialReportId",
  vegaspecification: {
     "__ref":"VegaSpecification: {"reportID":"initialReportId"}"
  }
}
VegaSpecification: { 
  reportID: "initialReportId",
  data: "data for a"
}
然后我们用过滤器b查询报告。 我们在缓存中再次没有该查询的数据,点击后端并接收

{ 
   _id: "initialReportId",
   vegaspecification: {
     reportID: "initialReportId",
     data: "data for a"
   }
}
{
  _id: "initialReportId",
  vegaspecification: {
    reportID: "initialReportId",
    data: "data for b"
  }
}
在缓存中,我们更新

Report: {
  _id: "initialReportId",
  vegaspecification: {
     "__ref":"VegaSpecification:{"reportID":"initialReportId"}"
  }
}
VegaSpecification: {
  reportID: "initialReportId",
  data: "data for b"
}
当我们再次查询报告中的过滤器a时,会访问缓存,因为已经存在一个查询:

Report: {
  _id: "initialReportId",
  vegaspecification: {
     "__ref":"VegaSpecification:{"reportID":"initialReportId"}"
  }
}
然后解析对VegaSpecification的引用-返回

VegaSpecification: {
  reportID: "initialReportId",
  data: "data for b"
}
因此,我们为过滤器a返回不正确的数据。我们使用“UseMoom”来重新计算记录的值

我们如何构造实体,以便使用 不同的查询变量我们收到的结果可以通过id区分