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

subscribeToMore not rerendering component when watchQuery's fetchPolicy is set to no-cache #8013

Closed
evenfrost opened this issue Apr 21, 2021 · 3 comments
Labels
🔍 investigate Investigate further

Comments

@evenfrost
Copy link

Intended outcome:

Using subscribeToMore with watchQuery's fetchPolicy set to no-cache properly rerenders the component on new subscription data arrival.

Actual outcome:

Using subscribeToMore with watchQuery's fetchPolicy set to no-cache doesn't rerender the component on new subscription data arrival.

How to reproduce the issue:

// index.ts
const defaultOptions: DefaultOptions = {
  watchQuery: {
    fetchPolicy: 'network-only',
    errorPolicy: 'all',
  },
  query: {
    fetchPolicy: 'no-cache', // if set to 'network-only', it works
    errorPolicy: 'all',
  },
  mutate: {
    errorPolicy: 'all',
  },
};

const client = new ApolloClient({
  defaultOptions,
  // ...
});
// Component.tsx

  const {
    data,
    subscribeToMore,
  } = useQuery<Data>(query, {
    variables: {
      id,
    },
  });
  
  useEffect(() => {
      const unsubscribe = subscribeToMore({
        document: subscription,
        variables: { id },
        updateQuery: (prev, { subscriptionData: { data: subscriptionData } }) => {
          if (!subscriptionData) {
            return prev;
          }

          console.log('subscriptionData', subscriptionData); // logged in both scenarios

          return {
            name: {
              ...prev.name,
              ...subscriptionData.name,
            },
          };
        },
      });

      return () => unsubscribe();
  // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [])
  
// component doesn't rerender on new subscription data arrival

Versions

  System:
    OS: Linux 5.4 Ubuntu 20.04.2 LTS (Focal Fossa)
  Binaries:
    Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
    Yarn: 1.12.3 - /usr/bin/yarn
    npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
  Browsers:
    Chrome: 90.0.4430.72
    Firefox: 87.0
  npmPackages:
    @apollo/client: ^3.3.15 => 3.3.15 
    apollo-client: ^2.6.10 => 2.6.10 
    apollo-server: ^2.23.0 => 2.23.0 
    apollo-server-koa: ^2.23.0 => 2.23.0 
    apollo-utilities: ^1.3.4 => 1.3.4 
@bignimbus bignimbus added the 🔍 investigate Investigate further label Nov 21, 2022
@jerelmiller
Copy link
Member

Hey @evenfrost 👋

Thanks for your patience on a response!

updateQuery is a cache write API so this is expected behavior. Your query doesn't rerender because it doesn't respond to cache update when using the no-cache fetch policy. This would explain why switching to network-only will fix the issue.

Totally understand the confusion here since the name updateQuery is a bit misleading here. You'll have to switch to one of the other fetch policies if you want this to work as you expect. Unfortunately it is a breaking change at this point to switch this behavior.

I've noted this down as a potential for improvement in a future major. If you have ideas on how you might see this change, feel free to open a feature request so we can track it. I'm going to go ahead and close this issue since there is nothing more actionable for us to look at. Thanks!

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 Apr 19, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔍 investigate Investigate further
Projects
None yet
Development

No branches or pull requests

3 participants