我刚开始在React应用程序上使用apollo客户端,但是我一直坚持使用缓存。我有一个主页,上面列出了要查询的产品的ID和名称的产品列表,以及要查询ID,名称,描述和图像的产品页面。
我希望如果用户首先访问主页,然后在特定产品页面上仅查询该产品的描述和图像,则在加载过程中还要显示名称(因为我应该已经对其进行了缓存)。我遵循了文档(http://dev.apollodata.com/react/cache-updates.html)的“控制存储”部分,但仍无法解决。
当我们转到产品页面时执行的查询仍然要求提供产品的ID和名称,而由于我已经询问过它们,因此应该对其进行缓存。
我想我缺少了一些东西,但我无法弄清楚。这是一些代码:
// Create the apollo graphql client.
const apolloClient = new ApolloClient({
networkInterface: createNetworkInterface({
uri: `${process.env.GRAPHQL_ENDPOINT}`
}),
queryTransformer: addTypename,
dataIdFromObject: (result) => {
if (result.id && result.__typename) {
console.log(result.id, result.__typename); //can see this on console, seems okey
return result.__typename + result.id;
}
// Make sure to return null if this object doesn't have an ID
return null;
},
});
// home page query
// return an array of objects (Product)
export default graphql(gql`
query ProductsQuery …Run Code Online (Sandbox Code Playgroud)