Comments (6)
That is kinda expected/intended behaviour.
useBackgroundQuery
only creates a queryRef
that a useReadQuery
can attach to. Due to how suspense works, we have to keep around that reference, even if the component with useReadQuery
would never be mounting.
If you don't attach to it in a useReadQuery
within 30 seconds (see autoDisposeTimeoutMs
), that subscription will be unsubscribed.
You'll see in your demo that once you add a useReadQuery
to the whole thing, it behaves the way you'd expect.
We have to solve it this way (with the autoDisposeTimeoutMs
) because we have to keep the subscription alive long enough for the useReadQuery
component not only to suspend/try render, but until it is actually rendered and committed to the component tree. That's unfortunately a drawback of how Suspense works in React.
from apollo-client.
Thank you for your answer!
I hadn't seen this option (autoDisposeTimeoutMs
) at all.
That's the problem I'm facing, my child component that reads the reference doesn't have time to mount.
But wouldn't it be a good idea, for performance reasons, to unsubscribe the subscription if the component that called the background query is unmounted and the reference hasn't been read?
from apollo-client.
@PiR1 I'm fairly confident this is related to #11438. An optimization we made in 3.9 with regards to background queries/query refs is what @phryneas described above, which is that if a queryRef
is not read by useReadQuery
in a given amount of time (autoDisposeTimeoutMs
), the query ref is put in sort of an idle state.
The work in #11438 however did remove the detection of the background query unmounting. If I try this with 3.8.10 for example, I see the problem you're describing disappear.
Let me think through this a bit more. I stand by that change as I believe it is generally a positive one. But you have a good point about not keeping that subscription around when useBackgroundQuery
unmounts before the autoDisposeTimeoutMs
has kicked in. Let me play around with a couple things and see if we can get the best of both worlds.
from apollo-client.
I've got a failing test that demonstrates this issue: #11651. This also occurs when changing variables on useBackgroundQuery
without consuming the queryRef
before the autoDisposeTimeoutMs
kicks in.
I tried a couple quick ideas without luck. I think this is going to require a bit more thought to try and nail all the cases down here and work well with strict mode. If you've got ideas and would like to contribute, feel free to use the tests in my PR as a base 🙂
from apollo-client.
Thank you for the update!
I'll try to have a look on my side too, thanks to the tests you've done.
from apollo-client.
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.
from apollo-client.
Related Issues (20)
- `useQuery` API documentation anchor links are all `#undefined` HOT 4
- `useFragment`, returns false data. HOT 6
- useFragment accepts a generic type, but it converts it into a DeepPartialObject. what is the best usage in here to avoid using DeepPartialObject? HOT 5
- Pagination using useSuspenseQuery gets empty array instead of cached array when api response with empty array HOT 4
- Merging array in DeepMerger HOT 3
- RFC: Schema-driven testing HOT 6
- Infer `connectToDevTools` regardless of whether another client has connected or not HOT 2
- startTransition has no affect on refetch or fetchMore with errorPolicy: 'all' HOT 4
- [Testing utilities] Example with MSW HOT 4
- [Testing utilities] Custom fetch implementation via context HOT 5
- [Testing utilities] Performance test against a large schema HOT 3
- Again, onCompleted is not called when useLazyQuery HOT 8
- Any callback function on a global level or in a mutation to collect optimistic response failures? HOT 7
- refetch from useSuspenseQuery doesn`t return the updated data with fetchPolicy: 'cache-first' HOT 4
- Issue with useSuspenseQuery in Next.js Client Component: Duplicate Queries on Server and Client HOT 6
- @client on arguments HOT 9
- [Docs] Add some documentation on how to test `preloadQuery`
- [Data masking] Provide testing utility to provide proper format to `from` option in `useFragment`
- Typescript error with RequestInfo HOT 5
- Paginated read functions is not returning updated args when using fetchMore HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-client.