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

Open
jeel2308 opened this issue Nov 16, 2023 · 8 comments · May be fixed by #11626
Open

nextFetchPolicy function is not called sometimes #11365

jeel2308 opened this issue Nov 16, 2023 · 8 comments · May be fixed by #11626
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants