Graphql-code-generator: Watch mode not working with JavaScript export schema type

Created on 29 Mar 2019  Â·  15Comments  Â·  Source: dotansimha/graphql-code-generator

I have the following working config file which generates types correctly.

schema: ./server/config/gql-gen/schema.js
overwrite: true
generates:
  # using .ts instead of .d.ts so we can import enums from it
  ./server/types/graphql.ts:
    config:
      noNamespaces: true
      contextType: ../graphql#GqlContext
      fieldResolverNamePrefix: Field
      # see https://graphql-code-generator.com/docs/plugins/typescript-resolvers#mappers-overwrite-parents-and-resolved-values
      mappers:
        Product: ../graphql/product/product-model#ProductParent
      scalars:
        Date: Date
    plugins:
      - add: "/**\n * THIS IS A GENERATED FILE, DO NOT MODIFY DIRECTLY\n */\n"
      - typescript-common
      - typescript-server
      - typescript-resolvers

However, when running the CLI with --watch, it does not see any changes to the schema files (which are imported in schema.js).

bug core waiting-for-release

Most helpful comment

You are right @minheq . I managed to reproduce it locally, it caused by require cache.
Fixed in graphql-toolkit :)

All 15 comments

I believe the underlying issue is that gql-gen has no idea which files to watch. Could we add another config option (watchGlob?) that watch mode could use, if present?

Hi @stevenmusumeche ,
You can add watch section to your codegen.yml, and add custom paths to watch, it can accept with boolean | string | string[], so you can just do:

watch:
   - my/custom/path/**/*.js

https://github.com/dotansimha/graphql-code-generator/blob/master/packages/utils/plugins-helpers/src/types.ts#L58

Leaving it open until we document it better :)

@dotansimha thanks for the clarification on the watch option. When adding the glob to my settings, I can see that gql-gen "sees" that a schema has changed, however, the types are not updated. I think the problem might be because I am using a custom schema loader?

  ✔ Parse configuration
  ✔ Generate outputs
  ℹ Watching for changes...   (changed product-schema.ts here)
  ✔ Parse configuration
  ✔ Generate outputs
  ℹ Watching for changes...
schema: ./server/config/gql-gen/schema.js
overwrite: true
watch:
  - ./server/graphql/**/*schema.ts
generates:
  # using .ts instead of .d.ts so we can import enums from it
  ./server/types/graphql.ts:
    config:
      noNamespaces: true
      contextType: ../graphql#GqlContext
      fieldResolverNamePrefix: Field
      scalars:
        Date: Date
    plugins:
      - add: "/**\n * THIS IS A GENERATED FILE, DO NOT MODIFY DIRECTLY\n */\n"
      - typescript-common
      - typescript-server
      - typescript-resolvers

I am also encountering the bug where codegen watch does not generate new output, if a schema is exported from JS file

@stevenmusumeche have u found a way to work around it?
@dotansimha I have reproduced the bug here:

https://github.com/minheq/gql-codegen-watch-bug-repro

Can we re-open the issue?

My workaround was to write a custom script that uses chokidar to watch for changes to the files.

@minheq @stevenmusumeche

Please note that if you are using a code file with export for your schema, the codegen can't follow and tell which files are loaded from your code files, so it can't really watch for those changes. That's why you need to pass custom files to the codegen.

@minheq I tried your repo, ran yarn codegen and it was waiting for changes, then I changed the schema.js file, and the output was:

➜  gql-codegen-watch-bug-repro git:(master) yarn codegen
yarn run v1.17.3
warning ../package.json: No license field
$ graphql-codegen --config codegen.yml
  ✔ Parse configuration
  ✔ Generate outputs
  ℹ Watching for changes...
### HERE I DID THE CHANGE
  ✔ Parse configuration
  ✔ Generate outputs
  ℹ Watching for changes...

It seems like it works, can you please provide more information? which OS do you use?

@dotansimha hey,

the bug here is not with watching the files, that part works fine.
the issue is that the output.ts is not updated as schema.js is changed.

for example if you try to add a field to Post in typedefs in schema.js, i would expect that when the change is detected, it would regenerate the Post in output.ts

thanks for looking into it

I'm using MacBook Pro 15 2017

You are right @minheq . I managed to reproduce it locally, it caused by require cache.
Fixed in graphql-toolkit :)

Fixed in 1.7.0

I am actually still seeing this behavior (watching files works, but output is untouched on change) in 1.8.3.

Here is my codegen.yml

schema: src/graph/schema.ts
require:
    - ts-node/register
watch:
    - src/graph/nodes/**/*.ts
overwrite: true
generates:
    src/graph/gen-types.ts:
        plugins:
            - typescript
            - typescript-resolvers
        config:
            contextType: ./context#IContext

@grezxune if it's still relevant, can you please report this in a new issue?

Looks still relevant to me, so I created new issue.

Is it possible to set the watch glob with the CLI -w param? I only want to run in watch mode when I run codegen with this param.

@fabis94 yes :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edorivai picture edorivai  Â·  3Comments

SimenB picture SimenB  Â·  3Comments

iamdanthedev picture iamdanthedev  Â·  3Comments

leebenson picture leebenson  Â·  3Comments

RIP21 picture RIP21  Â·  3Comments