@graphql-codegen/cli 2.4.0-alpha-23d229715.0 → 2.4.0-alpha-6cdbcf02e.0
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/bin.js +47 -54
- package/config.d.ts +1 -9
- package/index.js +47 -54
- package/index.mjs +49 -56
- package/package.json +3 -3
package/bin.js
CHANGED
|
@@ -505,19 +505,34 @@ async function loadSchema(schemaPointers, config) {
|
|
|
505
505
|
|
|
506
506
|
${e.message || e}
|
|
507
507
|
${e.stack || ''}
|
|
508
|
-
|
|
508
|
+
|
|
509
509
|
GraphQL Code Generator supports:
|
|
510
510
|
- ES Modules and CommonJS exports (export as default or named export "schema")
|
|
511
511
|
- Introspection JSON File
|
|
512
512
|
- URL of GraphQL endpoint
|
|
513
513
|
- Multiple files with type definitions (glob expression)
|
|
514
514
|
- String in config file
|
|
515
|
-
|
|
515
|
+
|
|
516
516
|
Try to use one of above options and run codegen again.
|
|
517
|
-
|
|
517
|
+
|
|
518
518
|
`);
|
|
519
519
|
}
|
|
520
520
|
}
|
|
521
|
+
const adjustRequirePointers = (documentPointers) => {
|
|
522
|
+
const adjustSinglePointer = (pointer) => {
|
|
523
|
+
if (typeof pointer === 'object' && 'require' in pointer) {
|
|
524
|
+
const config = 'config' in pointer ? pointer.config : {};
|
|
525
|
+
return { [pointer['require']]: config };
|
|
526
|
+
}
|
|
527
|
+
return pointer;
|
|
528
|
+
};
|
|
529
|
+
if (Array.isArray(documentPointers)) {
|
|
530
|
+
return documentPointers.map(adjustSinglePointer);
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
return adjustSinglePointer(documentPointers);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
521
536
|
async function loadDocuments(documentPointers, config) {
|
|
522
537
|
const loaders = [
|
|
523
538
|
new codeFileLoader.CodeFileLoader({
|
|
@@ -537,7 +552,7 @@ async function loadDocuments(documentPointers, config) {
|
|
|
537
552
|
}
|
|
538
553
|
ignore.push(path.join(process.cwd(), generatePath));
|
|
539
554
|
}
|
|
540
|
-
const loadedFromToolkit = await load.loadDocuments(documentPointers, {
|
|
555
|
+
const loadedFromToolkit = await load.loadDocuments(adjustRequirePointers(documentPointers), {
|
|
541
556
|
...defaultDocumentsLoadOptions,
|
|
542
557
|
ignore,
|
|
543
558
|
loaders,
|
|
@@ -676,10 +691,6 @@ function buildOptions() {
|
|
|
676
691
|
describe: 'Only print errors',
|
|
677
692
|
type: 'boolean',
|
|
678
693
|
},
|
|
679
|
-
profile: {
|
|
680
|
-
describe: 'Use profiler to measure performance',
|
|
681
|
-
type: 'boolean',
|
|
682
|
-
},
|
|
683
694
|
p: {
|
|
684
695
|
alias: 'project',
|
|
685
696
|
describe: 'Name of a project in GraphQL Config',
|
|
@@ -721,9 +732,6 @@ function updateContextWithCliFlags(context, cliFlags) {
|
|
|
721
732
|
if (cliFlags.project) {
|
|
722
733
|
context.useProject(cliFlags.project);
|
|
723
734
|
}
|
|
724
|
-
if (cliFlags.profile === true) {
|
|
725
|
-
context.useProfiler();
|
|
726
|
-
}
|
|
727
735
|
context.updateConfig(config);
|
|
728
736
|
}
|
|
729
737
|
class CodegenContext {
|
|
@@ -733,7 +741,6 @@ class CodegenContext {
|
|
|
733
741
|
this._graphqlConfig = graphqlConfig;
|
|
734
742
|
this.filepath = this._graphqlConfig ? this._graphqlConfig.filepath : filepath;
|
|
735
743
|
this.cwd = this._graphqlConfig ? this._graphqlConfig.dirpath : process.cwd();
|
|
736
|
-
this.profiler = pluginHelpers.createNoopProfiler();
|
|
737
744
|
}
|
|
738
745
|
useProject(name) {
|
|
739
746
|
this._project = name;
|
|
@@ -764,13 +771,6 @@ class CodegenContext {
|
|
|
764
771
|
...config,
|
|
765
772
|
};
|
|
766
773
|
}
|
|
767
|
-
useProfiler() {
|
|
768
|
-
this.profiler = pluginHelpers.createProfiler();
|
|
769
|
-
const now = new Date(); // 2011-10-05T14:48:00.000Z
|
|
770
|
-
const datetime = now.toISOString().split('.')[0]; // 2011-10-05T14:48:00
|
|
771
|
-
const datetimeNormalized = datetime.replace(/-|:/g, ''); // 20111005T144800
|
|
772
|
-
this.profilerOutput = `codegen-${datetimeNormalized}.json`;
|
|
773
|
-
}
|
|
774
774
|
getPluginContext() {
|
|
775
775
|
return this._pluginContext;
|
|
776
776
|
}
|
|
@@ -806,6 +806,19 @@ const makeDefaultLoader = (from) => {
|
|
|
806
806
|
};
|
|
807
807
|
};
|
|
808
808
|
async function executeCodegen(input) {
|
|
809
|
+
function wrapTask(task, source) {
|
|
810
|
+
return async () => {
|
|
811
|
+
try {
|
|
812
|
+
await Promise.resolve().then(() => task());
|
|
813
|
+
}
|
|
814
|
+
catch (error) {
|
|
815
|
+
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
816
|
+
error.source = source;
|
|
817
|
+
}
|
|
818
|
+
throw error;
|
|
819
|
+
}
|
|
820
|
+
};
|
|
821
|
+
}
|
|
809
822
|
const context = ensureContext(input);
|
|
810
823
|
const config = context.getConfig();
|
|
811
824
|
const pluginContext = context.getPluginContext();
|
|
@@ -841,21 +854,6 @@ async function executeCodegen(input) {
|
|
|
841
854
|
let rootSchemas;
|
|
842
855
|
let rootDocuments;
|
|
843
856
|
const generates = {};
|
|
844
|
-
function wrapTask(task, source, taskName) {
|
|
845
|
-
return () => {
|
|
846
|
-
return context.profiler.run(async () => {
|
|
847
|
-
try {
|
|
848
|
-
await Promise.resolve().then(() => task());
|
|
849
|
-
}
|
|
850
|
-
catch (error) {
|
|
851
|
-
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
852
|
-
error.source = source;
|
|
853
|
-
}
|
|
854
|
-
throw error;
|
|
855
|
-
}
|
|
856
|
-
}, taskName);
|
|
857
|
-
};
|
|
858
|
-
}
|
|
859
857
|
async function normalize() {
|
|
860
858
|
/* Load Require extensions */
|
|
861
859
|
const requireExtensions = pluginHelpers.normalizeInstanceOrArray(config.require);
|
|
@@ -959,7 +957,7 @@ async function executeCodegen(input) {
|
|
|
959
957
|
}
|
|
960
958
|
outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
961
959
|
outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
962
|
-
}, filename
|
|
960
|
+
}, filename),
|
|
963
961
|
},
|
|
964
962
|
{
|
|
965
963
|
title: 'Load GraphQL documents',
|
|
@@ -970,7 +968,7 @@ async function executeCodegen(input) {
|
|
|
970
968
|
if (documents.length > 0) {
|
|
971
969
|
outputDocuments.push(...documents);
|
|
972
970
|
}
|
|
973
|
-
}, filename
|
|
971
|
+
}, filename),
|
|
974
972
|
},
|
|
975
973
|
{
|
|
976
974
|
title: 'Generate',
|
|
@@ -998,7 +996,7 @@ async function executeCodegen(input) {
|
|
|
998
996
|
};
|
|
999
997
|
let outputs = [];
|
|
1000
998
|
if (hasPreset) {
|
|
1001
|
-
outputs = await
|
|
999
|
+
outputs = await preset.buildGeneratesSection({
|
|
1002
1000
|
baseOutputDir: filename,
|
|
1003
1001
|
presetConfig: outputConfig.presetConfig || {},
|
|
1004
1002
|
plugins: normalizedPluginsArray,
|
|
@@ -1008,8 +1006,7 @@ async function executeCodegen(input) {
|
|
|
1008
1006
|
config: mergedConfig,
|
|
1009
1007
|
pluginMap,
|
|
1010
1008
|
pluginContext,
|
|
1011
|
-
|
|
1012
|
-
}), `Build Generates Section: ${filename}`);
|
|
1009
|
+
});
|
|
1013
1010
|
}
|
|
1014
1011
|
else {
|
|
1015
1012
|
outputs = [
|
|
@@ -1022,7 +1019,6 @@ async function executeCodegen(input) {
|
|
|
1022
1019
|
config: mergedConfig,
|
|
1023
1020
|
pluginMap,
|
|
1024
1021
|
pluginContext,
|
|
1025
|
-
profiler: context.profiler,
|
|
1026
1022
|
},
|
|
1027
1023
|
];
|
|
1028
1024
|
}
|
|
@@ -1034,8 +1030,8 @@ async function executeCodegen(input) {
|
|
|
1034
1030
|
hooks: outputConfig.hooks || {},
|
|
1035
1031
|
});
|
|
1036
1032
|
};
|
|
1037
|
-
await
|
|
1038
|
-
}, filename
|
|
1033
|
+
await Promise.all(outputs.map(process));
|
|
1034
|
+
}, filename),
|
|
1039
1035
|
},
|
|
1040
1036
|
], {
|
|
1041
1037
|
// it stops when one of tasks failed
|
|
@@ -1202,7 +1198,7 @@ const hash = (content) => crypto.createHash('sha1').update(content).digest('base
|
|
|
1202
1198
|
async function generate(input, saveToFile = true) {
|
|
1203
1199
|
const context = ensureContext(input);
|
|
1204
1200
|
const config = context.getConfig();
|
|
1205
|
-
await
|
|
1201
|
+
await lifecycleHooks(config.hooks).afterStart();
|
|
1206
1202
|
let previouslyGeneratedFilenames = [];
|
|
1207
1203
|
function removeStaleFiles(config, generationResult) {
|
|
1208
1204
|
const filenames = generationResult.map(o => o.filename);
|
|
@@ -1231,8 +1227,8 @@ async function generate(input, saveToFile = true) {
|
|
|
1231
1227
|
if (config.watch) {
|
|
1232
1228
|
removeStaleFiles(config, generationResult);
|
|
1233
1229
|
}
|
|
1234
|
-
await
|
|
1235
|
-
await
|
|
1230
|
+
await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
|
|
1231
|
+
await Promise.all(generationResult.map(async (result) => {
|
|
1236
1232
|
const exists = fileExists(result.filename);
|
|
1237
1233
|
if (!shouldOverwrite(config, result.filename) && exists) {
|
|
1238
1234
|
return;
|
|
@@ -1244,7 +1240,7 @@ async function generate(input, saveToFile = true) {
|
|
|
1244
1240
|
previousHash = hash(readSync(result.filename));
|
|
1245
1241
|
}
|
|
1246
1242
|
if (previousHash && currentHash === previousHash) {
|
|
1247
|
-
debugLog(`Skipping file (${result.filename}) writing due to
|
|
1243
|
+
debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
|
|
1248
1244
|
return;
|
|
1249
1245
|
}
|
|
1250
1246
|
if (content.length === 0) {
|
|
@@ -1261,20 +1257,17 @@ async function generate(input, saveToFile = true) {
|
|
|
1261
1257
|
writeSync(absolutePath, result.content);
|
|
1262
1258
|
await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
|
|
1263
1259
|
await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
|
|
1264
|
-
}))
|
|
1265
|
-
await
|
|
1260
|
+
}));
|
|
1261
|
+
await lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename));
|
|
1266
1262
|
return generationResult;
|
|
1267
1263
|
}
|
|
1268
1264
|
// watch mode
|
|
1269
1265
|
if (config.watch) {
|
|
1270
1266
|
return createWatcher(context, writeOutput);
|
|
1271
1267
|
}
|
|
1272
|
-
const outputFiles = await
|
|
1273
|
-
await
|
|
1274
|
-
|
|
1275
|
-
if (context.profilerOutput) {
|
|
1276
|
-
writeSync(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
|
|
1277
|
-
}
|
|
1268
|
+
const outputFiles = await executeCodegen(context);
|
|
1269
|
+
await writeOutput(outputFiles);
|
|
1270
|
+
lifecycleHooks(config.hooks).beforeDone();
|
|
1278
1271
|
return outputFiles;
|
|
1279
1272
|
}
|
|
1280
1273
|
function shouldOverwrite(config, outputPath) {
|
package/config.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Types
|
|
1
|
+
import { Types } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { GraphQLConfig } from 'graphql-config';
|
|
3
3
|
import { GraphQLSchema } from 'graphql';
|
|
4
4
|
export declare type YamlCliFlags = {
|
|
@@ -9,7 +9,6 @@ export declare type YamlCliFlags = {
|
|
|
9
9
|
project: string;
|
|
10
10
|
silent: boolean;
|
|
11
11
|
errorsOnly: boolean;
|
|
12
|
-
profile: boolean;
|
|
13
12
|
};
|
|
14
13
|
export declare function generateSearchPlaces(moduleName: string): string[];
|
|
15
14
|
export interface LoadCodegenConfigOptions {
|
|
@@ -75,10 +74,6 @@ export declare function buildOptions(): {
|
|
|
75
74
|
describe: string;
|
|
76
75
|
type: "boolean";
|
|
77
76
|
};
|
|
78
|
-
profile: {
|
|
79
|
-
describe: string;
|
|
80
|
-
type: "boolean";
|
|
81
|
-
};
|
|
82
77
|
p: {
|
|
83
78
|
alias: string;
|
|
84
79
|
describe: string;
|
|
@@ -96,8 +91,6 @@ export declare class CodegenContext {
|
|
|
96
91
|
private _pluginContext;
|
|
97
92
|
cwd: string;
|
|
98
93
|
filepath: string;
|
|
99
|
-
profiler: Profiler;
|
|
100
|
-
profilerOutput?: string;
|
|
101
94
|
constructor({ config, graphqlConfig, filepath, }: {
|
|
102
95
|
config?: Types.Config;
|
|
103
96
|
graphqlConfig?: GraphQLConfig;
|
|
@@ -106,7 +99,6 @@ export declare class CodegenContext {
|
|
|
106
99
|
useProject(name?: string): void;
|
|
107
100
|
getConfig<T>(extraConfig?: T): T & Types.Config;
|
|
108
101
|
updateConfig(config: Partial<Types.Config>): void;
|
|
109
|
-
useProfiler(): void;
|
|
110
102
|
getPluginContext(): {
|
|
111
103
|
[key: string]: any;
|
|
112
104
|
};
|
package/index.js
CHANGED
|
@@ -423,19 +423,34 @@ async function loadSchema(schemaPointers, config) {
|
|
|
423
423
|
|
|
424
424
|
${e.message || e}
|
|
425
425
|
${e.stack || ''}
|
|
426
|
-
|
|
426
|
+
|
|
427
427
|
GraphQL Code Generator supports:
|
|
428
428
|
- ES Modules and CommonJS exports (export as default or named export "schema")
|
|
429
429
|
- Introspection JSON File
|
|
430
430
|
- URL of GraphQL endpoint
|
|
431
431
|
- Multiple files with type definitions (glob expression)
|
|
432
432
|
- String in config file
|
|
433
|
-
|
|
433
|
+
|
|
434
434
|
Try to use one of above options and run codegen again.
|
|
435
|
-
|
|
435
|
+
|
|
436
436
|
`);
|
|
437
437
|
}
|
|
438
438
|
}
|
|
439
|
+
const adjustRequirePointers = (documentPointers) => {
|
|
440
|
+
const adjustSinglePointer = (pointer) => {
|
|
441
|
+
if (typeof pointer === 'object' && 'require' in pointer) {
|
|
442
|
+
const config = 'config' in pointer ? pointer.config : {};
|
|
443
|
+
return { [pointer['require']]: config };
|
|
444
|
+
}
|
|
445
|
+
return pointer;
|
|
446
|
+
};
|
|
447
|
+
if (Array.isArray(documentPointers)) {
|
|
448
|
+
return documentPointers.map(adjustSinglePointer);
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
return adjustSinglePointer(documentPointers);
|
|
452
|
+
}
|
|
453
|
+
};
|
|
439
454
|
async function loadDocuments(documentPointers, config) {
|
|
440
455
|
const loaders = [
|
|
441
456
|
new codeFileLoader.CodeFileLoader({
|
|
@@ -455,7 +470,7 @@ async function loadDocuments(documentPointers, config) {
|
|
|
455
470
|
}
|
|
456
471
|
ignore.push(path.join(process.cwd(), generatePath));
|
|
457
472
|
}
|
|
458
|
-
const loadedFromToolkit = await load.loadDocuments(documentPointers, {
|
|
473
|
+
const loadedFromToolkit = await load.loadDocuments(adjustRequirePointers(documentPointers), {
|
|
459
474
|
...defaultDocumentsLoadOptions,
|
|
460
475
|
ignore,
|
|
461
476
|
loaders,
|
|
@@ -594,10 +609,6 @@ function buildOptions() {
|
|
|
594
609
|
describe: 'Only print errors',
|
|
595
610
|
type: 'boolean',
|
|
596
611
|
},
|
|
597
|
-
profile: {
|
|
598
|
-
describe: 'Use profiler to measure performance',
|
|
599
|
-
type: 'boolean',
|
|
600
|
-
},
|
|
601
612
|
p: {
|
|
602
613
|
alias: 'project',
|
|
603
614
|
describe: 'Name of a project in GraphQL Config',
|
|
@@ -639,9 +650,6 @@ function updateContextWithCliFlags(context, cliFlags) {
|
|
|
639
650
|
if (cliFlags.project) {
|
|
640
651
|
context.useProject(cliFlags.project);
|
|
641
652
|
}
|
|
642
|
-
if (cliFlags.profile === true) {
|
|
643
|
-
context.useProfiler();
|
|
644
|
-
}
|
|
645
653
|
context.updateConfig(config);
|
|
646
654
|
}
|
|
647
655
|
class CodegenContext {
|
|
@@ -651,7 +659,6 @@ class CodegenContext {
|
|
|
651
659
|
this._graphqlConfig = graphqlConfig;
|
|
652
660
|
this.filepath = this._graphqlConfig ? this._graphqlConfig.filepath : filepath;
|
|
653
661
|
this.cwd = this._graphqlConfig ? this._graphqlConfig.dirpath : process.cwd();
|
|
654
|
-
this.profiler = pluginHelpers.createNoopProfiler();
|
|
655
662
|
}
|
|
656
663
|
useProject(name) {
|
|
657
664
|
this._project = name;
|
|
@@ -682,13 +689,6 @@ class CodegenContext {
|
|
|
682
689
|
...config,
|
|
683
690
|
};
|
|
684
691
|
}
|
|
685
|
-
useProfiler() {
|
|
686
|
-
this.profiler = pluginHelpers.createProfiler();
|
|
687
|
-
const now = new Date(); // 2011-10-05T14:48:00.000Z
|
|
688
|
-
const datetime = now.toISOString().split('.')[0]; // 2011-10-05T14:48:00
|
|
689
|
-
const datetimeNormalized = datetime.replace(/-|:/g, ''); // 20111005T144800
|
|
690
|
-
this.profilerOutput = `codegen-${datetimeNormalized}.json`;
|
|
691
|
-
}
|
|
692
692
|
getPluginContext() {
|
|
693
693
|
return this._pluginContext;
|
|
694
694
|
}
|
|
@@ -724,6 +724,19 @@ const makeDefaultLoader = (from) => {
|
|
|
724
724
|
};
|
|
725
725
|
};
|
|
726
726
|
async function executeCodegen(input) {
|
|
727
|
+
function wrapTask(task, source) {
|
|
728
|
+
return async () => {
|
|
729
|
+
try {
|
|
730
|
+
await Promise.resolve().then(() => task());
|
|
731
|
+
}
|
|
732
|
+
catch (error) {
|
|
733
|
+
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
734
|
+
error.source = source;
|
|
735
|
+
}
|
|
736
|
+
throw error;
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
}
|
|
727
740
|
const context = ensureContext(input);
|
|
728
741
|
const config = context.getConfig();
|
|
729
742
|
const pluginContext = context.getPluginContext();
|
|
@@ -759,21 +772,6 @@ async function executeCodegen(input) {
|
|
|
759
772
|
let rootSchemas;
|
|
760
773
|
let rootDocuments;
|
|
761
774
|
const generates = {};
|
|
762
|
-
function wrapTask(task, source, taskName) {
|
|
763
|
-
return () => {
|
|
764
|
-
return context.profiler.run(async () => {
|
|
765
|
-
try {
|
|
766
|
-
await Promise.resolve().then(() => task());
|
|
767
|
-
}
|
|
768
|
-
catch (error) {
|
|
769
|
-
if (source && !(error instanceof graphql.GraphQLError)) {
|
|
770
|
-
error.source = source;
|
|
771
|
-
}
|
|
772
|
-
throw error;
|
|
773
|
-
}
|
|
774
|
-
}, taskName);
|
|
775
|
-
};
|
|
776
|
-
}
|
|
777
775
|
async function normalize() {
|
|
778
776
|
/* Load Require extensions */
|
|
779
777
|
const requireExtensions = pluginHelpers.normalizeInstanceOrArray(config.require);
|
|
@@ -877,7 +875,7 @@ async function executeCodegen(input) {
|
|
|
877
875
|
}
|
|
878
876
|
outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
879
877
|
outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
880
|
-
}, filename
|
|
878
|
+
}, filename),
|
|
881
879
|
},
|
|
882
880
|
{
|
|
883
881
|
title: 'Load GraphQL documents',
|
|
@@ -888,7 +886,7 @@ async function executeCodegen(input) {
|
|
|
888
886
|
if (documents.length > 0) {
|
|
889
887
|
outputDocuments.push(...documents);
|
|
890
888
|
}
|
|
891
|
-
}, filename
|
|
889
|
+
}, filename),
|
|
892
890
|
},
|
|
893
891
|
{
|
|
894
892
|
title: 'Generate',
|
|
@@ -916,7 +914,7 @@ async function executeCodegen(input) {
|
|
|
916
914
|
};
|
|
917
915
|
let outputs = [];
|
|
918
916
|
if (hasPreset) {
|
|
919
|
-
outputs = await
|
|
917
|
+
outputs = await preset.buildGeneratesSection({
|
|
920
918
|
baseOutputDir: filename,
|
|
921
919
|
presetConfig: outputConfig.presetConfig || {},
|
|
922
920
|
plugins: normalizedPluginsArray,
|
|
@@ -926,8 +924,7 @@ async function executeCodegen(input) {
|
|
|
926
924
|
config: mergedConfig,
|
|
927
925
|
pluginMap,
|
|
928
926
|
pluginContext,
|
|
929
|
-
|
|
930
|
-
}), `Build Generates Section: ${filename}`);
|
|
927
|
+
});
|
|
931
928
|
}
|
|
932
929
|
else {
|
|
933
930
|
outputs = [
|
|
@@ -940,7 +937,6 @@ async function executeCodegen(input) {
|
|
|
940
937
|
config: mergedConfig,
|
|
941
938
|
pluginMap,
|
|
942
939
|
pluginContext,
|
|
943
|
-
profiler: context.profiler,
|
|
944
940
|
},
|
|
945
941
|
];
|
|
946
942
|
}
|
|
@@ -952,8 +948,8 @@ async function executeCodegen(input) {
|
|
|
952
948
|
hooks: outputConfig.hooks || {},
|
|
953
949
|
});
|
|
954
950
|
};
|
|
955
|
-
await
|
|
956
|
-
}, filename
|
|
951
|
+
await Promise.all(outputs.map(process));
|
|
952
|
+
}, filename),
|
|
957
953
|
},
|
|
958
954
|
], {
|
|
959
955
|
// it stops when one of tasks failed
|
|
@@ -1203,7 +1199,7 @@ const hash = (content) => crypto.createHash('sha1').update(content).digest('base
|
|
|
1203
1199
|
async function generate(input, saveToFile = true) {
|
|
1204
1200
|
const context = ensureContext(input);
|
|
1205
1201
|
const config = context.getConfig();
|
|
1206
|
-
await
|
|
1202
|
+
await lifecycleHooks(config.hooks).afterStart();
|
|
1207
1203
|
let previouslyGeneratedFilenames = [];
|
|
1208
1204
|
function removeStaleFiles(config, generationResult) {
|
|
1209
1205
|
const filenames = generationResult.map(o => o.filename);
|
|
@@ -1232,8 +1228,8 @@ async function generate(input, saveToFile = true) {
|
|
|
1232
1228
|
if (config.watch) {
|
|
1233
1229
|
removeStaleFiles(config, generationResult);
|
|
1234
1230
|
}
|
|
1235
|
-
await
|
|
1236
|
-
await
|
|
1231
|
+
await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
|
|
1232
|
+
await Promise.all(generationResult.map(async (result) => {
|
|
1237
1233
|
const exists = fileExists(result.filename);
|
|
1238
1234
|
if (!shouldOverwrite(config, result.filename) && exists) {
|
|
1239
1235
|
return;
|
|
@@ -1245,7 +1241,7 @@ async function generate(input, saveToFile = true) {
|
|
|
1245
1241
|
previousHash = hash(readSync(result.filename));
|
|
1246
1242
|
}
|
|
1247
1243
|
if (previousHash && currentHash === previousHash) {
|
|
1248
|
-
debugLog(`Skipping file (${result.filename}) writing due to
|
|
1244
|
+
debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
|
|
1249
1245
|
return;
|
|
1250
1246
|
}
|
|
1251
1247
|
if (content.length === 0) {
|
|
@@ -1262,20 +1258,17 @@ async function generate(input, saveToFile = true) {
|
|
|
1262
1258
|
writeSync(absolutePath, result.content);
|
|
1263
1259
|
await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
|
|
1264
1260
|
await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
|
|
1265
|
-
}))
|
|
1266
|
-
await
|
|
1261
|
+
}));
|
|
1262
|
+
await lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename));
|
|
1267
1263
|
return generationResult;
|
|
1268
1264
|
}
|
|
1269
1265
|
// watch mode
|
|
1270
1266
|
if (config.watch) {
|
|
1271
1267
|
return createWatcher(context, writeOutput);
|
|
1272
1268
|
}
|
|
1273
|
-
const outputFiles = await
|
|
1274
|
-
await
|
|
1275
|
-
|
|
1276
|
-
if (context.profilerOutput) {
|
|
1277
|
-
writeSync(path.join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
|
|
1278
|
-
}
|
|
1269
|
+
const outputFiles = await executeCodegen(context);
|
|
1270
|
+
await writeOutput(outputFiles);
|
|
1271
|
+
lifecycleHooks(config.hooks).beforeDone();
|
|
1279
1272
|
return outputFiles;
|
|
1280
1273
|
}
|
|
1281
1274
|
function shouldOverwrite(config, outputPath) {
|
package/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isDetailedError, DetailedError,
|
|
1
|
+
import { isDetailedError, DetailedError, normalizeInstanceOrArray, normalizeConfig, normalizeOutputParam, getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
|
|
2
2
|
import { codegen } from '@graphql-codegen/core';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import logSymbols from 'log-symbols';
|
|
@@ -24,7 +24,7 @@ import { JsonFileLoader } from '@graphql-tools/json-file-loader';
|
|
|
24
24
|
import { UrlLoader } from '@graphql-tools/url-loader';
|
|
25
25
|
import yaml from 'yaml';
|
|
26
26
|
import { createRequire } from 'module';
|
|
27
|
-
import fs, { promises,
|
|
27
|
+
import fs, { promises, readFileSync, writeFileSync, statSync, unlink } from 'fs';
|
|
28
28
|
import Listr from 'listr';
|
|
29
29
|
import { exec } from 'child_process';
|
|
30
30
|
import isGlob from 'is-glob';
|
|
@@ -396,19 +396,34 @@ async function loadSchema(schemaPointers, config) {
|
|
|
396
396
|
|
|
397
397
|
${e.message || e}
|
|
398
398
|
${e.stack || ''}
|
|
399
|
-
|
|
399
|
+
|
|
400
400
|
GraphQL Code Generator supports:
|
|
401
401
|
- ES Modules and CommonJS exports (export as default or named export "schema")
|
|
402
402
|
- Introspection JSON File
|
|
403
403
|
- URL of GraphQL endpoint
|
|
404
404
|
- Multiple files with type definitions (glob expression)
|
|
405
405
|
- String in config file
|
|
406
|
-
|
|
406
|
+
|
|
407
407
|
Try to use one of above options and run codegen again.
|
|
408
|
-
|
|
408
|
+
|
|
409
409
|
`);
|
|
410
410
|
}
|
|
411
411
|
}
|
|
412
|
+
const adjustRequirePointers = (documentPointers) => {
|
|
413
|
+
const adjustSinglePointer = (pointer) => {
|
|
414
|
+
if (typeof pointer === 'object' && 'require' in pointer) {
|
|
415
|
+
const config = 'config' in pointer ? pointer.config : {};
|
|
416
|
+
return { [pointer['require']]: config };
|
|
417
|
+
}
|
|
418
|
+
return pointer;
|
|
419
|
+
};
|
|
420
|
+
if (Array.isArray(documentPointers)) {
|
|
421
|
+
return documentPointers.map(adjustSinglePointer);
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
return adjustSinglePointer(documentPointers);
|
|
425
|
+
}
|
|
426
|
+
};
|
|
412
427
|
async function loadDocuments(documentPointers, config) {
|
|
413
428
|
const loaders = [
|
|
414
429
|
new CodeFileLoader({
|
|
@@ -428,7 +443,7 @@ async function loadDocuments(documentPointers, config) {
|
|
|
428
443
|
}
|
|
429
444
|
ignore.push(join(process.cwd(), generatePath));
|
|
430
445
|
}
|
|
431
|
-
const loadedFromToolkit = await loadDocuments$1(documentPointers, {
|
|
446
|
+
const loadedFromToolkit = await loadDocuments$1(adjustRequirePointers(documentPointers), {
|
|
432
447
|
...defaultDocumentsLoadOptions,
|
|
433
448
|
ignore,
|
|
434
449
|
loaders,
|
|
@@ -567,10 +582,6 @@ function buildOptions() {
|
|
|
567
582
|
describe: 'Only print errors',
|
|
568
583
|
type: 'boolean',
|
|
569
584
|
},
|
|
570
|
-
profile: {
|
|
571
|
-
describe: 'Use profiler to measure performance',
|
|
572
|
-
type: 'boolean',
|
|
573
|
-
},
|
|
574
585
|
p: {
|
|
575
586
|
alias: 'project',
|
|
576
587
|
describe: 'Name of a project in GraphQL Config',
|
|
@@ -612,9 +623,6 @@ function updateContextWithCliFlags(context, cliFlags) {
|
|
|
612
623
|
if (cliFlags.project) {
|
|
613
624
|
context.useProject(cliFlags.project);
|
|
614
625
|
}
|
|
615
|
-
if (cliFlags.profile === true) {
|
|
616
|
-
context.useProfiler();
|
|
617
|
-
}
|
|
618
626
|
context.updateConfig(config);
|
|
619
627
|
}
|
|
620
628
|
class CodegenContext {
|
|
@@ -624,7 +632,6 @@ class CodegenContext {
|
|
|
624
632
|
this._graphqlConfig = graphqlConfig;
|
|
625
633
|
this.filepath = this._graphqlConfig ? this._graphqlConfig.filepath : filepath;
|
|
626
634
|
this.cwd = this._graphqlConfig ? this._graphqlConfig.dirpath : process.cwd();
|
|
627
|
-
this.profiler = createNoopProfiler();
|
|
628
635
|
}
|
|
629
636
|
useProject(name) {
|
|
630
637
|
this._project = name;
|
|
@@ -655,13 +662,6 @@ class CodegenContext {
|
|
|
655
662
|
...config,
|
|
656
663
|
};
|
|
657
664
|
}
|
|
658
|
-
useProfiler() {
|
|
659
|
-
this.profiler = createProfiler();
|
|
660
|
-
const now = new Date(); // 2011-10-05T14:48:00.000Z
|
|
661
|
-
const datetime = now.toISOString().split('.')[0]; // 2011-10-05T14:48:00
|
|
662
|
-
const datetimeNormalized = datetime.replace(/-|:/g, ''); // 20111005T144800
|
|
663
|
-
this.profilerOutput = `codegen-${datetimeNormalized}.json`;
|
|
664
|
-
}
|
|
665
665
|
getPluginContext() {
|
|
666
666
|
return this._pluginContext;
|
|
667
667
|
}
|
|
@@ -697,6 +697,19 @@ const makeDefaultLoader = (from) => {
|
|
|
697
697
|
};
|
|
698
698
|
};
|
|
699
699
|
async function executeCodegen(input) {
|
|
700
|
+
function wrapTask(task, source) {
|
|
701
|
+
return async () => {
|
|
702
|
+
try {
|
|
703
|
+
await Promise.resolve().then(() => task());
|
|
704
|
+
}
|
|
705
|
+
catch (error) {
|
|
706
|
+
if (source && !(error instanceof GraphQLError)) {
|
|
707
|
+
error.source = source;
|
|
708
|
+
}
|
|
709
|
+
throw error;
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
}
|
|
700
713
|
const context = ensureContext(input);
|
|
701
714
|
const config = context.getConfig();
|
|
702
715
|
const pluginContext = context.getPluginContext();
|
|
@@ -732,21 +745,6 @@ async function executeCodegen(input) {
|
|
|
732
745
|
let rootSchemas;
|
|
733
746
|
let rootDocuments;
|
|
734
747
|
const generates = {};
|
|
735
|
-
function wrapTask(task, source, taskName) {
|
|
736
|
-
return () => {
|
|
737
|
-
return context.profiler.run(async () => {
|
|
738
|
-
try {
|
|
739
|
-
await Promise.resolve().then(() => task());
|
|
740
|
-
}
|
|
741
|
-
catch (error) {
|
|
742
|
-
if (source && !(error instanceof GraphQLError)) {
|
|
743
|
-
error.source = source;
|
|
744
|
-
}
|
|
745
|
-
throw error;
|
|
746
|
-
}
|
|
747
|
-
}, taskName);
|
|
748
|
-
};
|
|
749
|
-
}
|
|
750
748
|
async function normalize() {
|
|
751
749
|
/* Load Require extensions */
|
|
752
750
|
const requireExtensions = normalizeInstanceOrArray(config.require);
|
|
@@ -850,7 +848,7 @@ async function executeCodegen(input) {
|
|
|
850
848
|
}
|
|
851
849
|
outputSchemaAst = await context.loadSchema(schemaPointerMap);
|
|
852
850
|
outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
|
|
853
|
-
}, filename
|
|
851
|
+
}, filename),
|
|
854
852
|
},
|
|
855
853
|
{
|
|
856
854
|
title: 'Load GraphQL documents',
|
|
@@ -861,7 +859,7 @@ async function executeCodegen(input) {
|
|
|
861
859
|
if (documents.length > 0) {
|
|
862
860
|
outputDocuments.push(...documents);
|
|
863
861
|
}
|
|
864
|
-
}, filename
|
|
862
|
+
}, filename),
|
|
865
863
|
},
|
|
866
864
|
{
|
|
867
865
|
title: 'Generate',
|
|
@@ -889,7 +887,7 @@ async function executeCodegen(input) {
|
|
|
889
887
|
};
|
|
890
888
|
let outputs = [];
|
|
891
889
|
if (hasPreset) {
|
|
892
|
-
outputs = await
|
|
890
|
+
outputs = await preset.buildGeneratesSection({
|
|
893
891
|
baseOutputDir: filename,
|
|
894
892
|
presetConfig: outputConfig.presetConfig || {},
|
|
895
893
|
plugins: normalizedPluginsArray,
|
|
@@ -899,8 +897,7 @@ async function executeCodegen(input) {
|
|
|
899
897
|
config: mergedConfig,
|
|
900
898
|
pluginMap,
|
|
901
899
|
pluginContext,
|
|
902
|
-
|
|
903
|
-
}), `Build Generates Section: ${filename}`);
|
|
900
|
+
});
|
|
904
901
|
}
|
|
905
902
|
else {
|
|
906
903
|
outputs = [
|
|
@@ -913,7 +910,6 @@ async function executeCodegen(input) {
|
|
|
913
910
|
config: mergedConfig,
|
|
914
911
|
pluginMap,
|
|
915
912
|
pluginContext,
|
|
916
|
-
profiler: context.profiler,
|
|
917
913
|
},
|
|
918
914
|
];
|
|
919
915
|
}
|
|
@@ -925,8 +921,8 @@ async function executeCodegen(input) {
|
|
|
925
921
|
hooks: outputConfig.hooks || {},
|
|
926
922
|
});
|
|
927
923
|
};
|
|
928
|
-
await
|
|
929
|
-
}, filename
|
|
924
|
+
await Promise.all(outputs.map(process));
|
|
925
|
+
}, filename),
|
|
930
926
|
},
|
|
931
927
|
], {
|
|
932
928
|
// it stops when one of tasks failed
|
|
@@ -1176,7 +1172,7 @@ const hash = (content) => createHash('sha1').update(content).digest('base64');
|
|
|
1176
1172
|
async function generate(input, saveToFile = true) {
|
|
1177
1173
|
const context = ensureContext(input);
|
|
1178
1174
|
const config = context.getConfig();
|
|
1179
|
-
await
|
|
1175
|
+
await lifecycleHooks(config.hooks).afterStart();
|
|
1180
1176
|
let previouslyGeneratedFilenames = [];
|
|
1181
1177
|
function removeStaleFiles(config, generationResult) {
|
|
1182
1178
|
const filenames = generationResult.map(o => o.filename);
|
|
@@ -1205,8 +1201,8 @@ async function generate(input, saveToFile = true) {
|
|
|
1205
1201
|
if (config.watch) {
|
|
1206
1202
|
removeStaleFiles(config, generationResult);
|
|
1207
1203
|
}
|
|
1208
|
-
await
|
|
1209
|
-
await
|
|
1204
|
+
await lifecycleHooks(config.hooks).beforeAllFileWrite(generationResult.map(r => r.filename));
|
|
1205
|
+
await Promise.all(generationResult.map(async (result) => {
|
|
1210
1206
|
const exists = fileExists(result.filename);
|
|
1211
1207
|
if (!shouldOverwrite(config, result.filename) && exists) {
|
|
1212
1208
|
return;
|
|
@@ -1218,7 +1214,7 @@ async function generate(input, saveToFile = true) {
|
|
|
1218
1214
|
previousHash = hash(readSync(result.filename));
|
|
1219
1215
|
}
|
|
1220
1216
|
if (previousHash && currentHash === previousHash) {
|
|
1221
|
-
debugLog(`Skipping file (${result.filename}) writing due to
|
|
1217
|
+
debugLog(`Skipping file (${result.filename}) writing due to indentical hash...`);
|
|
1222
1218
|
return;
|
|
1223
1219
|
}
|
|
1224
1220
|
if (content.length === 0) {
|
|
@@ -1235,20 +1231,17 @@ async function generate(input, saveToFile = true) {
|
|
|
1235
1231
|
writeSync(absolutePath, result.content);
|
|
1236
1232
|
await lifecycleHooks(result.hooks).afterOneFileWrite(result.filename);
|
|
1237
1233
|
await lifecycleHooks(config.hooks).afterOneFileWrite(result.filename);
|
|
1238
|
-
}))
|
|
1239
|
-
await
|
|
1234
|
+
}));
|
|
1235
|
+
await lifecycleHooks(config.hooks).afterAllFileWrite(generationResult.map(r => r.filename));
|
|
1240
1236
|
return generationResult;
|
|
1241
1237
|
}
|
|
1242
1238
|
// watch mode
|
|
1243
1239
|
if (config.watch) {
|
|
1244
1240
|
return createWatcher(context, writeOutput);
|
|
1245
1241
|
}
|
|
1246
|
-
const outputFiles = await
|
|
1247
|
-
await
|
|
1248
|
-
|
|
1249
|
-
if (context.profilerOutput) {
|
|
1250
|
-
writeSync(join(context.cwd, context.profilerOutput), JSON.stringify(context.profiler.collect()));
|
|
1251
|
-
}
|
|
1242
|
+
const outputFiles = await executeCodegen(context);
|
|
1243
|
+
await writeOutput(outputFiles);
|
|
1244
|
+
lifecycleHooks(config.hooks).beforeDone();
|
|
1252
1245
|
return outputFiles;
|
|
1253
1246
|
}
|
|
1254
1247
|
function shouldOverwrite(config, outputPath) {
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/cli",
|
|
3
|
-
"version": "2.4.0-alpha-
|
|
3
|
+
"version": "2.4.0-alpha-6cdbcf02e.0",
|
|
4
4
|
"peerDependencies": {
|
|
5
5
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
6
6
|
},
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@graphql-codegen/core": "2.4.
|
|
9
|
-
"@graphql-codegen/plugin-helpers": "2.
|
|
8
|
+
"@graphql-codegen/core": "2.4.1-alpha-6cdbcf02e.0",
|
|
9
|
+
"@graphql-codegen/plugin-helpers": "2.3.3-alpha-6cdbcf02e.0",
|
|
10
10
|
"@graphql-tools/apollo-engine-loader": "^7.0.5",
|
|
11
11
|
"@graphql-tools/code-file-loader": "^7.0.6",
|
|
12
12
|
"@graphql-tools/git-loader": "^7.0.5",
|