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.

readQuery crashes if there is nothing in the store

See original GitHub issue

Intended 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:closed
  • Created 6 years ago
  • Reactions:13
  • Comments:24 (4 by maintainers)

github_iconTop GitHub Comments

49reactions
abhiaiyer91commented, Apr 12, 2017

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?

36reactions
matthewerwincommented, Jul 2, 2017

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ApolloGraphQL -- ReadQuery `Field Not Found` Error?
My readQuery is returning a field not found error, even though it seems like the field is present in the cache. QUERY const ......
Read more >
readQuery, get data from cache - General - Apollo Community
I'm trying to get data from Apollo cache. I know that data is there because in Apollo dev tools specified records are available....
Read more >
How to update the Apollo Client's cache after a mutation
The Apollo Client is used to fetch data from any GraphQL server. ... readQuery would throw an error and the application would crash....
Read more >
Use Apollo to Manage the App's Local State
Also note that it if we chose not to initialize the ShoppingCart object, ... then the readQuery will throw an error, but if...
Read more >
How Query Store collects data - SQL Server - Microsoft Learn
If a new plan is created, Query Store adds the new plan entry for the query and keeps the previous ones along with...
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