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

Ser*_*ity 5 javascript reactjs graphql react-apollo apollo-client

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

缓存文件

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

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

export default cache;
Run Code Online (Sandbox Code Playgroud)

会话查询

就我而言,像 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}
`;
Run Code Online (Sandbox Code Playgroud)

对话.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>
Run Code Online (Sandbox Code Playgroud)

我能知道我错过了什么吗?

Sma*_*kid 1

应该first, after, before, last被声明为 的参数conversation而不是 的属性params

当查询参数包含after/before时, Apollo 会合并之前的页面。

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) {
    ...
    }
}

Run Code Online (Sandbox Code Playgroud)