From d431f426eb594b820ac712b9f5c616f4badf6bff Mon Sep 17 00:00:00 2001 From: Arian Santrach Date: Thu, 11 May 2023 11:29:09 +0200 Subject: [PATCH] fix: ignoreNoDocuments option usage when using graphql configs (#9371) --- .changeset/quiet-rocks-sin.md | 5 +++++ packages/graphql-codegen-cli/src/codegen.ts | 18 ++++++++++++++---- .../tests/generate-and-save.spec.ts | 17 +++++++++++++++++ .../tests/test-files/graphql.config.json | 14 ++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 .changeset/quiet-rocks-sin.md create mode 100644 packages/graphql-codegen-cli/tests/test-files/graphql.config.json diff --git a/.changeset/quiet-rocks-sin.md b/.changeset/quiet-rocks-sin.md new file mode 100644 index 00000000000..1a6326f1c4d --- /dev/null +++ b/.changeset/quiet-rocks-sin.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/cli': patch +--- + +Fixed option ignoreNoDocuments when using graphql configs diff --git a/packages/graphql-codegen-cli/src/codegen.ts b/packages/graphql-codegen-cli/src/codegen.ts index 9dc115ef394..043c7e92748 100644 --- a/packages/graphql-codegen-cli/src/codegen.ts +++ b/packages/graphql-codegen-cli/src/codegen.ts @@ -274,10 +274,20 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom const hash = JSON.stringify(documentPointerMap); const result = await cache('documents', hash, async () => { - const documents = await context.loadDocuments(documentPointerMap); - return { - documents, - }; + try { + const documents = await context.loadDocuments(documentPointerMap); + return { + documents, + }; + } catch (error) { + if (config.ignoreNoDocuments) { + return { + documents: [], + }; + } + + throw error; + } }); outputDocuments = result.documents; diff --git a/packages/graphql-codegen-cli/tests/generate-and-save.spec.ts b/packages/graphql-codegen-cli/tests/generate-and-save.spec.ts index f5e4016e4b7..77437f50e11 100644 --- a/packages/graphql-codegen-cli/tests/generate-and-save.spec.ts +++ b/packages/graphql-codegen-cli/tests/generate-and-save.spec.ts @@ -3,6 +3,7 @@ import { Types } from '@graphql-codegen/plugin-helpers'; import { useMonorepo } from '@graphql-codegen/testing'; import makeDir from 'make-dir'; import { generate } from '../src/generate-and-save.js'; +import { createContext } from '../src/config.js'; import * as fs from '../src/utils/file-system.js'; const SIMPLE_TEST_SCHEMA = `type MyType { f: String } type Query { f: String }`; @@ -76,6 +77,22 @@ describe('generate-and-save', () => { expect(writeSpy).not.toHaveBeenCalled(); }); + test('should not error when ignoreNoDocuments config option is present', async () => { + jest.spyOn(fs, 'writeFile').mockImplementation(); + const config = await createContext({ + config: './tests/test-files/graphql.config.json', + project: undefined, + errorsOnly: true, + overwrite: true, + profile: true, + require: [], + silent: false, + watch: false, + }); + + await generate(config, false); + }); + test('should use global overwrite option and write a file', async () => { const filename = 'overwrite.ts'; const writeSpy = jest.spyOn(fs, 'writeFile').mockImplementation(); diff --git a/packages/graphql-codegen-cli/tests/test-files/graphql.config.json b/packages/graphql-codegen-cli/tests/test-files/graphql.config.json new file mode 100644 index 00000000000..0ac1eb49059 --- /dev/null +++ b/packages/graphql-codegen-cli/tests/test-files/graphql.config.json @@ -0,0 +1,14 @@ +{ + "schema": ["../test-documents/schema.graphql"], + "documents": ["../test-documents/empty.graphql"], + "extensions": { + "codegen": { + "ignoreNoDocuments": true, + "generates": { + "./gql/": { + "preset": "client" + } + } + } + } +}