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

Generate @apollo/client/invariantErrorCodes.js for each release. #6665

Merged
merged 3 commits into from
Jul 21, 2020

Conversation

benjamn
Copy link
Member

@benjamn benjamn commented Jul 21, 2020

When an invariant fails in production, a cryptic numeric error of the form Invariant Violation: 42 is thrown, with a reference to the error codes section of invariant-packages README.md. This system was introduced in #4521 to save bundle size by stripping long error strings from production bundles, and it continues to be one of the most impactful things we've done to reduce bundle sizes.

However, this vague guidance (which was intended to suggest searching your installed node_modules/@apollo/client package for the error code) has not proven adequate in many cases, to say the least: see apollographql/invariant-packages#18, apollographql/invariant-packages#19, #6604, #5730, #5291, and #5975, to cite just a few of the many issues we've seen since #4521.

Using error codes instead of error strings remains important for production bundle sizes, but I think we can make it substantially easier to look up the error string corresponding to each error code, by generating a single file containing all the invariant error codes for each @apollo/client release.

Starting with Apollo Client 3.1.0, this manifest file can be found in @apollo/client/invariantErrorCodes.js (using an npm/yarn-installed copy of @apollo/client, since this file is generated in the ./dist directory, not checked into the repository). The file contains an explanatory comment, the @apollo/client version, and a sequential map from error numbers to the { file, node } responsible for the error.

I wish we could go back and republish old versions of @apollo/client to include this file, but at least things should be easier going forward.

When an invariant fails in production, a cryptic numeric error of the form
`Invariant Violation: 42` is thrown, with a reference to the error codes
section of invariant-packages README.md: https://github.com/apollographql/invariant-packages#error-codes

This vague guidance has not proven adequate in many cases, to say the
least: see apollographql/invariant-packages#18,
apollographql/invariant-packages#19, #6604,

Using error codes instead of error strings remains important for
production bundle sizes, but I think we can make it substantially easier
to look up the error string corresponding to each error code, by
generating a single file containing all the invariant error codes for each
@apollo/client release.

Starting with Apollo Client 3.1.0, this manifest file can be found in
@apollo/client/invariantErrorCodes.js (using an npm/yarn-installed copy of
@apollo/client, since this file is generated in the ./dist directory, not
checked into the repository). The file contains an explanatory comment,
the @apollo/client version, and a sequential map from error numbers to the
{ file, node } responsible for the error.

I wish we could go back and republish old versions of @apollo/client to
include this file, but at least things should be easier going forward.
@benjamn
Copy link
Member Author

benjamn commented Jul 21, 2020

Here's a taste of what this file looks like:

// This file is meant to help with looking up the source of errors like
// "Invariant Violation: 35" and is automatically generated by the file
// @apollo/client/config/processInvariants.ts for each @apollo/client
// release. The numbers may change from release to release, so please
// consult the @apollo/client/invariantErrorCodes.js file specific to
// your @apollo/client version. This file is not meant to be imported.
{
  "@apollo/client version": "3.1.0",

  1: {
    file: "@apollo/client/cache/inmemory/policies.js",
    node: invariant(!old || old === which, "Cannot change root " + which + " __typename more than once")
  },

  2: {
    file: "@apollo/client/cache/inmemory/policies.js",
    node: new InvariantError("Cannot automatically merge arrays")
  },

  3: {
    file: "@apollo/client/cache/inmemory/policies.js",
    node: invariant(hasOwn.call(response, responseName), "Missing field '" + responseName + "' while computing key fields")
  },

  4: {
    file: "@apollo/client/cache/inmemory/readFromStore.js",
    node: new InvariantError("Dangling reference to missing " + objectOrReference.__ref + " object")
  },

  // ... skipping a few ...

  52: {
    file: "@apollo/client/utilities/graphql/storeUtils.js",

    node: new InvariantError("The inline argument \"" + name.value + "\" of kind \"" + value.kind + "\"" +
        'is not supported. Use variables instead of inline arguments to ' +
        'overcome this limitation.')
  },

  53: {
    file: "@apollo/client/utilities/testing/mocking/mockLink.js",
    node: invariant(queryWithoutConnection, "query is required")
  }
}

Copy link
Contributor

@jcreighton jcreighton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆒 👍

@benjamn benjamn merged commit 877885c into master Jul 21, 2020
@benjamn benjamn deleted the auto-generate-invariantErrorCodes.js branch July 21, 2020 17:39
jimrandomh pushed a commit to jimrandomh/apollo-client that referenced this pull request Jul 22, 2020
…llographql#6665)

When an invariant fails in production, a cryptic numeric error of the form
`Invariant Violation: 42` is thrown, with a reference to the error codes
section of invariant-packages README.md: https://github.com/apollographql/invariant-packages#error-codes

This vague guidance has not proven adequate in many cases, to say the
least: see apollographql/invariant-packages#18,
apollographql/invariant-packages#19, apollographql#6604,

However, this vague guidance (which was intended to suggest searching
your installed node_modules/@apollo/client package for the error code)
has not proven adequate in many cases, to say the least: see apollographql/invariant-packages#18,
apollographql/invariant-packages#19, apollographql#6604, apollographql#5730, apollographql#5291, and apollographql#5975,
to cite just a few of the many issues we've seen since apollographql#4521.

Using error codes instead of error strings remains important for
production bundle sizes, but I think we can make it substantially easier
to look up the error string corresponding to each error code, by
generating a single file containing all the invariant error codes for each
@apollo/client release.

Starting with Apollo Client 3.1.0, this manifest file can be found in
@apollo/client/invariantErrorCodes.js (using an npm/yarn-installed copy of
@apollo/client, since this file is generated in the ./dist directory, not
checked into the repository). The file contains an explanatory comment,
the @apollo/client version, and a sequential map from error numbers to the
{ file, node } responsible for the error.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants