- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在我的 React 应用程序中使用 apollo 客户端实现缓存分页。
我的查询有过滤器参数,它应该是在缓存对象中创建新键的唯一参数。
出于某种原因,当 fetchMore 发生时指定了过滤器,新数据不会导致组件中的重新呈现。
我在合并函数中记录了现有的和传入的参数,似乎对于每个具有过滤器的 fetchMore,新数据确实到达了。所以,我不明白为什么组件没有重新渲染。
让事情变得更糟:使用或不使用过滤器多次调用 fetchMore 发送 http 请求并将传入数据与现有数据合并。我希望这不会发生,因为客户端应该看到它已经在缓存中有一个键,用于使用该键参数的查询。
查询如下:
query Shells($first: Int = 5, $offset: Int = 0, $filter: ShellFilter) {
shells(
orderBy: [STATUS_ASC, EXECUTION_FROM_DESC]
first: $first
offset: $offset
filter: $filter
) {
nodes {
...ShellData
}
totalCount
}
}
apolloClient 配置是这样的:
const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
shells: {
keyArgs: ['filter'],
merge: (existing, incoming) => {
console.log('existing:', existing, 'incoming:', incoming);
return mergeObjectsAndNestedArrays<ShellsConnection>(
existing,
incoming,
'nodes',
);
},
},
},
},
},
})
以及显示它的组件:
const ControlCenter = () => {
const { showModal } = useModalContext();
const [page, setPage] = useState(1);
const { data, loading, fetchMore } = useShellsQuery();
const [query, setQuery] = useURLQuery();
const onCounterpartiesChange = async (counterparties) => {
await fetchMore({
variables: {
filter: { shellCounterParties: { some: { orgId: { in: '20584' } } } },
},
});
setQuery({ counterparties });
};
const shells = data?.shells?.nodes;
console.log('hello from shells:', shells);
这些是日志:
编辑 1 - 文档引用
遵循文档:https://www.apollographql.com/docs/react/pagination/key-args/#setting-keyargs
任何参数都可以用作 keyArgs:limit
、offset
和 filter
。
最佳答案
在文档示例中,用作键的 arg 是原始值,但在您的情况下,过滤器 arg 是一个对象。这可能会导致 apollo 将所有结果视为相同的缓存版本。如果您的数据仅取决于 orgID,我认为您可以尝试 nested array将该字段设置为键的符号。
keyArgs: ["filter", ["shellCounterParties", ["some", ["orgId", ["in"]]]]]
keyArgs: (args, context) => args.filter.shellCounterParties.some.orgId.in
如果你真的需要根据整个过滤器对象进行缓存,我想最简单的方法就是将它字符串化
keyArgs: (args, context) => JSON.stringify(args.filter)
但要确定 apollo 是如何缓存数据的,我强烈建议您尝试 apollo devtools
相关:https://github.com/apollographql/apollo-client/issues/7314
关于reactjs - Graphql - Apollo Client/React - 缓存 - fetchMore 不更新查询的数据状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73266671/
我有一个无限滚动列表。我最近更新到最新的 Apollo 客户端,发现无限滚动不再有效。 经过深入调查。我注意到当我使用递增的跳过调用 fetchmore 时,它会导致整个页面重新呈现。有任何想法吗
我正在尝试使用 React Apollo ( https://www.apollographql.com/docs/react/data/pagination/#cursor-based ) 遵循基于
bounty 2 天后 到期。此问题的答案有资格获得 +100 声誉赏金。 Nizar Zizoune想要奖励现有答案: Bravo to me for solving it, even thoug
我在理解阿波罗时遇到问题 fetchMore方法。我是这个工具的新手,example in doc对我来说太复杂了。我在互联网上搜索,但找不到更多(当前)示例。你能帮我写一个无限加载数据的最简单的例子
我有一个来自 graphql 查询的“帖子”数组,我正在尝试使用偏移+限制进行分页,如 apollo docs 中所述。 。当前按“loadMore”会更新查询,并且 fetchMoreResult
是否可以使用 Apollo 同时运行多个 fetchMore? 我有一个相对复杂的钩子(Hook),它运行两个查询,并在一个数组中返回这些查询的结果,如下所示: export const useDas
我很想知道当具有增量数据获取的主数据模型位于代理或代理链后面时, View 会如何表现。 像取消分组代理(示例:http://lynxline.com/jongling-qt-models/)这样的项
我尝试在我的 React 应用程序中使用 apollo 客户端实现缓存分页。 我的查询有过滤器参数,它应该是在缓存对象中创建新键的唯一参数。 出于某种原因,当 fetchMore 发生时指定了过滤器,
我正在学习 apollo 和 graphQL,并且有一个简单的应用程序,它显示一个数据列表,带有一个添加到数组的 fetchMore 函数。 我想删除 updateQuery,因为它已被弃用并使用新的
我是 Apollo Client 的新手,我正在尝试为我的产品列表实现分页。但我不明白为什么 merge 函数中的 existing 参数总是返回空。每次调用 fetchMore 时,我的 incom
内部渲染: console.log('render', User.length, loading, networkStatus) 当我打电话时data.fetchMore两次,我得到: rende
我的类 DataTable 派生自 QAbstractTableModel。它在内部使用 QSqlTableModel 对象从数据库表中获取数据。它代表 db 中每一行的记录(它做的更多,但记录计数始
我是一名优秀的程序员,十分优秀!