-
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
keyFields mismatch with query leads to hard Invariant Violation crash #6673
Comments
I would challenge your assumption that this kind of mistake should be "non-fatal." If an ID can't be computed for some instances of an object type, those deficient objects will effectively not be compatible with the rest of the objects of that type in your data graph. That seems like a pretty serious problem that you would want to catch (ideally in development) and fix! I agree that it should be possible to catch and handle this kind of error at runtime (and I would have thought that was how it worked—so there's definitely a bug if that's not the case), but it shouldn't be something you can easily ignore. |
I'm completely ok with this mistake causing an exception which I (or my framework, e.g. Next.js) would have to catch explicitly. I'm definitely NOT ok with this error bringing my entire production website down, as it happened today :( Actually, I believe no kind of code except for syntax errors should have this effect, and I'm really strugging to understand why it's acceptable to intentionally write |
Ok, I think we agree on all of that. We will dig into this and make sure that we're providing appropriate |
I'm just burning through my second day of upgrading from apollo client v2 to v3. Just came here to say that what you did with apollo client v3 wasted so many hours of so many people with zero new functionality. I still didn't manage to get v3 to fully work, and the above error is just one of the errors with this major version upgrade. Please consider making less breaking changes for the future major update. This is just too much... |
@vfonic If you don't know why you're upgrading ("zero new functionality" is absurd, though I assume you just mean you don't see value in any of the new functionality), any amount of migration effort is going to feel like a chore. We are not going to slow down the pace of development, but we will continue to have extended prerelease periods before major releases, allowing anyone to try out what we're planning. The beta/RC period for AC3 was nearly a year long! Here's the current v3.2.0 PR, with beta releases you can already try (though it's just a minor release). More feedback on the consequences of any breaking changes (especially if they were not intended to be breaking) is always welcome. And if you're happy with AC2, maybe try taking a more gradual approach to updating to AC3? I realize it's a lot, but we really want you to end up appreciating the value of what you're doing, rather than getting frustrated. |
Thanks for kind words. I appreciate it! I'm just trying to keep up the pace so I don't end up stuck in some prehistoric version with borderline impossible upgrade path. I added a CSS-in-JS package and it didn't play nicely with the other packages I had so I tried to upgrade whichever packages stopped working or started having issues (in the process I also upgraded typescript to v4 so that probably broke quite some things). One such package was apollo-client so I chose to upgrade it. In the end, after I got it to work, I realized the new cache improvements, that I read about, made it slower (at least when using One last thing and then I'll finish my blabering: a simple codemod could make this shuffling between v2 and v3 much easier. (apollo-client => @apollo/client, react-apollo => @apollo/client, graphql-tag => @apollo/client). These are the only changes I've read about and noticed while upgrading. I understand there must be so many changes, but as a user, I didn't see them. I prefer Angular v50 (or React Native), than @apollo/client v3 that has so many changes that it's too difficult to upgrade (AngularJS => Angular). Perhaps having beta releases for a year long is not for the better? |
@vfonic I'm glad you mentioned this, because we do have an automated transform that can handle most of the |
I've just had this issue. If the schema of a query changes with what was in cache, the whole app blows with this error. Is there any way to catch this? Even if this is a fatal error there's still a chance to ignore the cache and fetch again before going nuke on the app. Right? |
The big issue with this is how difficult it is to track down the offending query as the stack trace and message give no clue as to which one it is except for the field name. |
have the same issue, it just does not tell you at all where this is coming from. it just crashes the whole server (because of SSR). Edit: i brute force checked every Query, but i did not found any query where the field is missing. Also i checked the cache an every object there has the field i want to use |
I was facing similar issue, this happened to me because I was using "id" + "column B" as the primary key, however during fetching, I only fetched "id", after also fetched "column B", error has gone. |
I'm facing similar issue. Is there something to do with Schema |
Yeah, same error |
almost a year since this was reported, still no update? |
A better error message would be really nice here. |
Same here. Is there any workaround to at least debug what's the offending entity? As far as I can tell, all of them have the keyFields. |
Could be as simple as adding the apollo-client/src/cache/inmemory/policies.ts Line 1092 in e5c411a
I did that locally to find out the problem. |
Should help with `cache.identify`-related cases of issue #6673.
A couple updates:
Thanks for everyone's patience. Please have a look at those PRs if you're curious/impatient! |
Looks good, thanks for the updates! In case you are curious, my use case was using Contentful cms. Each content model inherits from Entry, so I added:
Then when querying for some Catching errors early is good, but having changes on the CMS blow up the site would be no good. So thanks for removing the exception! |
Should help with `cache.identify`-related cases of issue #6673.
#8679) * Make `cache.identify` return undefined instead of throwing, to help with `cache.identify`-related cases of issue #6673. * Improve error message when `computeKeyFieldsObject` throws: #6673 (comment)
I get that this truly is a "fatal" sort of error, but when this happens in a |
This error message is ridiculously useless, unless you've only got 1 or 2 entities. I've got many, and all their keyFields are "id"! PLEASE add the entity name to the error message, please, pretty pretty please... 🥺 As a workaround, I did this (currently line 1813 of node_modules/@apollo/client/cache/cache.cjs.js): |
@benjamn nice job on this, it's now very easy to figure out how to resolve these when they occur! A couple additional questions: 1. Is there any way to handle/catch these invariant violations? It would be great to be able to show error UI, do reporting, etc. Ideally they should never happen in prod, but in practice it can be hard to know you've tested every possibility in highly polymorphic queries (e.g. an interface that's implemented by dozens of concrete types). 2. Could there be an option to mimic This could also be the default behavior when catching and not re-raising one of these invariant errors. Like, I might some queries where I just want to opt out of caching behavior for some types.. Let's say I'm trying to get a list of all commenters on a blog post. I'd love to be able to do like |
Intended outcome:
Specifying unknown fields in
keyFields
(or failing to ask for allkeyFields
in queries) should result in a catchable exception which doesn't affect the entire server process.Actual outcome:
When I pass an unknown field name to
keyFields
in InMemoryCache configuration, the entire node process crashes down withInvariant Violation
error.Here's the stacktrace I'm getting with the repo below:
My attempts to pin down this problem lead me to
setTimeout
call in zen-observable's hostReportError and to invariant(...) call in computeKeyObject in apollo-client.How to reproduce the issue:
Here's my repo which reproduces the problem: https://github.com/berekuk/bug-apollo-keyFields-crash
It uses Next.js since that's how I discovered this issue and since I didn't want to implement a custom Express.js server just for this bugreport.
I also tried to reproduce it with a simple node script which used
apolloClient.watchQuery
but didn't succeed.useQuery
probably does something differently, but I'm not sure what exactly.Versions
See https://github.com/berekuk/bug-apollo-keyFields-crash/blob/master/package.json:
PS: Besides this specific issue, I'd very much prefer for apollo-client never to do something like this on non-fatal errors. I'm worried, though, because it seems like apollo-client calls
invariant()
quite often, and though I'm not familiar with ts-invariant and zen-observable APIs, it probably behaves like a hard non-easily-catchable assertion in all those cases (?).The text was updated successfully, but these errors were encountered: