[英]How to get and set a ref for a newly cached related object in Apollo client InMemoryCache?
[英]Trying to get data from merge helper function with Apollo Client InMemoryCache Object, but receive only ref property
我正在尝试从我创建的类似分页的 API 中获取新的数据集,但我只从缓存中收到带有 ref 属性的 object,但数据来自 api。如何返回现有数据和传入数据的数组?
const client = new ApolloClient({
ssrMode: typeof window === undefined,
link: httpLink,
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
todos: {
merge(existing, incoming) {
console.log(incoming) // I want the data, not the ref object
return [ ...existing, ...incoming ]
}
}
}
}
}
})
});
结果在控制台日志中的照片合并助手的结果 Function
如果需要,我把我的仓库留在下面
您可以访问merge
的第三个参数中的缓存,并且可以使用它来将引用转换为数据对象。
// assuming these are all the fields on your
// Todo item's schema
const todoFragment = gql`
fragment todo on Todo {
id
name
completed
}
`;
function merge(existing, incoming, { isReference, cache, variables }) {
return [
...existing ?? [],
...incoming.map(x => {
if (!isReference(x))
return x; // it's already a data object
else {
// convert the Reference to a data object
return cache.readFragment({
fragment: todoFragment, // use a fragment with all properties on todo
id: options.cache.identify(x),
// only relevant if the objects' fields use variables
variables,
});
}
}),
];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.