readQuery crashes if there is nothing in the store
See original GitHub issueIntended outcome:
I want to check whether or not the apollo cache(store) has a watchQuery registered for a query name (ex: get_comments). I want to use readQuery (or a similar method?) to check the store and if there is nothing, I will get an undefined or null, or -1 etc. Something to indicate that there s no watchQuery exists under that query name.
Actual outcome:
When I try to read something from cache via readQuery, the app crashes if the query doesn’t match. The error I get is below:
ERROR Error: Uncaught (in promise): Error: Can't find field collections on object (ROOT_QUERY) undefined.
I can’t catch this error, since the return of the readQuery is not a promise or observable bur just a simple object. This error continues even if I convert the return to Observable via Observable.of(…readQuery())
How to reproduce the issue:
Just try to readQuery a random query name (ex: xwyz_xxx) and you should be able to get the error (which I couldn’t managed to catch before it crashes the app)
Please let me know if I am missing something completely. I am not very experienced in this repo. Thanks for the help.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:13
- Comments:24 (4 by maintainers)
Top GitHub Comments
Would it be alright if readQuery returned null with the helpful error message? Am i the only one not a fan of try/catching all my readQuerys?
@helfer the design of this to be an error state seems suspect. Particularly since there is no guarantee that the cached data would exist for a related query at the time say a mutation attempts to update records related to it which may only load on certain pages of a website. A site of any complexity is going to regularly run into this all the time because there is no predictability that any related queries have been cached yet.
Unwinding stacks or silencing errors (which may unintendedly silence more than the target error) is expensive and issue-prone. This problem could be solved without creating a breaking change by either adding a new method such as hasQuery() or by adding in an option on DataProxyReadQueryOptions like throwErrorOnNotFound=false to retain existing behavior but provide the option to avoid try/catch & wasteful stack unwinding or internal error handling logic for this condition.