@graphql-codegen/cli 6.1.3 → 6.2.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/cjs/codegen.js CHANGED
@@ -10,6 +10,7 @@ const core_1 = require("@graphql-codegen/core");
10
10
  const plugin_helpers_1 = require("@graphql-codegen/plugin-helpers");
11
11
  const load_1 = require("@graphql-tools/load");
12
12
  const graphql_1 = require("graphql");
13
+ const merge_1 = require("@graphql-tools/merge");
13
14
  const listr2_1 = require("listr2");
14
15
  const config_js_1 = require("./config.js");
15
16
  const plugins_js_1 = require("./plugins.js");
@@ -61,6 +62,18 @@ async function executeCodegen(input) {
61
62
  let rootDocuments;
62
63
  const generates = {};
63
64
  const cache = createCache();
65
+ // We need a simple string to uniqually identify the provided GraphQLSchema objects for the above cache.
66
+ // Because JavaScript does not provide access to its internal object ids, we need a workaround.
67
+ // Below is a common way to get unique ids for objects in JavaScript,
68
+ // by using a WeakMap and autoincrementing the id.
69
+ const jsObjectIds = new WeakMap();
70
+ let jsObjectIdCounter = 0;
71
+ function getJsObjectId(schema) {
72
+ if (!jsObjectIds.has(schema)) {
73
+ jsObjectIds.set(schema, jsObjectIdCounter++);
74
+ }
75
+ return jsObjectIds.get(schema);
76
+ }
64
77
  function wrapTask(task, source, taskName, ctx) {
65
78
  return () => context.profiler.run(async () => {
66
79
  try {
@@ -177,18 +190,31 @@ async function executeCodegen(input) {
177
190
  task: wrapTask(async () => {
178
191
  (0, debugging_js_1.debugLog)(`[CLI] Loading Schemas`);
179
192
  const schemaPointerMap = {};
193
+ const parsedSchemas = [];
180
194
  const allSchemaDenormalizedPointers = [...rootSchemas, ...outputSpecificSchemas];
181
195
  for (const denormalizedPtr of allSchemaDenormalizedPointers) {
182
- if (typeof denormalizedPtr === 'string') {
196
+ if ((0, graphql_1.isSchema)(denormalizedPtr)) {
197
+ parsedSchemas.push(denormalizedPtr);
198
+ }
199
+ else if (typeof denormalizedPtr === 'string') {
183
200
  schemaPointerMap[denormalizedPtr] = {};
184
201
  }
185
202
  else if (typeof denormalizedPtr === 'object') {
186
203
  Object.assign(schemaPointerMap, denormalizedPtr);
187
204
  }
188
205
  }
189
- const hash = JSON.stringify(schemaPointerMap);
206
+ const hash = JSON.stringify(schemaPointerMap) + parsedSchemas.map(getJsObjectId).join(',');
190
207
  const result = await cache('schema', hash, async () => {
191
- const outputSchemaAst = await context.loadSchema(schemaPointerMap);
208
+ // collect parsed schemas
209
+ const schemasToMerge = [...parsedSchemas];
210
+ // collect schemas, provided by pointers
211
+ if (Object.keys(schemaPointerMap).length) {
212
+ schemasToMerge.push(await context.loadSchema(schemaPointerMap));
213
+ }
214
+ // merge all collected schemas into one
215
+ const outputSchemaAst = schemasToMerge.length === 1
216
+ ? schemasToMerge[0]
217
+ : (0, graphql_1.buildASTSchema)((0, merge_1.mergeTypeDefs)(schemasToMerge));
192
218
  const outputSchema = (0, plugin_helpers_1.getCachedDocumentNodeFromSchema)(outputSchemaAst);
193
219
  return {
194
220
  outputSchemaAst,
package/esm/codegen.js CHANGED
@@ -5,7 +5,8 @@ import path from 'path';
5
5
  import { codegen } from '@graphql-codegen/core';
6
6
  import { getCachedDocumentNodeFromSchema, normalizeConfig, normalizeImportExtension, normalizeInstanceOrArray, normalizeOutputParam, } from '@graphql-codegen/plugin-helpers';
7
7
  import { NoTypeDefinitionsFound } from '@graphql-tools/load';
8
- import { GraphQLError } from 'graphql';
8
+ import { buildASTSchema, GraphQLError, isSchema } from 'graphql';
9
+ import { mergeTypeDefs } from '@graphql-tools/merge';
9
10
  import { Listr } from 'listr2';
10
11
  import { ensureContext } from './config.js';
11
12
  import { getPluginByName } from './plugins.js';
@@ -57,6 +58,18 @@ export async function executeCodegen(input) {
57
58
  let rootDocuments;
58
59
  const generates = {};
59
60
  const cache = createCache();
61
+ // We need a simple string to uniqually identify the provided GraphQLSchema objects for the above cache.
62
+ // Because JavaScript does not provide access to its internal object ids, we need a workaround.
63
+ // Below is a common way to get unique ids for objects in JavaScript,
64
+ // by using a WeakMap and autoincrementing the id.
65
+ const jsObjectIds = new WeakMap();
66
+ let jsObjectIdCounter = 0;
67
+ function getJsObjectId(schema) {
68
+ if (!jsObjectIds.has(schema)) {
69
+ jsObjectIds.set(schema, jsObjectIdCounter++);
70
+ }
71
+ return jsObjectIds.get(schema);
72
+ }
60
73
  function wrapTask(task, source, taskName, ctx) {
61
74
  return () => context.profiler.run(async () => {
62
75
  try {
@@ -173,18 +186,31 @@ export async function executeCodegen(input) {
173
186
  task: wrapTask(async () => {
174
187
  debugLog(`[CLI] Loading Schemas`);
175
188
  const schemaPointerMap = {};
189
+ const parsedSchemas = [];
176
190
  const allSchemaDenormalizedPointers = [...rootSchemas, ...outputSpecificSchemas];
177
191
  for (const denormalizedPtr of allSchemaDenormalizedPointers) {
178
- if (typeof denormalizedPtr === 'string') {
192
+ if (isSchema(denormalizedPtr)) {
193
+ parsedSchemas.push(denormalizedPtr);
194
+ }
195
+ else if (typeof denormalizedPtr === 'string') {
179
196
  schemaPointerMap[denormalizedPtr] = {};
180
197
  }
181
198
  else if (typeof denormalizedPtr === 'object') {
182
199
  Object.assign(schemaPointerMap, denormalizedPtr);
183
200
  }
184
201
  }
185
- const hash = JSON.stringify(schemaPointerMap);
202
+ const hash = JSON.stringify(schemaPointerMap) + parsedSchemas.map(getJsObjectId).join(',');
186
203
  const result = await cache('schema', hash, async () => {
187
- const outputSchemaAst = await context.loadSchema(schemaPointerMap);
204
+ // collect parsed schemas
205
+ const schemasToMerge = [...parsedSchemas];
206
+ // collect schemas, provided by pointers
207
+ if (Object.keys(schemaPointerMap).length) {
208
+ schemasToMerge.push(await context.loadSchema(schemaPointerMap));
209
+ }
210
+ // merge all collected schemas into one
211
+ const outputSchemaAst = schemasToMerge.length === 1
212
+ ? schemasToMerge[0]
213
+ : buildASTSchema(mergeTypeDefs(schemasToMerge));
188
214
  const outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
189
215
  return {
190
216
  outputSchemaAst,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-codegen/cli",
3
- "version": "6.1.3",
3
+ "version": "6.2.0",
4
4
  "peerDependenciesMeta": {
5
5
  "@parcel/watcher": {
6
6
  "optional": true
@@ -16,7 +16,7 @@
16
16
  "@babel/types": "^7.18.13",
17
17
  "@graphql-codegen/client-preset": "^5.2.4",
18
18
  "@graphql-codegen/core": "^5.0.1",
19
- "@graphql-codegen/plugin-helpers": "^6.1.1",
19
+ "@graphql-codegen/plugin-helpers": "^6.2.0",
20
20
  "@graphql-tools/apollo-engine-loader": "^8.0.28",
21
21
  "@graphql-tools/code-file-loader": "^8.1.28",
22
22
  "@graphql-tools/git-loader": "^8.0.32",
@@ -25,6 +25,7 @@
25
25
  "@graphql-tools/json-file-loader": "^8.0.26",
26
26
  "@graphql-tools/load": "^8.1.8",
27
27
  "@graphql-tools/url-loader": "^9.0.6",
28
+ "@graphql-tools/merge": "^9.0.6",
28
29
  "@graphql-tools/utils": "^11.0.0",
29
30
  "@inquirer/prompts": "^7.8.2",
30
31
  "@whatwg-node/fetch": "^0.10.0",