gpt4 book ai didi

javascript - 在中继分页graphql上滚动时合并以前的数据

转载 作者:行者123 更新时间:2023-12-03 17:22:32 26 4
gpt4 key购买 nike

我正在尝试使用中继式分页。但是,我在无限滚动时遇到了麻烦。当我滚动或加载下一组数据时,我只会获取当前数据,而不会将其合并到以前的数据中。这就是我所做的
缓存.ts

import { InMemoryCache } from '@apollo/client';
import { relayStylePagination } from '@apollo/client/utilities';

const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
conversation: relayStylePagination(),
},
},
},
});

export default cache;
session 查询
在我的例子中,像 first、after、before、last 这样的参数在 params 对象中
export const CONVERSATION = gql`
query conversation($channel: ShortId, $contact: ShortId, $params: ConnectionInput) {
conversation(channel: $channel, contact: $contact, params: $params) {
status
data {
pageInfo {
...PageInfo
}
edges {
cursor
node {
...Communication
}
}
}
}
}
${PAGE_INFO}
${COMMUNICATION}
`;
对话.tsx
const [loadConversation, { data, fetchMore, networkStatus, subscribeToMore }] = useLazyQuery(
CONVERSATION,
);
useEffect(() => {
isMounted.current = true;
if (channelId && contactId) {
loadConversation({
variables: {
channel: channelId,
contact: contactId,
params: { first },
},
});
}
return () => {
isMounted.current = false;
};
}, [channelId, contactId, loadConversation]);

<React.Suspense fallback={<Spinner />}>
<MessageList messages={messages ? generateChatMessages(messages) : []} />
{hasNextPage && (
<>
<button
type='button'
ref={setButtonRef}
id='buttonLoadMore'
disabled={isRefetching}
onClick={() => {
if (fetchMore) {
fetchMore({
variables: {
params: {
first,
after: data?.conversation?.data?.pageInfo.endCursor,
},
},
});
}
}}
/>
</>
)}
</React.Suspense>
我能知道我错过了什么吗?

最佳答案

first, after, before, last应声明为 conversation 的参数而不是 params 的属性.
Apollo 合并前几页when the query arguments contain after / before .

query conversation($channel: ShortId, $contact: ShortId, $after: String, $first: Int, $before: String, $last: Int) {
conversation(channel: $channel, contact: $contact, after: $after, first: $first, before: $before, last: $last) {
...
}
}

关于javascript - 在中继分页graphql上滚动时合并以前的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66415243/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com