Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lazy-snails-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/cli': patch
---

Fix ignoreNoDocuments=true swallowing all errors
2 changes: 1 addition & 1 deletion examples/programmatic-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@graphql-codegen/typescript-operations": "4.6.0",
"@graphql-codegen/typescript-resolvers": "4.5.0",
"@graphql-tools/graphql-file-loader": "8.0.1",
"@graphql-tools/load": "8.0.2",
"@graphql-tools/load": "8.1.0",
"@graphql-tools/schema": "10.0.6",
"graphql": "16.9.0",
"graphql-tag": "2.12.6",
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-codegen-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@graphql-tools/github-loader": "^8.0.0",
"@graphql-tools/graphql-file-loader": "^8.0.0",
"@graphql-tools/json-file-loader": "^8.0.0",
"@graphql-tools/load": "^8.0.0",
"@graphql-tools/load": "^8.1.0",
"@graphql-tools/prisma-loader": "^8.0.0",
"@graphql-tools/url-loader": "^8.0.0",
"@graphql-tools/utils": "^10.0.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/graphql-codegen-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
normalizeOutputParam,
Types,
} from '@graphql-codegen/plugin-helpers';
import { NoTypeDefinitionsFound } from '@graphql-tools/load';
import { DocumentNode, GraphQLError, GraphQLSchema } from 'graphql';
import { Listr, ListrTask } from 'listr2';
import { CodegenContext, ensureContext, shouldEmitLegacyCommonJSImports } from './config.js';
Expand Down Expand Up @@ -279,7 +280,7 @@ export async function executeCodegen(input: CodegenContext | Types.Config): Prom
documents,
};
} catch (error) {
if (config.ignoreNoDocuments) {
if (error instanceof NoTypeDefinitionsFound && config.ignoreNoDocuments) {
return {
documents: [],
};
Expand Down
8 changes: 7 additions & 1 deletion packages/graphql-codegen-cli/src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { JsonFileLoader } from '@graphql-tools/json-file-loader';
import {
loadDocuments as loadDocumentsToolkit,
loadSchema as loadSchemaToolkit,
NoTypeDefinitionsFound,
UnnormalizedTypeDefPointer,
} from '@graphql-tools/load';
import { PrismaLoader } from '@graphql-tools/prisma-loader';
Expand Down Expand Up @@ -101,7 +102,12 @@ export async function loadDocuments(
});
return loadedFromToolkit;
} catch (error) {
if (config.ignoreNoDocuments) return [];
// NoTypeDefinitionsFound from `@graphql-tools/load` already has a message with pointer, so we can just rethrow the error
if (error instanceof NoTypeDefinitionsFound) {
throw error;
}

// For other errors, we need to add an error message with documentPointers, so it's better for DevX
throw new Error(
[`Failed to load documents from ${Object.keys(documentPointers).join(',')}:`, printError(error)].join('\n')
);
Expand Down
112 changes: 92 additions & 20 deletions packages/graphql-codegen-cli/tests/generate-and-save.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { dirname, join } from 'path';
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 { generate } from '../src/generate-and-save.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 @@ -77,22 +77,6 @@ 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);
});
Comment thread
dotansimha marked this conversation as resolved.

test('should use global overwrite option and write a file', async () => {
const filename = 'overwrite.ts';
const writeSpy = jest.spyOn(fs, 'writeFile').mockImplementation();
Expand Down Expand Up @@ -255,11 +239,11 @@ describe('generate-and-save', () => {
expect(writeSpy).toHaveBeenCalled();
});

describe('Syntax errors when loading pointers', () => {
describe('Errors when loading pointers', () => {
Comment thread
dotansimha marked this conversation as resolved.
const originalConsole = { ...console };
const originalNodeEnv = process.env.NODE_ENV;

let consoleErrorMock;
let consoleErrorMock: jest.Mock;

beforeEach(() => {
// Mock common console functions to avoid noise in the terminal
Expand All @@ -279,7 +263,8 @@ describe('generate-and-save', () => {
process.env.NODE_ENV = originalNodeEnv;
});

test('Schema syntax error - should print native GraphQLError for', async () => {
test('Schema syntax error - should print native GraphQLError', async () => {
expect.assertions(4);
try {
await generate(
{
Expand Down Expand Up @@ -324,6 +309,7 @@ describe('generate-and-save', () => {
});

test('Document syntax error - should print native GraphQLError', async () => {
expect.assertions(4);
Comment thread
dotansimha marked this conversation as resolved.
try {
await generate(
{
Expand Down Expand Up @@ -352,5 +338,91 @@ describe('generate-and-save', () => {
`);
}
});

test('No documents found - should throw error by default', async () => {
expect.assertions(1);
try {
await generate(
{
verbose: true,
schema: './tests/test-files/schema-dir/schema.ts',
documents: './tests/test-files/document-file-does-not-exist.graphql',
generates: {
'src/test.ts': {
plugins: ['typescript'],
},
},
},
false
);
} catch {
expect(consoleErrorMock.mock.calls[0][0]).toBeSimilarStringTo(`
[FAILED] Unable to find any GraphQL type definitions for the following pointers:
[FAILED]
[FAILED] - ./tests/test-files/document-file-does-not-exist.graphql
`);
}
});

test('No documents found - should not fail if ignoreNoDocuments=true', async () => {
await generate(
{
verbose: true,
ignoreNoDocuments: true,
schema: './tests/test-files/schema-dir/schema.ts',
documents: './tests/test-files/document-file-does-not-exist.graphql',
generates: {
'src/test.ts': {
plugins: ['typescript'],
},
},
},
false
);
expect(consoleErrorMock).not.toHaveBeenCalled();
});

test('No documents found - GraphQL Config - should throw error by default', async () => {
expect.assertions(1);
try {
const config = await createContext({
config: './tests/test-files/graphql.config.no-doc.js',
project: undefined,
errorsOnly: true,
overwrite: true,
profile: true,
require: [],
silent: false,
watch: false,
});

await generate(config, false);
} catch {
expect(consoleErrorMock.mock.calls[0][0]).toBeSimilarStringTo(`
[FAILED]
[FAILED] Unable to find any GraphQL type definitions for the following pointers:
[FAILED]
[FAILED] - ../test-documents/empty.graphql
`);
}
});

test('No documents found - GraphQL Config - should not fail if ignoreNoDocuments=true', async () => {
jest.spyOn(fs, 'writeFile').mockImplementation();
const config = await createContext({
config: './tests/test-files/graphql.config.no-doc-ignored.js',
project: undefined,
errorsOnly: true,
overwrite: true,
profile: true,
require: [],
silent: false,
watch: false,
});

await generate(config, false);

expect(consoleErrorMock).not.toHaveBeenCalled();
});
});
});
14 changes: 0 additions & 14 deletions packages/graphql-codegen-cli/tests/test-files/graphql.config.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('graphql-config').IGraphQLConfig } */
module.exports = {
schema: ['../test-documents/schema.graphql'],
documents: ['../test-documents/empty.graphql'],
extensions: {
codegen: {
verbose: true,
ignoreNoDocuments: true,
generates: {
'./gql/': {
preset: 'client',
},
},
},
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/** @type {import('graphql-config').IGraphQLConfig } */
module.exports = {
schema: ['../test-documents/schema.graphql'],
documents: ['../test-documents/empty.graphql'],
extensions: {
codegen: {
verbose: true,
generates: {
'./gql/': {
preset: 'client',
},
},
},
},
};
Loading
Loading