@graphql-codegen/cli 6.0.0 → 6.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cjs/codegen.js CHANGED
@@ -245,12 +245,17 @@ async function executeCodegen(input) {
245
245
  const name = Object.keys(plugin)[0];
246
246
  return [name, pkg];
247
247
  }));
248
+ const importExtension = (0, plugin_helpers_1.normalizeImportExtension)({
249
+ emitLegacyCommonJSImports: config.emitLegacyCommonJSImports,
250
+ importExtension: config.importExtension,
251
+ });
248
252
  const mergedConfig = {
249
253
  ...rootConfig,
250
254
  ...(typeof outputFileTemplateConfig === 'string'
251
255
  ? { value: outputFileTemplateConfig }
252
256
  : outputFileTemplateConfig),
253
- emitLegacyCommonJSImports: (0, config_js_1.shouldEmitLegacyCommonJSImports)(config),
257
+ importExtension,
258
+ emitLegacyCommonJSImports: config.emitLegacyCommonJSImports ?? true,
254
259
  };
255
260
  const documentTransforms = Array.isArray(outputConfig.documentTransforms)
256
261
  ? await Promise.all(outputConfig.documentTransforms.map(async (config, index) => {
@@ -288,8 +293,8 @@ async function executeCodegen(input) {
288
293
  const process = async (outputArgs) => {
289
294
  const output = await (0, core_1.codegen)({
290
295
  ...outputArgs,
291
- // @ts-expect-error todo: fix 'emitLegacyCommonJSImports' does not exist in type 'GenerateOptions'
292
- emitLegacyCommonJSImports: (0, config_js_1.shouldEmitLegacyCommonJSImports)(config, outputArgs.filename),
296
+ importExtension,
297
+ emitLegacyCommonJSImports: config.emitLegacyCommonJSImports ?? true,
293
298
  cache,
294
299
  });
295
300
  result.push({
@@ -319,7 +324,7 @@ async function executeCodegen(input) {
319
324
  exitOnError: false,
320
325
  };
321
326
  });
322
- return task.newListr(generateTasks, { concurrent: (0, os_1.cpus)().length });
327
+ return task.newListr(generateTasks, { concurrent: (0, os_1.cpus)().length || 1 });
323
328
  },
324
329
  },
325
330
  ], {
package/cjs/config.js CHANGED
@@ -9,7 +9,6 @@ exports.parseArgv = parseArgv;
9
9
  exports.createContext = createContext;
10
10
  exports.updateContextWithCliFlags = updateContextWithCliFlags;
11
11
  exports.ensureContext = ensureContext;
12
- exports.shouldEmitLegacyCommonJSImports = shouldEmitLegacyCommonJSImports;
13
12
  const tslib_1 = require("tslib");
14
13
  const crypto_1 = require("crypto");
15
14
  const fs_1 = require("fs");
@@ -178,6 +177,18 @@ function buildOptions() {
178
177
  type: 'boolean',
179
178
  default: false,
180
179
  },
180
+ 'emit-legacy-common-js-imports': {
181
+ describe: 'Emit legacy CommonJS imports (deprecated, use import-extension instead)',
182
+ type: 'boolean',
183
+ },
184
+ 'import-extension': {
185
+ describe: 'Extension to append to imports (e.g., .js, .mjs, or empty string for no extension)',
186
+ type: 'string',
187
+ },
188
+ 'ignore-no-documents': {
189
+ describe: 'Suppress errors for no documents',
190
+ type: 'boolean',
191
+ },
181
192
  };
182
193
  }
183
194
  function parseArgv(argv = process.argv) {
@@ -225,6 +236,9 @@ function updateContextWithCliFlags(context, cliFlags) {
225
236
  // for some reason parsed value is `'false'` string so this ensure it always is a boolean.
226
237
  config.emitLegacyCommonJSImports = cliFlags['emit-legacy-common-js-imports'] === true;
227
238
  }
239
+ if (cliFlags['import-extension'] !== undefined) {
240
+ config.importExtension = cliFlags['import-extension'];
241
+ }
228
242
  if (cliFlags.project) {
229
243
  context.useProject(cliFlags.project);
230
244
  }
@@ -352,15 +366,3 @@ function addHashToDocumentFiles(documentFilesPromise) {
352
366
  return doc;
353
367
  }));
354
368
  }
355
- function shouldEmitLegacyCommonJSImports(config) {
356
- const globalValue = config.emitLegacyCommonJSImports === undefined ? true : !!config.emitLegacyCommonJSImports;
357
- // const outputConfig = config.generates[outputPath];
358
- // if (!outputConfig) {
359
- // debugLog(`Couldn't find a config of ${outputPath}`);
360
- // return globalValue;
361
- // }
362
- // if (isConfiguredOutput(outputConfig) && typeof outputConfig.emitLegacyCommonJSImports === 'boolean') {
363
- // return outputConfig.emitLegacyCommonJSImports;
364
- // }
365
- return globalValue;
366
- }
@@ -48,7 +48,21 @@ const createWatcher = (initialContext, onNext) => {
48
48
  const debouncedExec = (0, debounce_1.default)(() => {
49
49
  if (!isShutdown) {
50
50
  (0, codegen_js_1.executeCodegen)(initialContext)
51
- .then(({ result }) => onNext(result), () => Promise.resolve())
51
+ .then(({ result, error }) => {
52
+ // FIXME: this is a quick fix to stop `onNext` (writeOutput) from
53
+ // removing all files when there is an error.
54
+ //
55
+ // This is because `removeStaleFiles()` will remove files if the
56
+ // generated files are different between runs. And on error, it
57
+ // returns an empty array i.e. will remove all generated files from
58
+ // the previous run
59
+ //
60
+ // This also means we don't have config.allowPartialOutputs in watch mode
61
+ if (error) {
62
+ return;
63
+ }
64
+ onNext(result);
65
+ }, () => Promise.resolve())
52
66
  .then(() => emitWatching(watchDirectory));
53
67
  }
54
68
  }, 100);
@@ -146,7 +160,14 @@ const createWatcher = (initialContext, onNext) => {
146
160
  */
147
161
  stopWatching.runningWatcher = new Promise((resolve, reject) => {
148
162
  (0, codegen_js_1.executeCodegen)(initialContext)
149
- .then(({ result }) => onNext(result), () => Promise.resolve())
163
+ .then(({ result, error }) => {
164
+ // TODO: this is the initial run, the logic here mimics the above watcher logic.
165
+ // We need to check whether it's ok to deviate between these two.
166
+ if (error) {
167
+ return;
168
+ }
169
+ onNext(result);
170
+ }, () => Promise.resolve())
150
171
  .then(() => runWatcher(abortController.signal))
151
172
  .catch(err => {
152
173
  watcherSubscription.unsubscribe();
package/esm/codegen.js CHANGED
@@ -3,11 +3,11 @@ import { createRequire } from 'module';
3
3
  import { cpus } from 'os';
4
4
  import path from 'path';
5
5
  import { codegen } from '@graphql-codegen/core';
6
- import { getCachedDocumentNodeFromSchema, normalizeConfig, normalizeInstanceOrArray, normalizeOutputParam, } from '@graphql-codegen/plugin-helpers';
6
+ import { getCachedDocumentNodeFromSchema, normalizeConfig, normalizeImportExtension, normalizeInstanceOrArray, normalizeOutputParam, } from '@graphql-codegen/plugin-helpers';
7
7
  import { NoTypeDefinitionsFound } from '@graphql-tools/load';
8
8
  import { GraphQLError } from 'graphql';
9
9
  import { Listr } from 'listr2';
10
- import { ensureContext, shouldEmitLegacyCommonJSImports } from './config.js';
10
+ import { ensureContext } from './config.js';
11
11
  import { getPluginByName } from './plugins.js';
12
12
  import { getPresetByName } from './presets.js';
13
13
  import { debugLog, printLogs } from './utils/debugging.js';
@@ -241,12 +241,17 @@ export async function executeCodegen(input) {
241
241
  const name = Object.keys(plugin)[0];
242
242
  return [name, pkg];
243
243
  }));
244
+ const importExtension = normalizeImportExtension({
245
+ emitLegacyCommonJSImports: config.emitLegacyCommonJSImports,
246
+ importExtension: config.importExtension,
247
+ });
244
248
  const mergedConfig = {
245
249
  ...rootConfig,
246
250
  ...(typeof outputFileTemplateConfig === 'string'
247
251
  ? { value: outputFileTemplateConfig }
248
252
  : outputFileTemplateConfig),
249
- emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config),
253
+ importExtension,
254
+ emitLegacyCommonJSImports: config.emitLegacyCommonJSImports ?? true,
250
255
  };
251
256
  const documentTransforms = Array.isArray(outputConfig.documentTransforms)
252
257
  ? await Promise.all(outputConfig.documentTransforms.map(async (config, index) => {
@@ -284,8 +289,8 @@ export async function executeCodegen(input) {
284
289
  const process = async (outputArgs) => {
285
290
  const output = await codegen({
286
291
  ...outputArgs,
287
- // @ts-expect-error todo: fix 'emitLegacyCommonJSImports' does not exist in type 'GenerateOptions'
288
- emitLegacyCommonJSImports: shouldEmitLegacyCommonJSImports(config, outputArgs.filename),
292
+ importExtension,
293
+ emitLegacyCommonJSImports: config.emitLegacyCommonJSImports ?? true,
289
294
  cache,
290
295
  });
291
296
  result.push({
@@ -315,7 +320,7 @@ export async function executeCodegen(input) {
315
320
  exitOnError: false,
316
321
  };
317
322
  });
318
- return task.newListr(generateTasks, { concurrent: cpus().length });
323
+ return task.newListr(generateTasks, { concurrent: cpus().length || 1 });
319
324
  },
320
325
  },
321
326
  ], {
package/esm/config.js CHANGED
@@ -165,6 +165,18 @@ export function buildOptions() {
165
165
  type: 'boolean',
166
166
  default: false,
167
167
  },
168
+ 'emit-legacy-common-js-imports': {
169
+ describe: 'Emit legacy CommonJS imports (deprecated, use import-extension instead)',
170
+ type: 'boolean',
171
+ },
172
+ 'import-extension': {
173
+ describe: 'Extension to append to imports (e.g., .js, .mjs, or empty string for no extension)',
174
+ type: 'string',
175
+ },
176
+ 'ignore-no-documents': {
177
+ describe: 'Suppress errors for no documents',
178
+ type: 'boolean',
179
+ },
168
180
  };
169
181
  }
170
182
  export function parseArgv(argv = process.argv) {
@@ -212,6 +224,9 @@ export function updateContextWithCliFlags(context, cliFlags) {
212
224
  // for some reason parsed value is `'false'` string so this ensure it always is a boolean.
213
225
  config.emitLegacyCommonJSImports = cliFlags['emit-legacy-common-js-imports'] === true;
214
226
  }
227
+ if (cliFlags['import-extension'] !== undefined) {
228
+ config.importExtension = cliFlags['import-extension'];
229
+ }
215
230
  if (cliFlags.project) {
216
231
  context.useProject(cliFlags.project);
217
232
  }
@@ -338,15 +353,3 @@ function addHashToDocumentFiles(documentFilesPromise) {
338
353
  return doc;
339
354
  }));
340
355
  }
341
- export function shouldEmitLegacyCommonJSImports(config) {
342
- const globalValue = config.emitLegacyCommonJSImports === undefined ? true : !!config.emitLegacyCommonJSImports;
343
- // const outputConfig = config.generates[outputPath];
344
- // if (!outputConfig) {
345
- // debugLog(`Couldn't find a config of ${outputPath}`);
346
- // return globalValue;
347
- // }
348
- // if (isConfiguredOutput(outputConfig) && typeof outputConfig.emitLegacyCommonJSImports === 'boolean') {
349
- // return outputConfig.emitLegacyCommonJSImports;
350
- // }
351
- return globalValue;
352
- }
@@ -44,7 +44,21 @@ export const createWatcher = (initialContext, onNext) => {
44
44
  const debouncedExec = debounce(() => {
45
45
  if (!isShutdown) {
46
46
  executeCodegen(initialContext)
47
- .then(({ result }) => onNext(result), () => Promise.resolve())
47
+ .then(({ result, error }) => {
48
+ // FIXME: this is a quick fix to stop `onNext` (writeOutput) from
49
+ // removing all files when there is an error.
50
+ //
51
+ // This is because `removeStaleFiles()` will remove files if the
52
+ // generated files are different between runs. And on error, it
53
+ // returns an empty array i.e. will remove all generated files from
54
+ // the previous run
55
+ //
56
+ // This also means we don't have config.allowPartialOutputs in watch mode
57
+ if (error) {
58
+ return;
59
+ }
60
+ onNext(result);
61
+ }, () => Promise.resolve())
48
62
  .then(() => emitWatching(watchDirectory));
49
63
  }
50
64
  }, 100);
@@ -142,7 +156,14 @@ export const createWatcher = (initialContext, onNext) => {
142
156
  */
143
157
  stopWatching.runningWatcher = new Promise((resolve, reject) => {
144
158
  executeCodegen(initialContext)
145
- .then(({ result }) => onNext(result), () => Promise.resolve())
159
+ .then(({ result, error }) => {
160
+ // TODO: this is the initial run, the logic here mimics the above watcher logic.
161
+ // We need to check whether it's ok to deviate between these two.
162
+ if (error) {
163
+ return;
164
+ }
165
+ onNext(result);
166
+ }, () => Promise.resolve())
146
167
  .then(() => runWatcher(abortController.signal))
147
168
  .catch(err => {
148
169
  watcherSubscription.unsubscribe();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "6.0.0",
3
+ "version": "6.1.1",
4
4
  "peerDependenciesMeta": {
5
5
  "@parcel/watcher": {
6
6
  "optional": true
@@ -14,17 +14,17 @@
14
14
  "@babel/generator": "^7.18.13",
15
15
  "@babel/template": "^7.18.10",
16
16
  "@babel/types": "^7.18.13",
17
- "@graphql-codegen/client-preset": "^5.0.0",
17
+ "@graphql-codegen/client-preset": "^5.2.0",
18
18
  "@graphql-codegen/core": "^5.0.0",
19
- "@graphql-codegen/plugin-helpers": "^6.0.0",
19
+ "@graphql-codegen/plugin-helpers": "^6.1.0",
20
20
  "@graphql-tools/apollo-engine-loader": "^8.0.0",
21
21
  "@graphql-tools/code-file-loader": "^8.0.0",
22
22
  "@graphql-tools/git-loader": "^8.0.0",
23
- "@graphql-tools/github-loader": "^8.0.0",
23
+ "@graphql-tools/github-loader": "^9.0.0",
24
24
  "@graphql-tools/graphql-file-loader": "^8.0.0",
25
25
  "@graphql-tools/json-file-loader": "^8.0.0",
26
26
  "@graphql-tools/load": "^8.1.0",
27
- "@graphql-tools/url-loader": "^8.0.0",
27
+ "@graphql-tools/url-loader": "^9.0.0",
28
28
  "@graphql-tools/utils": "^10.0.0",
29
29
  "@inquirer/prompts": "^7.8.2",
30
30
  "@whatwg-node/fetch": "^0.10.0",
@@ -16,6 +16,7 @@ export type YamlCliFlags = {
16
16
  debug?: boolean;
17
17
  ignoreNoDocuments?: boolean;
18
18
  emitLegacyCommonJSImports?: boolean;
19
+ importExtension?: '' | `.${string}`;
19
20
  };
20
21
  export declare function generateSearchPlaces(moduleName: string): string[];
21
22
  export type CodegenConfigLoader = (filepath: string, content: string) => Promise<Types.Config> | Types.Config;
@@ -103,6 +104,18 @@ export declare function buildOptions(): {
103
104
  type: "boolean";
104
105
  default: boolean;
105
106
  };
107
+ 'emit-legacy-common-js-imports': {
108
+ describe: string;
109
+ type: "boolean";
110
+ };
111
+ 'import-extension': {
112
+ describe: string;
113
+ type: "string";
114
+ };
115
+ 'ignore-no-documents': {
116
+ describe: string;
117
+ type: "boolean";
118
+ };
106
119
  };
107
120
  export declare function parseArgv(argv?: string[]): YamlCliFlags;
108
121
  export declare function createContext(cliFlags?: YamlCliFlags): Promise<CodegenContext>;
@@ -137,4 +150,3 @@ export declare class CodegenContext {
137
150
  loadDocuments(pointer: Types.OperationDocument[]): Promise<Types.DocumentFile[]>;
138
151
  }
139
152
  export declare function ensureContext(input: CodegenContext | Types.Config): CodegenContext;
140
- export declare function shouldEmitLegacyCommonJSImports(config: Types.Config): boolean;
@@ -16,6 +16,7 @@ export type YamlCliFlags = {
16
16
  debug?: boolean;
17
17
  ignoreNoDocuments?: boolean;
18
18
  emitLegacyCommonJSImports?: boolean;
19
+ importExtension?: '' | `.${string}`;
19
20
  };
20
21
  export declare function generateSearchPlaces(moduleName: string): string[];
21
22
  export type CodegenConfigLoader = (filepath: string, content: string) => Promise<Types.Config> | Types.Config;
@@ -103,6 +104,18 @@ export declare function buildOptions(): {
103
104
  type: "boolean";
104
105
  default: boolean;
105
106
  };
107
+ 'emit-legacy-common-js-imports': {
108
+ describe: string;
109
+ type: "boolean";
110
+ };
111
+ 'import-extension': {
112
+ describe: string;
113
+ type: "string";
114
+ };
115
+ 'ignore-no-documents': {
116
+ describe: string;
117
+ type: "boolean";
118
+ };
106
119
  };
107
120
  export declare function parseArgv(argv?: string[]): YamlCliFlags;
108
121
  export declare function createContext(cliFlags?: YamlCliFlags): Promise<CodegenContext>;
@@ -137,4 +150,3 @@ export declare class CodegenContext {
137
150
  loadDocuments(pointer: Types.OperationDocument[]): Promise<Types.DocumentFile[]>;
138
151
  }
139
152
  export declare function ensureContext(input: CodegenContext | Types.Config): CodegenContext;
140
- export declare function shouldEmitLegacyCommonJSImports(config: Types.Config): boolean;
@@ -2,4 +2,9 @@ import { Types } from '@graphql-codegen/plugin-helpers';
2
2
  import { CodegenContext } from './config.cjs';
3
3
  export declare function generate(input: CodegenContext | (Types.Config & {
4
4
  cwd?: string;
5
- }), saveToFile?: boolean): Promise<Types.FileOutput[] | any>;
5
+ }), saveToFile?: boolean): Promise<Types.FileOutput[]
6
+ /**
7
+ * When this function runs in watch mode, it'd return an empty promise that doesn't resolve until the watcher exits
8
+ * FIXME: this effectively makes the result `any`, which loses type-hints
9
+ */
10
+ | any>;
@@ -2,4 +2,9 @@ import { Types } from '@graphql-codegen/plugin-helpers';
2
2
  import { CodegenContext } from './config.js';
3
3
  export declare function generate(input: CodegenContext | (Types.Config & {
4
4
  cwd?: string;
5
- }), saveToFile?: boolean): Promise<Types.FileOutput[] | any>;
5
+ }), saveToFile?: boolean): Promise<Types.FileOutput[]
6
+ /**
7
+ * When this function runs in watch mode, it'd return an empty promise that doesn't resolve until the watcher exits
8
+ * FIXME: this effectively makes the result `any`, which loses type-hints
9
+ */
10
+ | any>;