question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subscription updateQuery triggers refetch

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
sjmeverettcommented, May 24, 2020

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
}
1reaction
fdevcommented, May 26, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refetching queries in Apollo Client - Apollo GraphQL Docs
Refetch options ; updateCache. (cache: TCache) => void. Optional function that updates cached fields to trigger refetches of queries that include those fields....
Read more >
How to refetch a query when a new subscription arrives in ...
I was wondering if there's an elegant way to trigger the refetch of a query in react-apollo when a subscription receives new data...
Read more >
Subscriptions | Vue Apollo
GraphQL subscriptions are a way to push data from the server to the ... In most cases, intermittent polling or manual refetching are...
Read more >
Subscriptions - Client (React) - Apollo GraphQL Docs
Like queries, subscriptions enable you to fetch data. ... updateQuery is a function that tells Apollo Client how to combine the query's currently...
Read more >
Update query in a trigger - mysql - DBA Stack Exchange
I am using dynamic SQL. This works fine inside the procedure. Problem is: I want to fetch the current date values into the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found