apollographql / apollographql/apollo-tooling

Codegen in --watch mode crashes on fatal errors

Open
#1,920 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
3k
Forks
460
PR merge metrics
No merged PRs in 30d

Description

**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. (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. (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"`

Contributor guide

Open the contributing guide

Research direction

Start with the watch callback in apollo/lib/commands/client/codegen.js and the error path shown in apollo-language-server/lib/project/base.js. Reproduce the duplicate-operation case from the issue, then inspect how fatal errors propagate through the watcher. Done means --watch stays running after an error and retries successfully when a later file change fixes it.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
cli, developer-experience
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.