为防止 Apollo InMemoryCache 在处理根查询定义的片段时静默丢失数据,可以使用 fragmentMatcher
选项来准确地控制缓存中应存储哪些数据。以下是一个示例:
import { InMemoryCache } from '@apollo/client';
import { IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [
// 添加您的Type列表
// 示例:{ "kind": "UNION", "name": "SearchResult", "possibleTypes": [{ "name": "Person" }, { "name": "City" }] }
],
},
},
});
const cache = new InMemoryCache({
fragmentMatcher,
});
在 fragmentMatcher 选项中,我们声明了要存储的所有 types,这样 Apollo InMemoryCache 在根查询定义的片段中使用这些 types 时就不会静默丢失数据了。在添加 types 时,要注意将所有可能的类型都列出来,以确保数据不会丢失。