Skip to content

Codegen in --watch mode crashes on fatal errors #1920

@mdnorman

Description

@mdnorman

Intended outcome:

When using apollo client:codegen --watch, I expect that I can "set and it and forget it" to watch files, attempt to generate code for any new or changed gql, and try again after a file changes if it encounters any fatal errors in the last run.

Actual outcome:

When writing code, sometimes there are typos or compilation errors that cause apollo client:codegen --watch to crash completely, meaning that it is no longer watching any files. This typically happens after changing the name of a query within gql or copying gql from one place to another.

It's frustrating because I don't keep the watcher up all the time, and I only notice when I don't get new generated types even after all the code is correct again. It happens so often that I end up having to babysit it, which means that I may as well just run apollo client:codegen without --watch every time I feel like the code is in a good place.

The following is just one example of the type of error that causes a crash, but I'm guessing there are a class of errors like this one that are not caught by the watch loop.

some-project/node_modules/apollo-language-server/lib/project/base.js:126
                        throw new Error(`️️There are multiple definitions for the \`${definition.name.value}\` operation. Please rename or remove all operations with the duplicated name before continuing.`);
                        ^

Error: ️️There are multiple definitions for the `SomeQuery` operation. Please rename or remove all operations with the duplicated name before continuing.
    at GraphQLClientProject.checkForDuplicateOperations (some-project/node_modules/apollo-language-server/lib/project/base.js:126:31)
    at GraphQLClientProject.documentDidChange (some-project/node_modules/apollo-language-server/lib/project/base.js:116:14)
    at GraphQLClientProject.fileDidChange (some-project/node_modules/apollo-language-server/lib/project/base.js:101:14)
    at Gaze.<anonymous> (some-project/node_modules/apollo/lib/commands/client/codegen.js:112:30)
    at Gaze.emit (events.js:311:20)
    at Gaze.emit (some-project/node_modules/gaze/lib/gaze.js:129:32)
    at Gaze.<anonymous> (some-project/node_modules/gaze/lib/gaze.js:430:18)
    at Timeout._onTimeout (some-project/node_modules/gaze/lib/gaze.js:458:24)
    at listOnTimeout (internal/timers.js:549:17)
    at processTimers (internal/timers.js:492:7)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! some-project@0.0.0 apollo-codegen:watch: `apollo client:codegen --target typescript --watch`

Process finished with exit code 1

How to reproduce the issue:

  1. Create a file with a gql query and use it in a component
  2. Copy that file into another directory
    • Observed: client:codegen --watch crashes with error above
    • Expected: client:codegen --watch should catch any errors and be ready for the next file to change

Versions

"apollo": "2.27.3"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions