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

Subscription updateQuery triggers refetch #6340

Closed
fdev opened this issue May 24, 2020 · 6 comments
Closed

Subscription updateQuery triggers refetch #6340

fdev opened this issue May 24, 2020 · 6 comments
Assignees

Comments

@fdev
Copy link

fdev commented May 24, 2020

Intended outcome:
When receiving data from a websocket subscription, only the original useQuery data object should get modified.

Actual outcome:
When receiving data from a websocket subscription, the original query as defined in useQuery is run again as if refetch was called.

This happens each time new subscription data is received.

How to reproduce the issue:

The issue occurs with this simple setup:

const MyComponent = () => {
  const { subscribeToMore } = useQuery(MY_QUERY, {
    fetchPolicy: 'cache-and-network',
  })

  useEffect(() => {
    return subscribeToMore({
      document: MY_SUBSCRIPTION,
      updateQuery: (prev, { subscriptionData }) => {
        return subscriptionData.data
      },
    })
  }, [])

  return null
}

MY_QUERY and MY_SUBSCRIPTION are identical (except for the query and subscription prefix) and include an id for all types.

Versions

  System:
    OS: Linux 4.15 Ubuntu 18.04.4 LTS (Bionic Beaver)
  Binaries:
    Node: 12.16.2 - ~/.nvm/versions/node/v12.16.2/bin/node
    npm: 6.14.4 - ~/.nvm/versions/node/v12.16.2/bin/npm
  Browsers:
    Chrome: 83.0.4103.61
    Firefox: 76.0.1
  npmPackages:
    @apollo/client: ^3.0.0-beta.50 => 3.0.0-beta.50 
    @apollo/link-ws: ^2.0.0-beta.3 => 2.0.0-beta.3

This behavior seems to have been introduced in 3.0.0-beta.46.

@sjmeverett
Copy link

I'm pretty sure the same thing is causing fetchMore not to work either. If you have a fetchPolicy that does network fetches, the data is refetched, overwriting your updated data.

E.g. something like this:

const MyComponent = () => {
  const { fetchMore } = useQuery(MY_QUERY, {
    fetchPolicy: 'cache-and-network',
  })

  useEffect(() => {
    fetchMore({variables: {cursor: 1}})
  }, [])

  return null
}

@hwillson
Copy link
Member

Could someone provide a small runnable reproduction that demonstrates this? Thanks!

@fdev
Copy link
Author

fdev commented May 26, 2020

I suspect this issue has the same cause as #6327 and #6305, which have runnable examples.

@fdev
Copy link
Author

fdev commented May 30, 2020

This is fixed in @apollo/client@3.0.0-beta.52.

@geosigno
Copy link

This is still happening in version 3.3.11.

@DrYurii
Copy link

DrYurii commented May 21, 2021

@benjamn Well? Are you going to fix that? This bs still happens on 3.3.19 and has broken my app!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants