@graphql-codegen/cli 2.6.0 → 2.6.1-alpha-74513a63e.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.
Files changed (4) hide show
  1. package/bin.js +65 -34
  2. package/index.js +65 -34
  3. package/index.mjs +67 -36
  4. package/package.json +3 -3
package/bin.js CHANGED
@@ -53,12 +53,12 @@ const yaml = _interopDefault(require('yaml'));
53
53
  const module$1 = require('module');
54
54
  const fs = require('fs');
55
55
  const fs__default = _interopDefault(fs);
56
+ const crypto = require('crypto');
56
57
  const os = require('os');
57
58
  const Listr = _interopDefault(require('listr'));
58
59
  const isGlob = _interopDefault(require('is-glob'));
59
60
  const debounce = _interopDefault(require('debounce'));
60
61
  const mkdirp = _interopDefault(require('mkdirp'));
61
- const crypto = require('crypto');
62
62
  const inquirer = _interopDefault(require('inquirer'));
63
63
  const detectIndent = _interopDefault(require('detect-indent'));
64
64
  const getLatestVersion = _interopDefault(require('latest-version'));
@@ -779,23 +779,53 @@ class CodegenContext {
779
779
  const config = this.getConfig(defaultSchemaLoadOptions);
780
780
  if (this._graphqlConfig) {
781
781
  // TODO: SchemaWithLoader won't work here
782
- return this._graphqlConfig.getProject(this._project).loadSchema(pointer, 'GraphQLSchema', config);
782
+ return addHashToSchema(this._graphqlConfig.getProject(this._project).loadSchema(pointer, 'GraphQLSchema', config));
783
783
  }
784
- return loadSchema(pointer, config);
784
+ return addHashToSchema(loadSchema(pointer, config));
785
785
  }
786
786
  async loadDocuments(pointer) {
787
787
  const config = this.getConfig(defaultDocumentsLoadOptions);
788
788
  if (this._graphqlConfig) {
789
789
  // TODO: pointer won't work here
790
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
791
- return documents;
790
+ return addHashToDocumentFiles(this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config));
792
791
  }
793
- return loadDocuments(pointer, config);
792
+ return addHashToDocumentFiles(loadDocuments(pointer, config));
794
793
  }
795
794
  }
796
795
  function ensureContext(input) {
797
796
  return input instanceof CodegenContext ? input : new CodegenContext({ config: input });
798
797
  }
798
+ function hashContent(content) {
799
+ return crypto.createHash('sha256').update(content).digest('hex');
800
+ }
801
+ function hashSchema(schema) {
802
+ return hashContent(graphql.print(pluginHelpers.getCachedDocumentNodeFromSchema(schema)));
803
+ }
804
+ function addHashToSchema(schemaPromise) {
805
+ return schemaPromise.then(schema => {
806
+ // It's consumed later on. The general purpose is to use it for caching.
807
+ if (!schema.extensions) {
808
+ schema.extensions = {};
809
+ }
810
+ schema.extensions['hash'] = hashSchema(schema);
811
+ return schema;
812
+ });
813
+ }
814
+ function hashDocument(doc) {
815
+ if (doc.rawSDL) {
816
+ return hashContent(doc.rawSDL);
817
+ }
818
+ if (doc.document) {
819
+ return hashContent(graphql.print(doc.document));
820
+ }
821
+ return null;
822
+ }
823
+ function addHashToDocumentFiles(documentFilesPromise) {
824
+ return documentFilesPromise.then(documentFiles => documentFiles.map(doc => {
825
+ doc.hash = hashDocument(doc);
826
+ return doc;
827
+ }));
828
+ }
799
829
 
800
830
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
801
831
  const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
@@ -833,18 +863,17 @@ const makeDefaultLoader = (from) => {
833
863
  return new Promise(function (resolve) { resolve(_interopNamespace(require(relativeRequire.resolve(mod)))); });
834
864
  };
835
865
  };
836
- // TODO: Replace any with types
837
- function createCache(loader) {
866
+ function createCache() {
838
867
  const cache = new Map();
839
- return {
840
- load(key) {
841
- if (cache.has(key)) {
842
- return cache.get(key);
843
- }
844
- const value = loader(key);
845
- cache.set(key, value);
846
- return value;
847
- },
868
+ return function ensure(namespace, key, factory) {
869
+ const cacheKey = `${namespace}:${key}`;
870
+ const cachedValue = cache.get(cacheKey);
871
+ if (cachedValue) {
872
+ return cachedValue;
873
+ }
874
+ const value = factory();
875
+ cache.set(cacheKey, value);
876
+ return value;
848
877
  };
849
878
  }
850
879
  async function executeCodegen(input) {
@@ -883,20 +912,7 @@ async function executeCodegen(input) {
883
912
  let rootSchemas;
884
913
  let rootDocuments;
885
914
  const generates = {};
886
- const schemaLoadingCache = createCache(async function (hash) {
887
- const outputSchemaAst = await context.loadSchema(JSON.parse(hash));
888
- const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
889
- return {
890
- outputSchemaAst: outputSchemaAst,
891
- outputSchema: outputSchema,
892
- };
893
- });
894
- const documentsLoadingCache = createCache(async function (hash) {
895
- const documents = await context.loadDocuments(JSON.parse(hash));
896
- return {
897
- documents: documents,
898
- };
899
- });
915
+ const cache = createCache();
900
916
  function wrapTask(task, source, taskName) {
901
917
  return () => {
902
918
  return context.profiler.run(async () => {
@@ -1014,7 +1030,14 @@ async function executeCodegen(input) {
1014
1030
  }
1015
1031
  }
1016
1032
  const hash = JSON.stringify(schemaPointerMap);
1017
- const result = await schemaLoadingCache.load(hash);
1033
+ const result = await cache('schema', hash, async () => {
1034
+ const outputSchemaAst = await context.loadSchema(schemaPointerMap);
1035
+ const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
1036
+ return {
1037
+ outputSchemaAst: outputSchemaAst,
1038
+ outputSchema: outputSchema,
1039
+ };
1040
+ });
1018
1041
  outputSchemaAst = await result.outputSchemaAst;
1019
1042
  outputSchema = result.outputSchema;
1020
1043
  }, filename, `Load GraphQL schemas: ${filename}`),
@@ -1026,7 +1049,12 @@ async function executeCodegen(input) {
1026
1049
  // get different cache for shared docs and output specific docs
1027
1050
  const results = await Promise.all([rootDocuments, outputSpecificDocuments].map(docs => {
1028
1051
  const hash = JSON.stringify(docs);
1029
- return documentsLoadingCache.load(hash);
1052
+ return cache('documents', hash, async () => {
1053
+ const documents = await context.loadDocuments(docs);
1054
+ return {
1055
+ documents: documents,
1056
+ };
1057
+ });
1030
1058
  }));
1031
1059
  const documents = [];
1032
1060
  results.forEach(source => documents.push(...source.documents));
@@ -1090,7 +1118,10 @@ async function executeCodegen(input) {
1090
1118
  ];
1091
1119
  }
1092
1120
  const process = async (outputArgs) => {
1093
- const output = await core.codegen(outputArgs);
1121
+ const output = await core.codegen({
1122
+ ...outputArgs,
1123
+ cache,
1124
+ });
1094
1125
  result.push({
1095
1126
  filename: outputArgs.filename,
1096
1127
  content: output,
package/index.js CHANGED
@@ -53,13 +53,13 @@ const yaml = _interopDefault(require('yaml'));
53
53
  const module$1 = require('module');
54
54
  const fs = require('fs');
55
55
  const fs__default = _interopDefault(fs);
56
+ const crypto = require('crypto');
56
57
  const os = require('os');
57
58
  const Listr = _interopDefault(require('listr'));
58
59
  const child_process = require('child_process');
59
60
  const isGlob = _interopDefault(require('is-glob'));
60
61
  const debounce = _interopDefault(require('debounce'));
61
62
  const mkdirp = _interopDefault(require('mkdirp'));
62
- const crypto = require('crypto');
63
63
  const inquirer = _interopDefault(require('inquirer'));
64
64
  const detectIndent = _interopDefault(require('detect-indent'));
65
65
  const getLatestVersion = _interopDefault(require('latest-version'));
@@ -697,23 +697,53 @@ class CodegenContext {
697
697
  const config = this.getConfig(defaultSchemaLoadOptions);
698
698
  if (this._graphqlConfig) {
699
699
  // TODO: SchemaWithLoader won't work here
700
- return this._graphqlConfig.getProject(this._project).loadSchema(pointer, 'GraphQLSchema', config);
700
+ return addHashToSchema(this._graphqlConfig.getProject(this._project).loadSchema(pointer, 'GraphQLSchema', config));
701
701
  }
702
- return loadSchema(pointer, config);
702
+ return addHashToSchema(loadSchema(pointer, config));
703
703
  }
704
704
  async loadDocuments(pointer) {
705
705
  const config = this.getConfig(defaultDocumentsLoadOptions);
706
706
  if (this._graphqlConfig) {
707
707
  // TODO: pointer won't work here
708
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
709
- return documents;
708
+ return addHashToDocumentFiles(this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config));
710
709
  }
711
- return loadDocuments(pointer, config);
710
+ return addHashToDocumentFiles(loadDocuments(pointer, config));
712
711
  }
713
712
  }
714
713
  function ensureContext(input) {
715
714
  return input instanceof CodegenContext ? input : new CodegenContext({ config: input });
716
715
  }
716
+ function hashContent(content) {
717
+ return crypto.createHash('sha256').update(content).digest('hex');
718
+ }
719
+ function hashSchema(schema) {
720
+ return hashContent(graphql.print(pluginHelpers.getCachedDocumentNodeFromSchema(schema)));
721
+ }
722
+ function addHashToSchema(schemaPromise) {
723
+ return schemaPromise.then(schema => {
724
+ // It's consumed later on. The general purpose is to use it for caching.
725
+ if (!schema.extensions) {
726
+ schema.extensions = {};
727
+ }
728
+ schema.extensions['hash'] = hashSchema(schema);
729
+ return schema;
730
+ });
731
+ }
732
+ function hashDocument(doc) {
733
+ if (doc.rawSDL) {
734
+ return hashContent(doc.rawSDL);
735
+ }
736
+ if (doc.document) {
737
+ return hashContent(graphql.print(doc.document));
738
+ }
739
+ return null;
740
+ }
741
+ function addHashToDocumentFiles(documentFilesPromise) {
742
+ return documentFilesPromise.then(documentFiles => documentFiles.map(doc => {
743
+ doc.hash = hashDocument(doc);
744
+ return doc;
745
+ }));
746
+ }
717
747
 
718
748
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
719
749
  const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
@@ -751,18 +781,17 @@ const makeDefaultLoader = (from) => {
751
781
  return new Promise(function (resolve) { resolve(_interopNamespace(require(relativeRequire.resolve(mod)))); });
752
782
  };
753
783
  };
754
- // TODO: Replace any with types
755
- function createCache(loader) {
784
+ function createCache() {
756
785
  const cache = new Map();
757
- return {
758
- load(key) {
759
- if (cache.has(key)) {
760
- return cache.get(key);
761
- }
762
- const value = loader(key);
763
- cache.set(key, value);
764
- return value;
765
- },
786
+ return function ensure(namespace, key, factory) {
787
+ const cacheKey = `${namespace}:${key}`;
788
+ const cachedValue = cache.get(cacheKey);
789
+ if (cachedValue) {
790
+ return cachedValue;
791
+ }
792
+ const value = factory();
793
+ cache.set(cacheKey, value);
794
+ return value;
766
795
  };
767
796
  }
768
797
  async function executeCodegen(input) {
@@ -801,20 +830,7 @@ async function executeCodegen(input) {
801
830
  let rootSchemas;
802
831
  let rootDocuments;
803
832
  const generates = {};
804
- const schemaLoadingCache = createCache(async function (hash) {
805
- const outputSchemaAst = await context.loadSchema(JSON.parse(hash));
806
- const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
807
- return {
808
- outputSchemaAst: outputSchemaAst,
809
- outputSchema: outputSchema,
810
- };
811
- });
812
- const documentsLoadingCache = createCache(async function (hash) {
813
- const documents = await context.loadDocuments(JSON.parse(hash));
814
- return {
815
- documents: documents,
816
- };
817
- });
833
+ const cache = createCache();
818
834
  function wrapTask(task, source, taskName) {
819
835
  return () => {
820
836
  return context.profiler.run(async () => {
@@ -932,7 +948,14 @@ async function executeCodegen(input) {
932
948
  }
933
949
  }
934
950
  const hash = JSON.stringify(schemaPointerMap);
935
- const result = await schemaLoadingCache.load(hash);
951
+ const result = await cache('schema', hash, async () => {
952
+ const outputSchemaAst = await context.loadSchema(schemaPointerMap);
953
+ const outputSchema = pluginHelpers.getCachedDocumentNodeFromSchema(outputSchemaAst);
954
+ return {
955
+ outputSchemaAst: outputSchemaAst,
956
+ outputSchema: outputSchema,
957
+ };
958
+ });
936
959
  outputSchemaAst = await result.outputSchemaAst;
937
960
  outputSchema = result.outputSchema;
938
961
  }, filename, `Load GraphQL schemas: ${filename}`),
@@ -944,7 +967,12 @@ async function executeCodegen(input) {
944
967
  // get different cache for shared docs and output specific docs
945
968
  const results = await Promise.all([rootDocuments, outputSpecificDocuments].map(docs => {
946
969
  const hash = JSON.stringify(docs);
947
- return documentsLoadingCache.load(hash);
970
+ return cache('documents', hash, async () => {
971
+ const documents = await context.loadDocuments(docs);
972
+ return {
973
+ documents: documents,
974
+ };
975
+ });
948
976
  }));
949
977
  const documents = [];
950
978
  results.forEach(source => documents.push(...source.documents));
@@ -1008,7 +1036,10 @@ async function executeCodegen(input) {
1008
1036
  ];
1009
1037
  }
1010
1038
  const process = async (outputArgs) => {
1011
- const output = await core.codegen(outputArgs);
1039
+ const output = await core.codegen({
1040
+ ...outputArgs,
1041
+ cache,
1042
+ });
1012
1043
  result.push({
1013
1044
  filename: outputArgs.filename,
1014
1045
  content: output,
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isDetailedError, DetailedError, createNoopProfiler, createProfiler, normalizeInstanceOrArray, normalizeConfig, normalizeOutputParam, getCachedDocumentNodeFromSchema } from '@graphql-codegen/plugin-helpers';
1
+ import { isDetailedError, DetailedError, createNoopProfiler, createProfiler, getCachedDocumentNodeFromSchema, normalizeInstanceOrArray, normalizeConfig, normalizeOutputParam } from '@graphql-codegen/plugin-helpers';
2
2
  import { codegen } from '@graphql-codegen/core';
3
3
  import { AggregateError, isValidPath } from '@graphql-tools/utils';
4
4
  import chalk from 'chalk';
@@ -8,7 +8,7 @@ import wrapAnsi from 'wrap-ansi';
8
8
  import { stripIndent } from 'common-tags';
9
9
  import { dummyLogger } from 'ts-log';
10
10
  import UpdateRenderer from 'listr-update-renderer';
11
- import { GraphQLError } from 'graphql';
11
+ import { print, GraphQLError } from 'graphql';
12
12
  import path, { resolve, extname, join, delimiter, sep, dirname, isAbsolute, relative } from 'path';
13
13
  import { cosmiconfig, defaultLoaders } from 'cosmiconfig';
14
14
  import { env } from 'string-env-interpolation';
@@ -26,13 +26,13 @@ import { UrlLoader } from '@graphql-tools/url-loader';
26
26
  import yaml from 'yaml';
27
27
  import { createRequire } from 'module';
28
28
  import fs, { promises, unlink, writeFileSync, readFileSync } from 'fs';
29
+ import { createHash } from 'crypto';
29
30
  import { cpus } from 'os';
30
31
  import Listr from 'listr';
31
32
  import { exec } from 'child_process';
32
33
  import isGlob from 'is-glob';
33
34
  import debounce from 'debounce';
34
35
  import mkdirp from 'mkdirp';
35
- import { createHash } from 'crypto';
36
36
  import inquirer from 'inquirer';
37
37
  import detectIndent from 'detect-indent';
38
38
  import getLatestVersion from 'latest-version';
@@ -670,23 +670,53 @@ class CodegenContext {
670
670
  const config = this.getConfig(defaultSchemaLoadOptions);
671
671
  if (this._graphqlConfig) {
672
672
  // TODO: SchemaWithLoader won't work here
673
- return this._graphqlConfig.getProject(this._project).loadSchema(pointer, 'GraphQLSchema', config);
673
+ return addHashToSchema(this._graphqlConfig.getProject(this._project).loadSchema(pointer, 'GraphQLSchema', config));
674
674
  }
675
- return loadSchema(pointer, config);
675
+ return addHashToSchema(loadSchema(pointer, config));
676
676
  }
677
677
  async loadDocuments(pointer) {
678
678
  const config = this.getConfig(defaultDocumentsLoadOptions);
679
679
  if (this._graphqlConfig) {
680
680
  // TODO: pointer won't work here
681
- const documents = await this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config);
682
- return documents;
681
+ return addHashToDocumentFiles(this._graphqlConfig.getProject(this._project).loadDocuments(pointer, config));
683
682
  }
684
- return loadDocuments(pointer, config);
683
+ return addHashToDocumentFiles(loadDocuments(pointer, config));
685
684
  }
686
685
  }
687
686
  function ensureContext(input) {
688
687
  return input instanceof CodegenContext ? input : new CodegenContext({ config: input });
689
688
  }
689
+ function hashContent(content) {
690
+ return createHash('sha256').update(content).digest('hex');
691
+ }
692
+ function hashSchema(schema) {
693
+ return hashContent(print(getCachedDocumentNodeFromSchema(schema)));
694
+ }
695
+ function addHashToSchema(schemaPromise) {
696
+ return schemaPromise.then(schema => {
697
+ // It's consumed later on. The general purpose is to use it for caching.
698
+ if (!schema.extensions) {
699
+ schema.extensions = {};
700
+ }
701
+ schema.extensions['hash'] = hashSchema(schema);
702
+ return schema;
703
+ });
704
+ }
705
+ function hashDocument(doc) {
706
+ if (doc.rawSDL) {
707
+ return hashContent(doc.rawSDL);
708
+ }
709
+ if (doc.document) {
710
+ return hashContent(print(doc.document));
711
+ }
712
+ return null;
713
+ }
714
+ function addHashToDocumentFiles(documentFilesPromise) {
715
+ return documentFilesPromise.then(documentFiles => documentFiles.map(doc => {
716
+ doc.hash = hashDocument(doc);
717
+ return doc;
718
+ }));
719
+ }
690
720
 
691
721
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
692
722
  const isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
@@ -724,18 +754,17 @@ const makeDefaultLoader = (from) => {
724
754
  return import(relativeRequire.resolve(mod));
725
755
  };
726
756
  };
727
- // TODO: Replace any with types
728
- function createCache(loader) {
757
+ function createCache() {
729
758
  const cache = new Map();
730
- return {
731
- load(key) {
732
- if (cache.has(key)) {
733
- return cache.get(key);
734
- }
735
- const value = loader(key);
736
- cache.set(key, value);
737
- return value;
738
- },
759
+ return function ensure(namespace, key, factory) {
760
+ const cacheKey = `${namespace}:${key}`;
761
+ const cachedValue = cache.get(cacheKey);
762
+ if (cachedValue) {
763
+ return cachedValue;
764
+ }
765
+ const value = factory();
766
+ cache.set(cacheKey, value);
767
+ return value;
739
768
  };
740
769
  }
741
770
  async function executeCodegen(input) {
@@ -774,20 +803,7 @@ async function executeCodegen(input) {
774
803
  let rootSchemas;
775
804
  let rootDocuments;
776
805
  const generates = {};
777
- const schemaLoadingCache = createCache(async function (hash) {
778
- const outputSchemaAst = await context.loadSchema(JSON.parse(hash));
779
- const outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
780
- return {
781
- outputSchemaAst: outputSchemaAst,
782
- outputSchema: outputSchema,
783
- };
784
- });
785
- const documentsLoadingCache = createCache(async function (hash) {
786
- const documents = await context.loadDocuments(JSON.parse(hash));
787
- return {
788
- documents: documents,
789
- };
790
- });
806
+ const cache = createCache();
791
807
  function wrapTask(task, source, taskName) {
792
808
  return () => {
793
809
  return context.profiler.run(async () => {
@@ -905,7 +921,14 @@ async function executeCodegen(input) {
905
921
  }
906
922
  }
907
923
  const hash = JSON.stringify(schemaPointerMap);
908
- const result = await schemaLoadingCache.load(hash);
924
+ const result = await cache('schema', hash, async () => {
925
+ const outputSchemaAst = await context.loadSchema(schemaPointerMap);
926
+ const outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
927
+ return {
928
+ outputSchemaAst: outputSchemaAst,
929
+ outputSchema: outputSchema,
930
+ };
931
+ });
909
932
  outputSchemaAst = await result.outputSchemaAst;
910
933
  outputSchema = result.outputSchema;
911
934
  }, filename, `Load GraphQL schemas: ${filename}`),
@@ -917,7 +940,12 @@ async function executeCodegen(input) {
917
940
  // get different cache for shared docs and output specific docs
918
941
  const results = await Promise.all([rootDocuments, outputSpecificDocuments].map(docs => {
919
942
  const hash = JSON.stringify(docs);
920
- return documentsLoadingCache.load(hash);
943
+ return cache('documents', hash, async () => {
944
+ const documents = await context.loadDocuments(docs);
945
+ return {
946
+ documents: documents,
947
+ };
948
+ });
921
949
  }));
922
950
  const documents = [];
923
951
  results.forEach(source => documents.push(...source.documents));
@@ -981,7 +1009,10 @@ async function executeCodegen(input) {
981
1009
  ];
982
1010
  }
983
1011
  const process = async (outputArgs) => {
984
- const output = await codegen(outputArgs);
1012
+ const output = await codegen({
1013
+ ...outputArgs,
1014
+ cache,
1015
+ });
985
1016
  result.push({
986
1017
  filename: outputArgs.filename,
987
1018
  content: output,
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "2.6.0",
3
+ "version": "2.6.1-alpha-74513a63e.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.5.0",
9
- "@graphql-codegen/plugin-helpers": "^2.4.0",
8
+ "@graphql-codegen/core": "2.5.1-alpha-74513a63e.0",
9
+ "@graphql-codegen/plugin-helpers": "2.4.1-alpha-74513a63e.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",