diff --git a/.changeset/shiny-items-kiss.md b/.changeset/shiny-items-kiss.md new file mode 100644 index 00000000000..7a11b2725f9 --- /dev/null +++ b/.changeset/shiny-items-kiss.md @@ -0,0 +1,19 @@ +--- +'@graphql-codegen/plugin-helpers': minor +'@graphql-codegen/cli': minor +--- + +Add new config option to not exit with non-zero exit code when there are no documents. + +You can use this option in your config: +```yaml +schema: 'schema.graphql' +documents: + - 'src/**/*.graphql' +ignoreNoDocuments: true +``` + +Alternative you can use the CLI to set this option: +```bash +$ codegen --config-file=config.yml --ignore-no-documents +``` diff --git a/packages/graphql-codegen-cli/src/config.ts b/packages/graphql-codegen-cli/src/config.ts index a03c9c66767..7d2cebf92c1 100644 --- a/packages/graphql-codegen-cli/src/config.ts +++ b/packages/graphql-codegen-cli/src/config.ts @@ -30,6 +30,7 @@ export type YamlCliFlags = { silent: boolean; errorsOnly: boolean; profile: boolean; + ignoreNoDocuments?: boolean; }; export function generateSearchPlaces(moduleName: string) { @@ -281,6 +282,11 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam config.errorsOnly = cliFlags.errorsOnly; } + if (cliFlags['ignore-no-documents'] !== undefined) { + // for some reason parsed value is `'false'` string so this ensure it always is a boolean. + config.ignoreNoDocuments = cliFlags['ignore-no-documents'] === true; + } + if (cliFlags.project) { context.useProject(cliFlags.project); } diff --git a/packages/graphql-codegen-cli/src/load.ts b/packages/graphql-codegen-cli/src/load.ts index 7ae20356cee..ddc166bd5b2 100644 --- a/packages/graphql-codegen-cli/src/load.ts +++ b/packages/graphql-codegen-cli/src/load.ts @@ -58,16 +58,16 @@ export async function loadSchema( ${e.message || e} ${e.stack || ''} - + GraphQL Code Generator supports: - ES Modules and CommonJS exports (export as default or named export "schema") - Introspection JSON File - URL of GraphQL endpoint - Multiple files with type definitions (glob expression) - String in config file - + Try to use one of above options and run codegen again. - + ` ); } @@ -97,13 +97,17 @@ export async function loadDocuments( ignore.push(join(process.cwd(), generatePath)); } - const loadedFromToolkit = await loadDocumentsToolkit(documentPointers, { - ...defaultDocumentsLoadOptions, - ignore, - loaders, - ...config, - ...config.config, - }); - - return loadedFromToolkit; + try { + const loadedFromToolkit = await loadDocumentsToolkit(documentPointers, { + ...defaultDocumentsLoadOptions, + ignore, + loaders, + ...config, + ...config.config, + }); + return loadedFromToolkit; + } catch (error) { + if (config.ignoreNoDocuments) return []; + throw error; + } } diff --git a/packages/graphql-codegen-cli/tests/cli-flags.spec.ts b/packages/graphql-codegen-cli/tests/cli-flags.spec.ts index 51f53ebfed6..54f01ccbe3d 100644 --- a/packages/graphql-codegen-cli/tests/cli-flags.spec.ts +++ b/packages/graphql-codegen-cli/tests/cli-flags.spec.ts @@ -120,6 +120,34 @@ describe('CLI Flags', () => { expect(config.watch).toBeFalsy(); }); + it('Should overwrite ignoreNoDocuments config using cli flags to false', async () => { + mockConfig(` + schema: schema.graphql + ignoreNoDocuments: true + generates: + file.ts: + - plugin + `); + const args = createArgv('--ignore-no-documents=false'); + const context = await createContext(parseArgv(args)); + const config = context.getConfig(); + expect(config.ignoreNoDocuments).toBeFalsy(); + }); + + it('Should overwrite ignoreNoDocuments config using cli flags to true', async () => { + mockConfig(` + schema: schema.graphql + ignoreNoDocuments: false + generates: + file.ts: + - plugin + `); + const args = createArgv('--ignore-no-documents'); + const context = await createContext(parseArgv(args)); + const config = context.getConfig(); + expect(config.ignoreNoDocuments).toBeTruthy(); + }); + it('Should set --overwrite with new YML api', async () => { mockConfig(` schema: schema.graphql diff --git a/packages/utils/plugins-helpers/src/types.ts b/packages/utils/plugins-helpers/src/types.ts index 3f79d40679a..074413e684b 100644 --- a/packages/utils/plugins-helpers/src/types.ts +++ b/packages/utils/plugins-helpers/src/types.ts @@ -433,6 +433,10 @@ export namespace Types { usePolling: boolean; interval?: number; }; + /** + * @description A flag to suppress non-zero exit code when there are no documents to generate. + */ + ignoreNoDocuments?: boolean; /** * @description A flag to suppress printing errors when they occur. */ diff --git a/website/docs/config-reference/codegen-config.mdx b/website/docs/config-reference/codegen-config.mdx index 7f03a3f6cfb..ab6b6653f52 100644 --- a/website/docs/config-reference/codegen-config.mdx +++ b/website/docs/config-reference/codegen-config.mdx @@ -77,6 +77,8 @@ Here are the supported options that you can define in the config file (see [sour - **`silent`** - A flag to suppress printing errors when they occur. +- **`ignoreNoDocuments`** - A flag to not exit with non-zero exit code when there are no documents. + - **`errorsOnly`** - A flag to suppress printing anything except errors. - **`hooks`** - Specifies scripts to run when events are happening in the codegen's core. You can read more about lifecycle hooks [here](./lifecycle-hooks). You can specify this on your root configuration or on each output.