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.84k stars 1.33k forks source link

Watch mode not working as expected #1095

Closed itscarlosrufo closed 5 years ago

itscarlosrufo commented 5 years ago

Describe the bug Watch mode is either not watching or not overwriting over the changes made

To Reproduce Steps to reproduce the behavior:

Clone repository: git clone -b graphql-ts https://github.com/swcarlosrj/SpaceX-API.git Install deps & start server: yarn && yarn start Generate types: yarn generate Change any file into schema folder

Expected behavior Auto generate types & overwrite the current types everytime that I change any file

Environment:

OS: macOS Mojave 10.14.2 (18C54) Codegen: 0.15.2 Node: 10.10.0

Additional context With the current codegen.yml config, it doesn't watch although it generated correctly the types

schema: src/graphql/schema/index.ts
overwrite: true
watch: true
require:
  - ts-node/register
generates:
  ./src/graphql/types/types.d.ts:
    config:
      contextType: ./context#MyContext
    plugins:
      - typescript-common
      - typescript-server
      - typescript-resolvers

Instead with this codegen.yml config, it doesn't generate correctly the types although it seems to be watching

schema: src/graphql/schema/**/*.ts
overwrite: true
watch: true
require:
  - ts-node/register
generates:
  ./src/graphql/types/types.d.ts:
    config:
      contextType: ./context#MyContext
    plugins:
      - typescript-common
      - typescript-server
      - typescript-resolvers
itscarlosrufo commented 5 years ago

cc @kamilkisiela

ctrlplusb commented 5 years ago

FYI I tried via the CLI flag and watch mode doesn't work either. It exits immediately:

$ npx gql-gen --config graphql/codegen.yml -w
  ✔ Parse configuration
  ✔ Generate outputs
  ℹ Watching for changes...
$ _
dotansimha commented 5 years ago

@swcarlosrj Can you please explain what was the difference in the output between schema: src/graphql/schema/**/*.ts and src/graphql/schema/index.ts? why does the output it not correct with schema: src/graphql/schema/**/*.ts?

Also, what src/graphql/schema/index.ts contains? I'm not sure we are watching the files loaded by src/graphql/schema/index.ts (or maybe it's just require cache in this case).

itscarlosrufo commented 5 years ago

@dotansimha I've just tried, it works great both overwriting new generated types & watching mode, I think that I was using documents config field before, thats why I was not getting the desired generated types.

If it's useful for someone, I leave here the current config that I'm using to generate types for the GraphQL Schema & Resolvers, working 100% as expected!

schema: src/graphql/schema/**/*.ts
overwrite: true
watch: true
require:
  - ts-node/register
generates:
  ./src/graphql/types/types.d.ts:
    config:
      contextType: ./context#MyContext
    plugins:
      - typescript-common
      - typescript-server
      - typescript-resolvers

Incredible tool & better work, thanks for such amazing contribution 😍