FetchMore : Request executed twice every time
Asked Answered
V

5

8

I am trying to implement pagination in a comment section.

I have normal visual behavior on the website. When I click the get more button, 10 new comments are added.

My problem is the request is executed twice every time. I have no idea why. The first time, it is executed with a cursor value, the second time without it. It seems that the useQuery hook is executed after each fetchMore.

Any help would be appreciated. Thanks!

component :

export default ({ event }) => {
  const { data: moreCommentsData, fetchMore } = useQuery(getMoreCommentsQuery, {
    variables: {
      id: event.id,
    },
    fetchPolicy: "cache-and-network",
  });
  const getMoreComments = () => {
    const cursor =
      moreCommentsData.event.comments[
        moreCommentsData.event.comments.length - 1
      ];
    fetchMore({
      variables: {
        id: event.id,
        cursor: cursor.id,
      },
     updateQuery: (prev, { fetchMoreResult, ...rest }) => {
        return {
          ...fetchMoreResult,
          event: {
            ...fetchMoreResult.event,
            comments: [
              ...prev.event.comments,
              ...fetchMoreResult.event.comments,
            ],
            commentCount: fetchMoreResult.event.commentCount,
         },
        };
      },
    });
  };
  return (
    <Container>
      {moreCommentsData &&
      moreCommentsData.event &&
      moreCommentsData.event.comments
        ? moreCommentsData.event.comments.map((c) => c.text + " ")
        : ""}

      <Button content="Load More" basic onClick={() => getMoreComments()} />
    </Container>
  ); 
};

query :

const getMoreCommentsQuery = gql`
  query($id: ID, $cursor: ID) {
    event(id: $id) {
      id
      comments(cursor: $cursor) {
        id
        text
        author {
          id
          displayName
          photoURL
        }
      }
    }
  }
`;
Varus answered 2/3, 2021 at 14:38 Comment(0)
V
16

Adding

nextFetchPolicy: "cache-first"

to the useQuery hook prevents making a server call when the component re renders.

This solved my problem.

Varus answered 5/3, 2021 at 8:26 Comment(3)
This seems to be an Apollo bug, it's already fixed in a PR and will be hopefully available soon, see: github.com/apollographql/apollo-client/issues/6916Salcedo
not working at all, I don't know why this accepted.Illaffected
Thanks, it works for me, but it must be a bug in Vue Apollo.Conlon
P
0

Could it be that the second request you are seeing is just because of refetchOnWindowFocus, because that happens a lot...

Pinsky answered 5/12, 2021 at 17:34 Comment(0)
S
0

I had a similar problem and I solved it by changing the fetchPolicy to network-only.

Saar answered 24/9, 2022 at 20:12 Comment(0)
B
0

Please note, if you are hitting this issue with @apollo/client v3.5.x there was a bug related to this which was fixed from v3.6.0+. That commit solved the duplicated execution issue in my case.

Boorish answered 30/9, 2022 at 14:20 Comment(0)
H
-1

It only makes one request if you are worried about that but the react component refreshes but not rerender each time an item in the useQuery hook changes.

Take for instance it would refresh the component when loading changes, making it seem like it sends multiple requests.

Holofernes answered 4/3, 2021 at 2:0 Comment(1)
It does make two requests. My backend is called 2 times, I have console.logs in the backend.Varus

© 2022 - 2024 — McMap. All rights reserved.