Skip to content

Commit

Permalink
feat: add ignoreNoDocuments flag and config option (#8040)
Browse files Browse the repository at this point in the history
* feat: add new ignoreNoDocuments config and cli flag

* docs: add ignoreNoDocuments flag
  • Loading branch information
saihaj committed Jul 7, 2022
1 parent 367cc4a commit 8e44df5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 12 deletions.
19 changes: 19 additions & 0 deletions .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
```
6 changes: 6 additions & 0 deletions packages/graphql-codegen-cli/src/config.ts
Expand Up @@ -30,6 +30,7 @@ export type YamlCliFlags = {
silent: boolean;
errorsOnly: boolean;
profile: boolean;
ignoreNoDocuments?: boolean;
};

export function generateSearchPlaces(moduleName: string) {
Expand Down Expand Up @@ -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);
}
Expand Down
28 changes: 16 additions & 12 deletions packages/graphql-codegen-cli/src/load.ts
Expand Up @@ -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.
`
);
}
Expand Down Expand Up @@ -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;
}
}
28 changes: 28 additions & 0 deletions packages/graphql-codegen-cli/tests/cli-flags.spec.ts
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/plugins-helpers/src/types.ts
Expand Up @@ -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.
*/
Expand Down
2 changes: 2 additions & 0 deletions website/docs/config-reference/codegen-config.mdx
Expand Up @@ -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.
Expand Down

1 comment on commit 8e44df5

@vercel
Copy link

@vercel vercel bot commented on 8e44df5 Jul 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.