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

Partial data returned on error after resetting cache #11938

Closed
TuurDutoit opened this issue Jul 9, 2024 · 7 comments · Fixed by #11984
Closed

Partial data returned on error after resetting cache #11938

TuurDutoit opened this issue Jul 9, 2024 · 7 comments · Fixed by #11984
Labels

Comments

@TuurDutoit
Copy link

TuurDutoit commented Jul 9, 2024

Issue Description

In our app, useQuery sometimes returns partial data after resetting the cache (client.resetStore()) and receiving an error.

We have a React Native app that uses Apollo for most data fetching. Our Apollo client is initialized like so:

const client = new ApolloClient<any>({
  link,
  cache,
  defaultOptions: {
    watchQuery: {
      fetchPolicy: 'cache-and-network',
    },
  },
})

Note that the default errorPolicy is none and the default returnPartialData is false.

We have a home screen that fetches some data with a useQuery hook. In our settings screen, it's possible to change your language and log out, among other things. Both these actions will cause the Apollo cache to be cleared (client.resetStore), to ensure we're not saving any data from other users or data in the wrong language. This in turn causes the home screen query to refetch automatically.

If you do so, and our GraphQL API returns an error with partial data while refetching the home screen query, useQuery suddenly returns the partial data. With our Apollo client config, useQuery should never return partial data.

Successful query:

const { data, error } = useQuery(...)
// data: full response
// error: undefined

Failed query:

const { data, error } = useQuery(...)
// data: undefined
// error: Error

After client.resetStore() and error from API on refetch:

const { data, error } = useQuery(...)
// data: partial data!
// error: Error

We use @apollo/client 3.7.7 currently, but I've tried on the latest (3.10.8), with the same result. The linked example is also using the latest version.

Link to Reproduction

https://codesandbox.io/p/devbox/peaceful-hopper-mtpd9x

Reproduction Steps

Click the "Reset cache" button a few times in quick succession. For me, it always happens after 3 clicks. I'm not sure why that's needed in this simple example - in our app, a single client.resetStore() will trigger the issue.

Expected outcome

Raw data is always undefined

Actual outcome

At some point, it becomes an object, with some of the requested data (notice how name is missing, breaking the list!)

Screen recording

Screen.Recording.2024-07-09.at.16.26.35.mov

@apollo/client version

3.7.7

@jerelmiller
Copy link
Member

Hey @TuurDutoit 👋

We are in the middle of finalizing our 3.11 release and will try and take a look shortly after. Thanks for submitting the issue with a reproduction!

@TuurDutoit
Copy link
Author

Another interesting find: if I add returnPartialData: true to the useQuery in the repro, the data stays undefined!

@TuurDutoit
Copy link
Author

@jerelmiller I saw that 3.11 was released. Have you had a chance to look into this yet?

@jerelmiller
Copy link
Member

Hey @TuurDutoit 👋

I just opened #11984 which should fix the issue. Turns out it didn't have much to do with resetStore itself, but rather multiple fetches where a result would return errors with the data property set. I was able to reproduce this with refetch by itself as well. I've tried the fix with your original reproduction and the issue is fixed.

Funny enough, I was able to reproduce this with 2 refetches as opposed to 3. I suspect it took 3 clicks on your reproduction because resetStore has some code to detect if its being called while a query is in flight (which kicked in on the 2nd click and avoided the request).

Could you try the following snapshot release to see if it fixes the issue in your actual app?

npm i @apollo/[email protected]

We'll either get this out with 3.11.3 or 3.11.4.

@TuurDutoit
Copy link
Author

@jerelmiller That does seem to fix it in our app!

Copy link
Contributor

github-actions bot commented Aug 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 Sep 6, 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 Sep 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
2 participants