dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.78k stars 1.31k forks source link

codegen/cli `generate` with `watch` bug #9490

Open ghost opened 1 year ago

ghost commented 1 year ago

Which packages are impacted by your issue?

@graphql-codegen/cli

Describe the bug

When using watch in the generate call from @graphql-codegen/cli you will get an error:

node:internal/errors:478
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_INVALID_ARG_TYPE]: The "to" argument must be of type string. Received undefined
    at new NodeError (node:internal/errors:387:5)
    at validateString (node:internal/validators:162:11)
    at relative (node:path:1192:5)
    at makeGlobalPatternSet (/home/tomk/git/ah/ah-checkout/web/node_modules/@graphql-codegen/cli/cjs/utils/patterns.js:103:33)
    at createWatcher (/home/tomk/git/ah/ah-checkout/web/node_modules/@graphql-codegen/cli/cjs/utils/watcher.js:28:69)
    at generate (/home/tomk/git/ah/ah-checkout/web/node_modules/@graphql-codegen/cli/cjs/generate-and-save.js:100:47) {
  code: 'ERR_INVALID_ARG_TYPE'
}
error Command failed with exit code 1.

This is triggered from here: https://github.com/dotansimha/graphql-code-generator/blob/2139f1db5704aa34a0cd89342736bf67ec556da3/packages/graphql-codegen-cli/src/utils/patterns.ts#L122

filePath is not always defined (in fact it is not possible to set it at all using the plain object input of generate), see here: https://github.com/dotansimha/graphql-code-generator/blob/2139f1db5704aa34a0cd89342736bf67ec556da3/packages/graphql-codegen-cli/src/config.ts#L365

Typescript in strict mode would have complained here, since the result of the RHS is then in fact string | undefined.

The constructor is called only with the config object, so filepath will always be undefined, triggering the error. https://github.com/dotansimha/graphql-code-generator/blob/2139f1db5704aa34a0cd89342736bf67ec556da3/packages/graphql-codegen-cli/src/config.ts#L448

Why not use strict: true for this project? That would have caught the bug here.

Workaround for the current version: Call generate with an instance of CodegenContext, instead of using the plain object.

Your Example Website or App

private repo

Steps to Reproduce the Bug or Issue

See description

import { generate } from '@graphql-codegen/cli';

generate({
   // ...config
  watch: true
});

Expected behavior

No crash

Screenshots or Videos

No response

Platform

Codegen Config File

import { generate } from '@graphql-codegen/cli';

generate({
   // ...config
  watch: true
});

Additional context

Workaround:

import { generate, CodegenContext } from '@graphql-codegen/cli';

generate(new CodegenContext({
  config: {
    // ...config
    watch: true
  },
  filepath: "i-must-be-set"
}));

I'm not sure what that extra watcher does, but this is actually working for me, triggering rebuild when any of the documents change.

saihaj commented 1 year ago

would you like to send a PR for this!?

milesrichardson commented 1 year ago

oof sorry about this (I introduced the bug)... IIRC I think I had copied some earlier logic and didn't quite understand what filepath was even doing here, but it worked in all my tests, and indeed, I guess we weren't using strict mode so I missed the type checking errors

MatthewMaclean commented 7 months ago

This is still a problem and annoying to discover when upgrading to v4/v5 of the CLI. If you rely on setting cwd, then you also can't switch over to CodegenContext easily.