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

nextFetchPolicy function is not called sometimes #11365

Closed
jeel2308 opened this issue Nov 16, 2023 · 10 comments · Fixed by #11626 or Shoutzor/frontend#138 · May be fixed by GregBrimble/workers.sh#585, thanhhoann/PokeBuy#147 or basilisSam/Github-Issues#323
Assignees

Comments

@jeel2308
Copy link

jeel2308 commented Nov 16, 2023

Issue Description

I am using latest version of apollo client v3.8.7.
I am providing default nextFetchPolicy function to update fetchPolicy, if query has fetchPolicy of cache-and-network. According to the docs, nextFetchPolicy function should be called after request is done(reason after-fetch) and when variables are changed (reason variables-changed).
However, nextFetchPolicy function is not called after variables are changed(with reason variables-changed). It is only called when request is done.
Here is the code snippet that I am using,

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: httpLink,
  defaultOptions: {
    watchQuery: {
      fetchPolicy: "cache-and-network",
      notifyOnNetworkStatusChange: true,
      nextFetchPolicy: (
        currentFetchPolicy,
        { reason, initialFetchPolicy, observable }
      ) => {
        if (reason === "variables-changed") {
          return initialFetchPolicy;
        }

        if (
          reason === "after-fetch" &&
          ["cache-and-network"].includes(currentFetchPolicy)
        ) {
          return "cache-only";
        }

        return currentFetchPolicy;
      }
    },

    query: {
      fetchPolicy: "network-only"
    }
  }
});

Issue is solved if I comment out code inside after-fetch condition, but it limits usability of nextFetchPolicy function.

Link to Reproduction

https://codesandbox.io/p/sandbox/eloquent-cori-2jngr7?file=%2Fsrc%2Findex.jsx

Reproduction Steps

  • Change values from dropdown and check logs in console.

  • Comment out code inside after-fetch condition in nextFetchPolicy function and check logs again.

  • Current outcome:

    • NextFetchPolicy function is called with reason 'after-fetch' whenever request is completed and not called when variables are changed(with reason variables-changed).
  • Intended outcome:

    • NextFetchPolicy function should be called whenever variables are updated. Then it should be called again, if variable change triggers request with reason after-fetch.
@bignimbus
Copy link
Contributor

Hi @jeel2308 👋🏻 thanks for opening this issue!

Your reproduction includes the use of the graphql higher-order component, which is deprecated as of 2020 (see the relevant docs page). Have you seen this behavior with the currently-supported React API?

@jeel2308
Copy link
Author

Hi @bignimbus, thanks for your reply.

I have updated reproduction to use useQuery instead of graphql . Issue is still observed.

@github-actions github-actions bot removed the 🏓 awaiting-contributor-response requires input from a contributor label Nov 17, 2023
@bignimbus bignimbus added the 🏓 awaiting-team-response requires input from the apollo team label Nov 17, 2023
@jeel2308
Copy link
Author

jeel2308 commented Dec 20, 2023

Any update on this?

@phryneas
Copy link
Member

Hi Patel,
I've taken a look, and this is caused by these lines:

// If we're changing the fetchPolicy anyway, don't try to change it here
// using applyNextFetchPolicy. The explicit options.fetchPolicy wins.
options.fetchPolicy === oldFetchPolicy

I'll change this to a bug with a reproduction, but right now I can't give you a timeframe on a fix here, as we are in the middle of finalizing Apollo Client 3.9 right now.

@jeel2308
Copy link
Author

Hi, any updates on this? If you are busy on upcoming release, then can I raise PR for it?

@phryneas phryneas self-assigned this Feb 16, 2024
@phryneas
Copy link
Member

I'll try to take a stab at this next week :)

@phryneas
Copy link
Member

phryneas commented Feb 27, 2024

@jeel2308 you can give the PR release in #11626 a stab if it solves your problem :)

@jeel2308
Copy link
Author

Sure. I will have look. Thank you for the update

Copy link
Contributor

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

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 Aug 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.