Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watch mode not working to generate the schema.graphql file with graphql-code-generator #854

Closed
zhe opened this issue Mar 26, 2023 · 1 comment

Comments

@zhe
Copy link

zhe commented Mar 26, 2023

Hi community, I'm trying to use graphql-codegen in watch mode to generate the schema.graphql file whenever the schema file is updated, but it seems not working, I had to stop and re-run the generate script to get the updated schema.graphql file.

Just wondering if anyone had seen this, or knows how to work with this?

The codegen --verbose info:

[STARTED] Parse Configuration
[SUCCESS] Parse Configuration
[STARTED] Generate outputs
[STARTED] Generate to src/schema.graphql
[STARTED] Load GraphQL schemas
[SUCCESS] Load GraphQL schemas
[STARTED] Load GraphQL documents
[SUCCESS] Load GraphQL documents
[STARTED] Generate
[SUCCESS] Generate
[SUCCESS] Generate to src/schema.graphql
[SUCCESS] Generate outputs
  ℹ Watching for changes...

Guide: https://pothos-graphql.dev/docs/guide/generating-client-types#generating-a-schemagraphql-file-with-graphql-code-generator

Codegen watch mode: https://the-guild.dev/graphql/codegen/docs/getting-started/development-workflow#watch-mode

Thanks! 🙏

@hayes
Copy link
Owner

hayes commented Mar 26, 2023

This worked for me, but there are likely better ways to do it:

codegen.yml

overwrite: true
schema: graphql/build-schema.js
documents: 'components/**/!(*.d).{ts,tsx}'
generates:
  schema.graphql:
    plugins:
      - schema-ast

Then in build-schema.js

Object.keys(require.cache).filter(key => !key.includes('node_modules')).forEach(key => {
    delete require.cache[key]
});

module.exports = require('@boost/module').requireModule(require.resolve('./schema.ts'))

They may have added support for loading ts files directly, so not sure if the extra loading step is still necessary.

I'm pretty sure this is overcomplicating things and there is a better option, I just haven't spent a lot of time looking at graphql-codgen.

Generally I just write the schema directly using fs.writeFileSync(schema.graphql, printSchema(schema)) in my dev server which is already watching/reloading

You might also want to double check if just passing file patterns that include your typescript files works, but good chance it doesn't

@hayes hayes closed this as completed Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants