繁体   English   中英

尝试使用 Apollo Client InMemoryCache Object 从合并助手 function 获取数据,但仅接收 ref 属性

[英]Trying to get data from merge helper function with Apollo Client InMemoryCache Object, but receive only ref property

我正在尝试从我创建的类似分页的 API 中获取新的数据集,但我只从缓存中收到带有 ref 属性的 object,但数据来自 api。如何返回现有数据和传入数据的数组?

在内存缓存 Object

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

useQuery Hook 调用分页 api

如果需要,我把我的仓库留在下面

todo 客户端 git 回购与合并助手 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,
        });
      }
    }),
  ];
}

当尝试在自定义 function

[英]React throws an error “Can't assign to property” scrollLeft “on 1: not an object” When trying use ref in custom function

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM