Skip to content
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

SSR crashes with "invariant violation" when InMemoryCache uses "possibleTypes" and "error-policy" is set to "all" #6915

Closed
ZauberNerd opened this issue Aug 26, 2020 · 4 comments

Comments

@ZauberNerd
Copy link

ZauberNerd commented Aug 26, 2020

Intended outcome:

When a GraphQL resolver fails to resolve a field, @apollo/client should render as much data as possible (with errorPolicy: all) and should populate the error field of useQuery.

Actual outcome:

The server process crashes with an "Invariant violation" error:

Invariant Violation: Missing field 'bar' in {
  "foo": "bar"
}
    at new InvariantError (~/workspace/tmp/apollo-client-ssr/node_modules/ts-invariant/lib/invariant.js:16:28)
    at ~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/cache/cache.cjs.js:791:103
    at Set.forEach (<anonymous>)
    at StoreWriter.processSelectionSet (~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/cache/cache.cjs.js:762:17)
    at StoreWriter.writeToStore (~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/cache/cache.cjs.js:711:29)
    at InMemoryCache.write (~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/cache/cache.cjs.js:1390:37)
    at InMemoryCache.ApolloCache.writeQuery (~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/cache/cache.cjs.js:53:21)
    at ~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/core/core.cjs.js:890:31
    at perform (~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/cache/cache.cjs.js:1493:17)
    at InMemoryCache.performTransaction (~/workspace/tmp/apollo-client-ssr/node_modules/@apollo/client/cache/cache.cjs.js:1508:13) {
  framesToPop: 1,
  name: 'Invariant Violation'
}

How to reproduce the issue:

  1. find / mock a response with a broken resolver
  2. set errorPolicy to all on your query
  3. define possibleTypes for the InMemoryCache
  4. wittness the crash

Here is a reproduction: https://github.com/ZauberNerd/apollo-client-ssr-reproducer

Versions

  System:
    OS: Linux 5.8 Arch Linux
  Binaries:
    Node: 12.16.0 - ~/.nvm/versions/node/v12.16.0/bin/node
    Yarn: 1.22.4 - ~/.nvm/versions/node/v12.16.0/bin/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.16.0/bin/npm
  Browsers:
    Chrome: 84.0.4147.135
    Firefox: 79.0
  npmPackages:
    @apollo/client: ^3.1.3 => 3.1.3
@ZauberNerd
Copy link
Author

Hi @benjamn @hwillson have you had some time to look into this?
Or could you give me some pointers on where to start, so that I could prepare a PR myself?

@benjamn
Copy link
Member

benjamn commented Sep 11, 2020

@ZauberNerd Since the bar field has an error, I believe it needs to be null rather than missing from the result object.

See the Errors section of the GraphQL spec, or this explanation of null in GraphQL responses from Hasura.

In other words, the following change seems to be the "right" way to fix the problem:

diff --git a/src/server.ts b/src/server.ts
index f2f67d2..54379ae 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -14,7 +14,7 @@ app.get('/', async (_, res) => {
 
 app.post('/graphql', (_, res) => {
   res.json({
-    data: { foo: 'bar' },
+    data: { foo: 'bar', bar: null },
     errors: [
       {
         message: 'Could not resolve fields',

As it happens, this particular exception used to be a warning (in AC2, before #6055). I might be open to switching back to a warning, but I think the content of the warning indicates something useful even in this case (an invalid GraphQL response).

@RadoslavGenov
Copy link

@ZauberNerd Have you found a way to mitigate this error? Did you refactor all of your resolvers? Or have you decided to not migrate yet?

@ZauberNerd
Copy link
Author

Hi @RadoslavGenov @benjamn yes, thank you for the great explanation. I have followed it and updated my GraphQL mock server, so this ticket can be closed.

benjamn added a commit that referenced this issue Jun 22, 2021
benjamn added a commit that referenced this issue Jun 22, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants