Synchronous Local-State Resolvers Don't Return Data Synchronously
See original GitHub issueIntended outcome:
Admittedly, this might fall into feature request territory, but here goes.
I have a local state resolver that returns something synchronously. Whenever I go to query it, though, it always returns data: undefined
at first then an instant later returns the actual data.
I suspect that this is because Apollo will always await
the result of a local resolver, even if it returns a non-thennable. This mandates an event loop tick and thus the component is rendered with an initial value of null.
Actual outcome:
See above.
How to reproduce the issue: https://codesandbox.io/s/quizzical-firefly-oyxwn?file=/src/App.js
Open the console and see that component is initially rendered with appVersion: null
.
Versions See reproduction. :^)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:10
Top Results From Across the Web
Advanced topics on caching in Apollo Client
This article describes special cases and considerations when using the Apollo Client cache. Bypassing the cache. Sometimes you shouldn't use the cache for...
Read more >Using resolved promise data synchronously - Stack Overflow
I want to know if there is any way at all to use the data from a resolved promise in 'normal' synchronous code....
Read more >A word on Angular route resolvers – and a praise for Reactive ...
The resolved data is available synchronously when the component starts. If you never heard about resolvers, you are fine!
Read more >Problem with async/await pattern -- in C# and JavaScript
This means that if I have an existing function that's synchronous, and has a return value that will remain synchronously calculated and thus ......
Read more >Asynchronous Programming - Eloquent JavaScript
In a synchronous environment, where the request function returns only after it has done its work, the easiest way to perform this task...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I appreciate your feedback, and I respectfully disagree. Your code snippet is a decent workaround but is also a fair amount of boilerplate. It also makes it hard to use hooks that depend on the data returned due to the early return rule.
One “solution” to that problem is allowing
useFoo
to take undefined, but that just starts to infect code like wildfire and is still just a workaround because data that is known synchronously isn’t returned synchronously. The workaround beyond that (which I do actually use in some places) is to have a presentational component that is rendered once all of the data is available, which I do in some places, but is even more boilerplate and redundant types I have to specify in my TS code. It feels very much like going back to the old style of Redux, which isn’t even recommended anymore (see edit at top).I adopted Apollo to simplify my state management, and I’m simply advocating for a feature that I think would make it even better.
All that being said, I’d appreciate input from an Apollo contributor rather than disagreeing about the merits of a feature with a stranger. :^)
All valid points, but not my use case.
In particular, I’m putting a bit of data inside of Apollo just for simplicity and I’d rather it be available synchronously. I could do workarounds with state and context (or… god forbid… Redux), but I’d much rather be able to manage it all from inside Apollo. I don’t want to write fallbacks that I know I won’t need.