Skip to content

Commit

Permalink
fix: ignoreNoDocuments option usage when using graphql configs (#9371)
Browse files Browse the repository at this point in the history
  • Loading branch information
Axxxx0n committed May 11, 2023
1 parent 0f85802 commit d431f42
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-rocks-sin.md
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': patch
---

Fixed option ignoreNoDocuments when using graphql configs
18 changes: 14 additions & 4 deletions packages/graphql-codegen-cli/src/codegen.ts
Expand Up @@ -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;
Expand Down
17 changes: 17 additions & 0 deletions packages/graphql-codegen-cli/tests/generate-and-save.spec.ts
Expand Up @@ -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 }`;
Expand Down Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions 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"
}
}
}
}
}

0 comments on commit d431f42

Please sign in to comment.