@graphql-codegen/cli 2.3.1 → 2.5.0-alpha-bc33c031d.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 CHANGED
@@ -505,20 +505,30 @@ 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
- async function loadDocuments(documentPointers, config) {
521
+ const requiredLoader = async (pointers, schema) => {
522
+ return Promise.all(pointers.map(async (pointer) => {
523
+ const userLoader = (await makeDefaultLoader(process.cwd())(pointer.require)).default;
524
+ const documents = userLoader(pointer.config || {}, schema);
525
+ return documents;
526
+ })).then(results => [].concat(...results));
527
+ };
528
+ const isRequirePointer = (pointer) => {
529
+ return typeof pointer === 'object' && 'require' in pointer;
530
+ };
531
+ async function loadDocuments(documentPointers, config, schema) {
522
532
  const loaders = [
523
533
  new codeFileLoader.CodeFileLoader({
524
534
  pluckConfig: {
@@ -537,14 +547,35 @@ async function loadDocuments(documentPointers, config) {
537
547
  }
538
548
  ignore.push(path.join(process.cwd(), generatePath));
539
549
  }
540
- const loadedFromToolkit = await load.loadDocuments(documentPointers, {
550
+ const loaderPointers = [];
551
+ const requestPointers = [];
552
+ if (Array.isArray(documentPointers)) {
553
+ documentPointers.forEach(pointer => {
554
+ if (isRequirePointer(pointer)) {
555
+ requestPointers.push(pointer);
556
+ }
557
+ else {
558
+ loaderPointers.push(pointer);
559
+ }
560
+ });
561
+ }
562
+ else {
563
+ if (isRequirePointer(documentPointers)) {
564
+ requestPointers.push(documentPointers);
565
+ }
566
+ else {
567
+ loaderPointers.push(documentPointers);
568
+ }
569
+ }
570
+ const loadedFromToolkit = await load.loadDocuments(loaderPointers, {
541
571
  ...defaultDocumentsLoadOptions,
542
572
  ignore,
543
573
  loaders,
544
574
  ...config,
545
575
  ...config.config,
546
576
  });
547
- return loadedFromToolkit;
577
+ const loadedAlterantively = await requiredLoader(requestPointers, schema);
578
+ return [...loadedFromToolkit, ...loadedAlterantively];
548
579
  }
549
580
 
550
581
  const { lstat } = fs.promises;
@@ -767,14 +798,14 @@ class CodegenContext {
767
798
  }
768
799
  return loadSchema(pointer, config);
769
800
  }
770
- async loadDocuments(pointer) {
801
+ async loadDocuments(input) {
771
802
  const config = this.getConfig(defaultDocumentsLoadOptions);
772
803
  if (this._graphqlConfig) {
773
804
  // TODO: pointer won't work here
774
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
805
+ const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(input.pointer, config);
775
806
  return documents;
776
807
  }
777
- return loadDocuments(pointer, config);
808
+ return loadDocuments(input.pointer, config, input.schema);
778
809
  }
779
810
  }
780
811
  function ensureContext(input) {
@@ -790,6 +821,20 @@ const makeDefaultLoader = (from) => {
790
821
  return new Promise(function (resolve) { resolve(_interopNamespace(require(relativeRequire.resolve(mod)))); });
791
822
  };
792
823
  };
824
+ // TODO: Replace any with types
825
+ function createCache(loader) {
826
+ const cache = new Map();
827
+ return {
828
+ load(key) {
829
+ if (cache.has(key)) {
830
+ return cache.get(key);
831
+ }
832
+ const value = loader(key);
833
+ cache.set(key, value);
834
+ return value;
835
+ },
836
+ };
837
+ }
793
838
  async function executeCodegen(input) {
794
839
  function wrapTask(task, source) {
795
840
  return async () => {
@@ -839,6 +884,20 @@ async function executeCodegen(input) {
839
884
  let rootSchemas;
840
885
  let rootDocuments;
841
886
  const generates = {};
887
+ const schemaLoadingCache = createCache(async function (hash) {
888
+ const outputSchemaAst = await context.loadSchema(JSON.parse(hash));
889
+ const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
890
+ return {
891
+ outputSchemaAst: outputSchemaAst,
892
+ outputSchema: outputSchema,
893
+ };
894
+ });
895
+ const documentsLoadingCache = createCache(async function (hash) {
896
+ const documents = await context.loadDocuments(JSON.parse(hash));
897
+ return {
898
+ documents,
899
+ };
900
+ });
842
901
  async function normalize() {
843
902
  /* Load Require extensions */
844
903
  const requireExtensions = pluginHelpers.normalizeInstanceOrArray(config.require);
@@ -940,8 +999,10 @@ async function executeCodegen(input) {
940
999
  Object.assign(schemaPointerMap, unnormalizedPtr);
941
1000
  }
942
1001
  }
943
- outputSchemaAst = await context.loadSchema(schemaPointerMap);
944
- outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
1002
+ const hash = JSON.stringify(schemaPointerMap);
1003
+ const result = await schemaLoadingCache.load(hash);
1004
+ outputSchemaAst = await result.outputSchemaAst;
1005
+ outputSchema = result.outputSchema;
945
1006
  }, filename),
946
1007
  },
947
1008
  {
@@ -949,7 +1010,9 @@ async function executeCodegen(input) {
949
1010
  task: wrapTask(async () => {
950
1011
  debugLog(`[CLI] Loading Documents`);
951
1012
  const allDocuments = [...rootDocuments, ...outputSpecificDocuments];
952
- const documents = await context.loadDocuments(allDocuments);
1013
+ const hash = JSON.stringify({ pointer: allDocuments, schema: outputSchema });
1014
+ const result = await documentsLoadingCache.load(hash);
1015
+ const documents = result.documents;
953
1016
  if (documents.length > 0) {
954
1017
  outputDocuments.push(...documents);
955
1018
  }
package/codegen.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import { Types } from '@graphql-codegen/plugin-helpers';
2
2
  import { CodegenContext } from './config';
3
+ export declare const makeDefaultLoader: (from: string) => (mod: string) => Promise<any>;
3
4
  export declare function executeCodegen(input: CodegenContext | Types.Config): Promise<Types.FileOutput[]>;
package/config.d.ts CHANGED
@@ -103,6 +103,9 @@ export declare class CodegenContext {
103
103
  [key: string]: any;
104
104
  };
105
105
  loadSchema(pointer: Types.Schema): Promise<GraphQLSchema>;
106
- loadDocuments(pointer: Types.OperationDocument[]): Promise<Types.DocumentFile[]>;
106
+ loadDocuments(input: {
107
+ pointer: Types.OperationDocument[];
108
+ schema: GraphQLSchema;
109
+ }): Promise<Types.DocumentFile[]>;
107
110
  }
108
111
  export declare function ensureContext(input: CodegenContext | Types.Config): CodegenContext;
package/index.js CHANGED
@@ -423,20 +423,30 @@ 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
- async function loadDocuments(documentPointers, config) {
439
+ const requiredLoader = async (pointers, schema) => {
440
+ return Promise.all(pointers.map(async (pointer) => {
441
+ const userLoader = (await makeDefaultLoader(process.cwd())(pointer.require)).default;
442
+ const documents = userLoader(pointer.config || {}, schema);
443
+ return documents;
444
+ })).then(results => [].concat(...results));
445
+ };
446
+ const isRequirePointer = (pointer) => {
447
+ return typeof pointer === 'object' && 'require' in pointer;
448
+ };
449
+ async function loadDocuments(documentPointers, config, schema) {
440
450
  const loaders = [
441
451
  new codeFileLoader.CodeFileLoader({
442
452
  pluckConfig: {
@@ -455,14 +465,35 @@ async function loadDocuments(documentPointers, config) {
455
465
  }
456
466
  ignore.push(path.join(process.cwd(), generatePath));
457
467
  }
458
- const loadedFromToolkit = await load.loadDocuments(documentPointers, {
468
+ const loaderPointers = [];
469
+ const requestPointers = [];
470
+ if (Array.isArray(documentPointers)) {
471
+ documentPointers.forEach(pointer => {
472
+ if (isRequirePointer(pointer)) {
473
+ requestPointers.push(pointer);
474
+ }
475
+ else {
476
+ loaderPointers.push(pointer);
477
+ }
478
+ });
479
+ }
480
+ else {
481
+ if (isRequirePointer(documentPointers)) {
482
+ requestPointers.push(documentPointers);
483
+ }
484
+ else {
485
+ loaderPointers.push(documentPointers);
486
+ }
487
+ }
488
+ const loadedFromToolkit = await load.loadDocuments(loaderPointers, {
459
489
  ...defaultDocumentsLoadOptions,
460
490
  ignore,
461
491
  loaders,
462
492
  ...config,
463
493
  ...config.config,
464
494
  });
465
- return loadedFromToolkit;
495
+ const loadedAlterantively = await requiredLoader(requestPointers, schema);
496
+ return [...loadedFromToolkit, ...loadedAlterantively];
466
497
  }
467
498
 
468
499
  const { lstat } = fs.promises;
@@ -685,14 +716,14 @@ class CodegenContext {
685
716
  }
686
717
  return loadSchema(pointer, config);
687
718
  }
688
- async loadDocuments(pointer) {
719
+ async loadDocuments(input) {
689
720
  const config = this.getConfig(defaultDocumentsLoadOptions);
690
721
  if (this._graphqlConfig) {
691
722
  // TODO: pointer won't work here
692
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
723
+ const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(input.pointer, config);
693
724
  return documents;
694
725
  }
695
- return loadDocuments(pointer, config);
726
+ return loadDocuments(input.pointer, config, input.schema);
696
727
  }
697
728
  }
698
729
  function ensureContext(input) {
@@ -708,6 +739,20 @@ const makeDefaultLoader = (from) => {
708
739
  return new Promise(function (resolve) { resolve(_interopNamespace(require(relativeRequire.resolve(mod)))); });
709
740
  };
710
741
  };
742
+ // TODO: Replace any with types
743
+ function createCache(loader) {
744
+ const cache = new Map();
745
+ return {
746
+ load(key) {
747
+ if (cache.has(key)) {
748
+ return cache.get(key);
749
+ }
750
+ const value = loader(key);
751
+ cache.set(key, value);
752
+ return value;
753
+ },
754
+ };
755
+ }
711
756
  async function executeCodegen(input) {
712
757
  function wrapTask(task, source) {
713
758
  return async () => {
@@ -757,6 +802,20 @@ async function executeCodegen(input) {
757
802
  let rootSchemas;
758
803
  let rootDocuments;
759
804
  const generates = {};
805
+ const schemaLoadingCache = createCache(async function (hash) {
806
+ const outputSchemaAst = await context.loadSchema(JSON.parse(hash));
807
+ const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
808
+ return {
809
+ outputSchemaAst: outputSchemaAst,
810
+ outputSchema: outputSchema,
811
+ };
812
+ });
813
+ const documentsLoadingCache = createCache(async function (hash) {
814
+ const documents = await context.loadDocuments(JSON.parse(hash));
815
+ return {
816
+ documents,
817
+ };
818
+ });
760
819
  async function normalize() {
761
820
  /* Load Require extensions */
762
821
  const requireExtensions = pluginHelpers.normalizeInstanceOrArray(config.require);
@@ -858,8 +917,10 @@ async function executeCodegen(input) {
858
917
  Object.assign(schemaPointerMap, unnormalizedPtr);
859
918
  }
860
919
  }
861
- outputSchemaAst = await context.loadSchema(schemaPointerMap);
862
- outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
920
+ const hash = JSON.stringify(schemaPointerMap);
921
+ const result = await schemaLoadingCache.load(hash);
922
+ outputSchemaAst = await result.outputSchemaAst;
923
+ outputSchema = result.outputSchema;
863
924
  }, filename),
864
925
  },
865
926
  {
@@ -867,7 +928,9 @@ async function executeCodegen(input) {
867
928
  task: wrapTask(async () => {
868
929
  debugLog(`[CLI] Loading Documents`);
869
930
  const allDocuments = [...rootDocuments, ...outputSpecificDocuments];
870
- const documents = await context.loadDocuments(allDocuments);
931
+ const hash = JSON.stringify({ pointer: allDocuments, schema: outputSchema });
932
+ const result = await documentsLoadingCache.load(hash);
933
+ const documents = result.documents;
871
934
  if (documents.length > 0) {
872
935
  outputDocuments.push(...documents);
873
936
  }
package/index.mjs CHANGED
@@ -396,20 +396,30 @@ 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
- async function loadDocuments(documentPointers, config) {
412
+ const requiredLoader = async (pointers, schema) => {
413
+ return Promise.all(pointers.map(async (pointer) => {
414
+ const userLoader = (await makeDefaultLoader(process.cwd())(pointer.require)).default;
415
+ const documents = userLoader(pointer.config || {}, schema);
416
+ return documents;
417
+ })).then(results => [].concat(...results));
418
+ };
419
+ const isRequirePointer = (pointer) => {
420
+ return typeof pointer === 'object' && 'require' in pointer;
421
+ };
422
+ async function loadDocuments(documentPointers, config, schema) {
413
423
  const loaders = [
414
424
  new CodeFileLoader({
415
425
  pluckConfig: {
@@ -428,14 +438,35 @@ async function loadDocuments(documentPointers, config) {
428
438
  }
429
439
  ignore.push(join(process.cwd(), generatePath));
430
440
  }
431
- const loadedFromToolkit = await loadDocuments$1(documentPointers, {
441
+ const loaderPointers = [];
442
+ const requestPointers = [];
443
+ if (Array.isArray(documentPointers)) {
444
+ documentPointers.forEach(pointer => {
445
+ if (isRequirePointer(pointer)) {
446
+ requestPointers.push(pointer);
447
+ }
448
+ else {
449
+ loaderPointers.push(pointer);
450
+ }
451
+ });
452
+ }
453
+ else {
454
+ if (isRequirePointer(documentPointers)) {
455
+ requestPointers.push(documentPointers);
456
+ }
457
+ else {
458
+ loaderPointers.push(documentPointers);
459
+ }
460
+ }
461
+ const loadedFromToolkit = await loadDocuments$1(loaderPointers, {
432
462
  ...defaultDocumentsLoadOptions,
433
463
  ignore,
434
464
  loaders,
435
465
  ...config,
436
466
  ...config.config,
437
467
  });
438
- return loadedFromToolkit;
468
+ const loadedAlterantively = await requiredLoader(requestPointers, schema);
469
+ return [...loadedFromToolkit, ...loadedAlterantively];
439
470
  }
440
471
 
441
472
  const { lstat } = promises;
@@ -658,14 +689,14 @@ class CodegenContext {
658
689
  }
659
690
  return loadSchema(pointer, config);
660
691
  }
661
- async loadDocuments(pointer) {
692
+ async loadDocuments(input) {
662
693
  const config = this.getConfig(defaultDocumentsLoadOptions);
663
694
  if (this._graphqlConfig) {
664
695
  // TODO: pointer won't work here
665
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
696
+ const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(input.pointer, config);
666
697
  return documents;
667
698
  }
668
- return loadDocuments(pointer, config);
699
+ return loadDocuments(input.pointer, config, input.schema);
669
700
  }
670
701
  }
671
702
  function ensureContext(input) {
@@ -681,6 +712,20 @@ const makeDefaultLoader = (from) => {
681
712
  return import(relativeRequire.resolve(mod));
682
713
  };
683
714
  };
715
+ // TODO: Replace any with types
716
+ function createCache(loader) {
717
+ const cache = new Map();
718
+ return {
719
+ load(key) {
720
+ if (cache.has(key)) {
721
+ return cache.get(key);
722
+ }
723
+ const value = loader(key);
724
+ cache.set(key, value);
725
+ return value;
726
+ },
727
+ };
728
+ }
684
729
  async function executeCodegen(input) {
685
730
  function wrapTask(task, source) {
686
731
  return async () => {
@@ -730,6 +775,20 @@ async function executeCodegen(input) {
730
775
  let rootSchemas;
731
776
  let rootDocuments;
732
777
  const generates = {};
778
+ const schemaLoadingCache = createCache(async function (hash) {
779
+ const outputSchemaAst = await context.loadSchema(JSON.parse(hash));
780
+ const outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
781
+ return {
782
+ outputSchemaAst: outputSchemaAst,
783
+ outputSchema: outputSchema,
784
+ };
785
+ });
786
+ const documentsLoadingCache = createCache(async function (hash) {
787
+ const documents = await context.loadDocuments(JSON.parse(hash));
788
+ return {
789
+ documents,
790
+ };
791
+ });
733
792
  async function normalize() {
734
793
  /* Load Require extensions */
735
794
  const requireExtensions = normalizeInstanceOrArray(config.require);
@@ -831,8 +890,10 @@ async function executeCodegen(input) {
831
890
  Object.assign(schemaPointerMap, unnormalizedPtr);
832
891
  }
833
892
  }
834
- outputSchemaAst = await context.loadSchema(schemaPointerMap);
835
- outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
893
+ const hash = JSON.stringify(schemaPointerMap);
894
+ const result = await schemaLoadingCache.load(hash);
895
+ outputSchemaAst = await result.outputSchemaAst;
896
+ outputSchema = result.outputSchema;
836
897
  }, filename),
837
898
  },
838
899
  {
@@ -840,7 +901,9 @@ async function executeCodegen(input) {
840
901
  task: wrapTask(async () => {
841
902
  debugLog(`[CLI] Loading Documents`);
842
903
  const allDocuments = [...rootDocuments, ...outputSpecificDocuments];
843
- const documents = await context.loadDocuments(allDocuments);
904
+ const hash = JSON.stringify({ pointer: allDocuments, schema: outputSchema });
905
+ const result = await documentsLoadingCache.load(hash);
906
+ const documents = result.documents;
844
907
  if (documents.length > 0) {
845
908
  outputDocuments.push(...documents);
846
909
  }
package/load.d.ts CHANGED
@@ -12,4 +12,4 @@ export declare const defaultDocumentsLoadOptions: {
12
12
  skipGraphQLImport: boolean;
13
13
  };
14
14
  export declare function loadSchema(schemaPointers: UnnormalizedTypeDefPointer, config: Types.Config): Promise<GraphQLSchema>;
15
- export declare function loadDocuments(documentPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], config: Types.Config): Promise<Types.DocumentFile[]>;
15
+ export declare function loadDocuments(documentPointers: UnnormalizedTypeDefPointer | UnnormalizedTypeDefPointer[], config: Types.Config, schema: GraphQLSchema): Promise<Types.DocumentFile[]>;
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "2.3.1",
3
+ "version": "2.5.0-alpha-bc33c031d.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.0",
9
- "@graphql-codegen/plugin-helpers": "^2.3.2",
8
+ "@graphql-codegen/core": "2.4.1-alpha-bc33c031d.0",
9
+ "@graphql-codegen/plugin-helpers": "2.3.3-alpha-bc33c031d.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",