-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Using useQuery with pollInterval triggers onCompleted only once #5531
Comments
Have you checked the network traffic? I believe that it does not actually poll, as opposed to executing onComplete only on the first query. |
@ajhool I have just tested it again and I can confirm that the query is being called multiple times. I can see it in the network traffic every second, but the |
Yeah i'm seeing the same thing.
|
+1 Having same problem here. onCompleted will be fired only once at the first fetch. |
+1 Im also seeing the same issue |
Setting |
This could be the culprit? apollo-client/src/react/data/QueryData.ts Line 433 in 6bc9fdc
It seems onCompleted only runs when data has changed, so if you are polling and no data changes occur then it will not fire. Ideally there could be an extra prop to always call. Or maybe an alternative function prop, |
A workaround for our case was to add
Obviously this means you will bypass the client-side cache but it will ensure the completion hook is triggered every time. |
We've tried the workarounds here but none work. We don't see const messagesQuery = useQuery(GET_CHAT_MESSAGES_BY_GROUP_ID, {
variables: { chatGroupId },
pollInterval: 1000,
onCompleted: () => console.log('If this worked no useEffect needed. 😕'),
}); |
I'm running into this issue as well. Even with |
The same is true for |
Can confirm as well |
Same behaviour here |
Guys, set the fetchPolicy: 'network-only', it should work then, I had the same problem. And better switch to: useLazyQuery instead of polling if it is possible. |
Erm sorry but that's subjective. I need to store it in the cache since it's very expensive for me to re-fetch it. I need polling since I'm using it for an async server operation that has the results ready in between 30secs and 2 minutes, thus I need to continuously "check" if they are ready for serving |
Also, @dominik-myszkowski , setting |
Are any of you falling in to this conditional? Is it not firing when no data has changed? |
@bhishp I would really love the onCompeted event only to run on data changed. however even when the data changes, I cannot see the onCompeted method triggered |
This worked for me. Setting 'network-only' did not. |
@hwillson any updates on that with the release of 3.0? |
@andreasonny83 I'm seeing the same, the data changed but my onComplete didn't fire. |
The issue with setting |
@jure Yes, but as @andreasonny83 mentioned and I've also confirmed, it isn't firing when the data changes. |
Right, absolutely, it should! That's the bug. I've commented merely to point out that setting |
For what it's worth, I've sort of resolved the issue caused by this workaround for the time being by chucking the polling {
cache: new InMemoryCache({
typePolicies: {
Manuscript: {
fields: {
_currentRoles: {
read(existing, { cache, args, readField }) {
const currentRoles = currentRolesVar()
},
},
},
},
},
} It's quite the detour, but it works, so hopefully it's useful for someone else too. |
Yup, can confirm. Still happening in The only other alternative I can come up with is using a |
This has proven to be the most reliable combination for us. note the separate user queries and the manual starting and stoping of polling. this bypasses a second bug in which stop polling does not reliably work with pollinterval
|
This should now be resolved in |
I don't think this is fixed @hwillson Here's a codesandbox repro. Making Should this be re-opened? |
Same, I just tried with version 3.5.6, still not working. |
I have the same problem with latest version (for me it happens both on |
This issue persists in It's baffling how issues like this can go multiple years without being addressed in such a widely used library... And this is not the first one I've seen. I suggest reopening this issue. |
I confirmed that the issue persists in the current latest version My work around to called onCompleted and solved this issue. It was to implement a timer or
|
@akikoskine, @brainkim, can we reopen this issue? |
same issue, in addition to onCompleted, my react table is not reflecting changes even though i see the calls in network. |
My work around:
|
Hi all - after taking a look at @3nvi's codesandbox (thanks for providing that!), However, if the selection set observed by your query is being updated in your cache, If anyone has any feedback on |
Glad to hear that you're looking into this @alessbell! Not being able to reliably subscribe/listen for (refetches/polling) responses via function useQueryVariation(...) {
const context = React.useRef()
context.current = {
onCompleted(data) {...}
}
const query = useQuery(..., {
onCompleted: context.current.onCompleted,
})
return {
...query,
refetch: React.useCallback(() => {
return query.refetch().then(result => {
context.current.onCompleted(result.data)
return result
})
}, [query.refetch])
}
} To me this doesn't feel like a maintainable solution (the workaround doesn't capture refetches triggered in other ways) but rather like trying to reverse an implementation detail. The name |
@alessbell @hwillson @ajhool fetchPolicy is Simultaneously Network call goes (Because fetchPolicy is Additional: Please check |
Bumping this because I ran into it. I was using The fact that |
I'm on an old version of Apollo Client so some of this may not be relevant to the current code, but when running into this I also found that when using polling if I added a |
Intended outcome:
I would expect the
onCompleted
callback to be fired after every poll.Actual outcome:
onCompleted
is being fired only once. First time the query is made.How to reproduce the issue:
Versions
npmPackages:
@apollo/react-common: ^3.0.1 => 3.0.1
@apollo/react-hooks: ^3.0.1 => 3.0.1
apollo-cache-inmemory: ^1.3.5 => 1.3.11
apollo-client: ^2.6.4 => 2.6.4
apollo-link: ^1.2.3 => 1.2.4
apollo-link-context: ^1.0.10 => 1.0.10
apollo-link-error: ^1.1.1 => 1.1.2
apollo-link-http: ^1.5.5 => 1.5.7
apollo-link-logger: ^1.2.3 => 1.2.3
apollo-server-koa: ^2.1.0 => 2.2.4
apollo-utilities: ^1.3.2 => 1.3.2
react-apollo: ^2.2.4 => 2.3.2
The text was updated successfully, but these errors were encountered: