Javascript 在中继分页图上滚动时合并以前的数据
我正在尝试使用中继式分页。然而,我在无限滚动上遇到了麻烦。当我滚动或加载下一组数据时,我只获取当前数据,而不将其合并到以前的数据中。我就是这样做的 cache.tsJavascript 在中继分页图上滚动时合并以前的数据,javascript,reactjs,graphql,apollo-client,react-apollo,Javascript,Reactjs,Graphql,Apollo Client,React Apollo,我正在尝试使用中继式分页。然而,我在无限滚动上遇到了麻烦。当我滚动或加载下一组数据时,我只获取当前数据,而不将其合并到以前的数据中。我就是这样做的 cache.ts import { InMemoryCache } from '@apollo/client'; import { relayStylePagination } from '@apollo/client/utilities'; const cache = new InMemoryCache({ typePolicies: {
import { InMemoryCache } from '@apollo/client';
import { relayStylePagination } from '@apollo/client/utilities';
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
conversation: relayStylePagination(),
},
},
},
});
export default cache;
会话查询
在我的例子中,像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>
const[loadConversation,{data,fetchMore,networkStatus,subscribeToMore}]=useLazyQuery(
会话
);
useffect(()=>{
isMounted.current=真;
if(信道ID和联系人ID){
负载会话({
变量:{
channel:channelId,
联系人:contactId,
参数:{first},
},
});
}
return()=>{
isMounted.current=假;
};
},[channelId、contactId、loadConversation]);
{hasNextPage&&(
{
如果(获取更多){
费特莫尔({
变量:{
参数:{
第一,
在:data?.conversation?.data?.pageInfo.endCursor之后,
},
},
});
}
}}
/>
)}
我能知道我遗漏了什么吗?首先、之后、之前、最后的
应该声明为对话的参数,而不是参数的属性
阿波罗合并了前几页
谢谢你的回复。这意味着,无法扩展relayStylePagination
以从params对象获取参数??relayStylePagination仅处理relay中已知的参数。其他参数将留给您。
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) {
...
}
}