Skip to content

Commit 8e44df5

Browse files
authored
feat: add ignoreNoDocuments flag and config option (#8040)
* feat: add new ignoreNoDocuments config and cli flag * docs: add ignoreNoDocuments flag
1 parent 367cc4a commit 8e44df5

File tree

6 files changed

+75
-12
lines changed

6 files changed

+75
-12
lines changed

‎.changeset/shiny-items-kiss.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
'@graphql-codegen/plugin-helpers': minor
3+
'@graphql-codegen/cli': minor
4+
---
5+
6+
Add new config option to not exit with non-zero exit code when there are no documents.
7+
8+
You can use this option in your config:
9+
```yaml
10+
schema: 'schema.graphql'
11+
documents:
12+
- 'src/**/*.graphql'
13+
ignoreNoDocuments: true
14+
```
15+
16+
Alternative you can use the CLI to set this option:
17+
```bash
18+
$ codegen --config-file=config.yml --ignore-no-documents
19+
```

‎packages/graphql-codegen-cli/src/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type YamlCliFlags = {
3030
silent: boolean;
3131
errorsOnly: boolean;
3232
profile: boolean;
33+
ignoreNoDocuments?: boolean;
3334
};
3435

3536
export function generateSearchPlaces(moduleName: string) {
@@ -281,6 +282,11 @@ export function updateContextWithCliFlags(context: CodegenContext, cliFlags: Yam
281282
config.errorsOnly = cliFlags.errorsOnly;
282283
}
283284

285+
if (cliFlags['ignore-no-documents'] !== undefined) {
286+
// for some reason parsed value is `'false'` string so this ensure it always is a boolean.
287+
config.ignoreNoDocuments = cliFlags['ignore-no-documents'] === true;
288+
}
289+
284290
if (cliFlags.project) {
285291
context.useProject(cliFlags.project);
286292
}

‎packages/graphql-codegen-cli/src/load.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,16 @@ export async function loadSchema(
5858
5959
${e.message || e}
6060
${e.stack || ''}
61-
61+
6262
GraphQL Code Generator supports:
6363
- ES Modules and CommonJS exports (export as default or named export "schema")
6464
- Introspection JSON File
6565
- URL of GraphQL endpoint
6666
- Multiple files with type definitions (glob expression)
6767
- String in config file
68-
68+
6969
Try to use one of above options and run codegen again.
70-
70+
7171
`
7272
);
7373
}
@@ -97,13 +97,17 @@ export async function loadDocuments(
9797
ignore.push(join(process.cwd(), generatePath));
9898
}
9999

100-
const loadedFromToolkit = await loadDocumentsToolkit(documentPointers, {
101-
...defaultDocumentsLoadOptions,
102-
ignore,
103-
loaders,
104-
...config,
105-
...config.config,
106-
});
107-
108-
return loadedFromToolkit;
100+
try {
101+
const loadedFromToolkit = await loadDocumentsToolkit(documentPointers, {
102+
...defaultDocumentsLoadOptions,
103+
ignore,
104+
loaders,
105+
...config,
106+
...config.config,
107+
});
108+
return loadedFromToolkit;
109+
} catch (error) {
110+
if (config.ignoreNoDocuments) return [];
111+
throw error;
112+
}
109113
}

‎packages/graphql-codegen-cli/tests/cli-flags.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,34 @@ describe('CLI Flags', () => {
120120
expect(config.watch).toBeFalsy();
121121
});
122122

123+
it('Should overwrite ignoreNoDocuments config using cli flags to false', async () => {
124+
mockConfig(`
125+
schema: schema.graphql
126+
ignoreNoDocuments: true
127+
generates:
128+
file.ts:
129+
- plugin
130+
`);
131+
const args = createArgv('--ignore-no-documents=false');
132+
const context = await createContext(parseArgv(args));
133+
const config = context.getConfig();
134+
expect(config.ignoreNoDocuments).toBeFalsy();
135+
});
136+
137+
it('Should overwrite ignoreNoDocuments config using cli flags to true', async () => {
138+
mockConfig(`
139+
schema: schema.graphql
140+
ignoreNoDocuments: false
141+
generates:
142+
file.ts:
143+
- plugin
144+
`);
145+
const args = createArgv('--ignore-no-documents');
146+
const context = await createContext(parseArgv(args));
147+
const config = context.getConfig();
148+
expect(config.ignoreNoDocuments).toBeTruthy();
149+
});
150+
123151
it('Should set --overwrite with new YML api', async () => {
124152
mockConfig(`
125153
schema: schema.graphql

‎packages/utils/plugins-helpers/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ export namespace Types {
433433
usePolling: boolean;
434434
interval?: number;
435435
};
436+
/**
437+
* @description A flag to suppress non-zero exit code when there are no documents to generate.
438+
*/
439+
ignoreNoDocuments?: boolean;
436440
/**
437441
* @description A flag to suppress printing errors when they occur.
438442
*/

‎website/docs/config-reference/codegen-config.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ Here are the supported options that you can define in the config file (see [sour
7777

7878
- **`silent`** - A flag to suppress printing errors when they occur.
7979

80+
- **`ignoreNoDocuments`** - A flag to not exit with non-zero exit code when there are no documents.
81+
8082
- **`errorsOnly`** - A flag to suppress printing anything except errors.
8183

8284
- **`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.

0 commit comments

Comments
 (0)