这个问题主要是由于 Apollo Cache 中 batch 函数的缺失所导致的。一种解决方法是使用 InMemoryCache,因为它具有 batch 函数。同时,还需要更新代码中的缓存实例,以使用 InMemoryCache:
import {InMemoryCache} from "@apollo/client/cache";
const cache = new InMemoryCache();
另外一种解决方法是手动添加 batch 函数,可以按照以下示例代码将其添加到 Apollo Cache 中:
import { gql } from "@apollo/client";
// 添加 batch 函数 const batchFn = async (operations, fetchFunc) => { const responses = []; for (let operation of operations) { const { query, variables, context } = operation; const result = await fetchFunc({ query, variables, context }); responses.push(result); } return responses; };
const createCache = () => { return new ApolloCache({ typePolicies: { // ... }, // 添加 batch 函数 batch: batchFn, }); };
const cache = createCache();