Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination using useSuspenseQuery and React transition is broken #11315

Closed
pvandamme opened this issue Aug 28, 2023 · 11 comments · Fixed by #11638
Closed

Pagination using useSuspenseQuery and React transition is broken #11315

pvandamme opened this issue Aug 28, 2023 · 11 comments · Fixed by #11638

Comments

@pvandamme
Copy link

pvandamme commented Aug 28, 2023

I have a paginated query that I use with useSuspenseQuery.
When I call the fetchMore function inside a startTransition, an unexpected re-render with only the new data happens before the final re-render with all the data merged together.

I've made a reproduction link so you can try it out : https://stackblitz.com/edit/stackblitz-starters-fqmtck?file=src%2Fcomponents%2FPaginatedQuery.tsx

Reproduction steps

1 - Go to the stackblitz link
2 - Open your console
3 - Click "Fetch More"
4 - Look at logs

@pvandamme pvandamme changed the title Pagination using useSuspenseQuery and react transition is broken Pagination using useSuspenseQuery and React transition is broken Aug 28, 2023
@pvandamme pvandamme changed the title Pagination using useSuspenseQuery and React transition is broken Pagination using useSuspenseQuery is broken Aug 28, 2023
@pvandamme pvandamme changed the title Pagination using useSuspenseQuery is broken Pagination using useSuspenseQuery and React transition is broken Aug 28, 2023
@alessbell
Copy link
Member

Thanks for reporting this, @pvandamme! Really appreciate the reproduction - looks like this issue should be transferred over to the Apollo Client repo which I will do shortly.

@pvandamme
Copy link
Author

Thanks @alessbell :)

@pvandamme
Copy link
Author

Hi @alessbell, any issue that I can follow on the apollo client repo for updates ?
Thanks !

@none23
Copy link

none23 commented Oct 23, 2023

Faced a similar issue.
Seems to be caused by this line

@phryneas phryneas transferred this issue from apollographql/apollo-client-nextjs Oct 24, 2023
@phryneas
Copy link
Member

Hi there, I'm sorry - it seems that something went wrong when transferring this earlier. I transferred it now.

@finkef
Copy link

finkef commented Feb 27, 2024

Is there any workaround available currently?

@none23
Copy link

none23 commented Feb 27, 2024

@finkef
Well, with @apollo/client v3.9.5, deleting line 243 in node_modules/@apollo/client/react/internal/cache/QueryReference.js seems to fix the issue for me.
But I wouldn't do it in an app I care about)

@finkef
Copy link

finkef commented Feb 27, 2024

@none23 Thanks!

I just came up with this workaround, but wouldn't expect the best performance. It appears that readQuery always gets the correct data.

import { useApolloClient } from "@apollo/client"
import { useSuspenseQuery as useApolloSuspenseQuery } from "@apollo/experimental-nextjs-app-support/ssr"
import { startTransition, useCallback, useMemo } from "react"

export const useSuspenseQuery: typeof useApolloSuspenseQuery = (
  ...args: Parameters<typeof useApolloSuspenseQuery>
) => {
  const client = useApolloClient()
  const { data, fetchMore, ...rest } = useApolloSuspenseQuery(...args)

  const newFetchMore = useCallback(
    (...fetchMoreArgs: Parameters<typeof fetchMore>) => {
      return new Promise((resolve, reject) => {
        startTransition(() => {
          fetchMore(...fetchMoreArgs)
            .then(resolve)
            .catch(reject)
        })
      })
    },
    [fetchMore],
  )

  const actualData = useMemo(() => {
    return client.readQuery({
      query: args[0],
      variables: (args as any)[1]?.variables,
    })
  }, [data])

  return {
    data: actualData,
    fetchMore: newFetchMore,
    ...rest,
  } as any
}

@jerelmiller
Copy link
Member

Hey @pvandamme 👋

I finally got around to fixing this issue with #11638. I've tried this with your reproduction and this solves it.

Would you mind trying this snapshot release to make sure it works on your end?

npm i @apollo/client@0.0.0-pr-11638-20240304221457

Note: You'll need a more modern version of the Next.js app support package since it uses some internals of Apollo Client from newer versions.

Copy link
Contributor

github-actions bot commented Mar 5, 2024

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.

Copy link
Contributor

github-actions bot commented Apr 7, 2024

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
For general questions, we recommend using StackOverflow or our discord server.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.