Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/persisted-documents-string-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"devDependencies": {
"@graphql-typed-document-node/core": "3.2.0",
"@graphql-codegen/cli": "6.1.3",
"@graphql-codegen/cli": "6.2.1",
"@babel/core": "7.25.2",
"@babel/preset-env": "7.25.3",
"@babel/preset-typescript": "7.28.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/persisted-documents/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"devDependencies": {
"@graphql-typed-document-node/core": "3.2.0",
"@graphql-codegen/cli": "6.1.3",
"@graphql-codegen/cli": "6.2.1",
"@babel/core": "7.25.2",
"@babel/preset-env": "7.25.3",
"@babel/preset-typescript": "7.28.5"
Expand Down
2 changes: 1 addition & 1 deletion examples/programmatic-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {
"@graphql-codegen/core": "5.0.1",
"@graphql-codegen/plugin-helpers": "6.1.1",
"@graphql-codegen/plugin-helpers": "6.2.0",
"@graphql-codegen/typed-document-node": "6.1.7",
"@graphql-codegen/typescript": "5.0.9",
"@graphql-codegen/typescript-operations": "5.0.9",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/apollo-client-defer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"graphql-yoga": "5.7.0"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@types/node": "^22.0.0",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.10",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/apollo-client-swc-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"devDependencies": {
"@graphql-codegen/client-preset-swc-plugin": "0.2.0",
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-react-swc": "^3.3.0",
"@types/react": "18.3.3",
"@types/react-dom": "18.3.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/apollo-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-react": "^5.0.0",
"@types/node": "^22.0.0",
"@types/react": "^18.0.15",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/http-executor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-react": "^5.0.0",
"@types/node": "^22.0.0",
"@types/react": "^18.0.17",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/nextjs-swr/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"swr": "^2.0.0"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@graphql-codegen/schema-ast": "5.0.1",
"@graphql-codegen/client-preset-swc-plugin": "0.2.0",
"@types/node": "^22.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/tanstack-react-query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"react-dom": "^18.2.0"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-react": "^5.0.0",
"@types/node": "^22.0.0",
"@types/react": "^18.0.17",
Expand Down
2 changes: 1 addition & 1 deletion examples/react/urql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"devDependencies": {
"@types/react": "^18.0.17",
"@types/react-dom": "^18.0.10",
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-react": "^5.0.0",
"typescript": "5.5.4",
"serve": "14.2.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"devDependencies": {
"@graphql-codegen/cli": "6.1.3"
"@graphql-codegen/cli": "6.2.1"
},
"dependencies": {
"@graphql-typed-document-node/core": "3.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript-graphql-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"devDependencies": {
"@graphql-codegen/cli": "6.1.3"
"@graphql-codegen/cli": "6.2.1"
},
"dependencies": {
"graphql": "16.9.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/typescript-resolvers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"devDependencies": {
"@graphql-codegen/cli": "6.1.3",
"@graphql-codegen/cli": "6.2.1",
"@graphql-codegen/typescript": "5.0.9",
"@graphql-codegen/typescript-resolvers": "5.1.7"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/vite/vite-react-cts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"vite": "^6.0.0"
},
"devDependencies": {
"@graphql-codegen/cli": "6.1.3",
"@graphql-codegen/cli": "6.2.1",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"cypress": "15.7.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/vite/vite-react-mts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"vite": "^6.0.0"
},
"devDependencies": {
"@graphql-codegen/cli": "6.1.3",
"@graphql-codegen/cli": "6.2.1",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"cypress": "15.7.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/vite/vite-react-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"vite": "^6.0.0"
},
"devDependencies": {
"@graphql-codegen/cli": "6.1.3",
"@graphql-codegen/cli": "6.2.1",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"cypress": "15.7.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/apollo-composable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"vue": "^3.2.37"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-vue": "^5.0.0",
"typescript": "^5.0.0",
"vite": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/urql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"vue": "^3.2.45"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-vue": "^5.0.0",
"typescript": "^5.0.0",
"vite": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/villus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"vue": "^3.2.37"
},
"devDependencies": {
"@graphql-codegen/cli": "^6.1.3",
"@graphql-codegen/cli": "^6.2.1",
"@vitejs/plugin-vue": "^5.0.0",
"typescript": "^5.0.0",
"vite": "^6.0.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/yoga-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"devDependencies": {
"@graphql-typed-document-node/core": "3.2.0",
"@graphql-codegen/cli": "6.1.3",
"@graphql-codegen/cli": "6.2.1",
"@babel/core": "7.25.2",
"@babel/preset-env": "7.25.3",
"@babel/preset-typescript": "7.28.5"
Expand Down
17 changes: 17 additions & 0 deletions packages/graphql-codegen-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# @graphql-codegen/cli

## 6.2.1

### Patch Changes

- [#10618](https://github.com/dotansimha/graphql-code-generator/pull/10618) [`e804925`](https://github.com/dotansimha/graphql-code-generator/commit/e804925361f326fe2fc7a2ee3f6db950bcd79dab) Thanks [@PalmerTurley34](https://github.com/PalmerTurley34)! - Honor per-output preset `importExtension` and `emitLegacyCommonJSImports` config instead of always using the root config values.

## 6.2.0

### Minor Changes

- [#10617](https://github.com/dotansimha/graphql-code-generator/pull/10617) [`8c4db2a`](https://github.com/dotansimha/graphql-code-generator/commit/8c4db2abd0578d7b1c9b4197dfa41dae3ef6252b) Thanks [@ikusakov2](https://github.com/ikusakov2)! - Allow GraphQLSchema to be passed directly to generate({schema: ...}) function

### Patch Changes

- Updated dependencies [[`8c4db2a`](https://github.com/dotansimha/graphql-code-generator/commit/8c4db2abd0578d7b1c9b4197dfa41dae3ef6252b)]:
- @graphql-codegen/[email protected]

## 6.1.3

### Patch Changes
Expand Down
6 changes: 3 additions & 3 deletions packages/graphql-codegen-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@graphql-codegen/cli",
"version": "6.1.3",
"version": "6.2.1",
"license": "MIT",
"bin": {
"gql-gen": "dist/cjs/bin.js",
Expand Down Expand Up @@ -45,7 +45,7 @@
"@babel/types": "^7.18.13",
"@graphql-codegen/client-preset": "^5.2.4",
"@graphql-codegen/core": "^5.0.1",
"@graphql-codegen/plugin-helpers": "^6.1.1",
"@graphql-codegen/plugin-helpers": "^6.2.0",
"@graphql-tools/apollo-engine-loader": "^8.0.28",
"@graphql-tools/code-file-loader": "^8.1.28",
"@graphql-tools/git-loader": "^8.0.32",
Expand All @@ -54,6 +54,7 @@
"@graphql-tools/json-file-loader": "^8.0.26",
"@graphql-tools/load": "^8.1.8",
"@graphql-tools/url-loader": "^9.0.6",
"@graphql-tools/merge": "^9.0.6",
"@graphql-tools/utils": "^11.0.0",
"@inquirer/prompts": "^7.8.2",
"@whatwg-node/fetch": "^0.10.0",
Expand All @@ -76,7 +77,6 @@
"yargs": "^17.0.0"
},
"devDependencies": {
"@graphql-tools/merge": "9.0.6",
"@parcel/watcher": "^2.1.0",
"@types/is-glob": "4.0.4",
"@types/js-yaml": "4.0.9",
Expand Down
55 changes: 44 additions & 11 deletions packages/graphql-codegen-cli/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
Types,
} from '@graphql-codegen/plugin-helpers';
import { NoTypeDefinitionsFound } from '@graphql-tools/load';
import { DocumentNode, GraphQLError, GraphQLSchema } from 'graphql';
import { buildASTSchema, DocumentNode, GraphQLError, GraphQLSchema, isSchema } from 'graphql';
import { mergeTypeDefs } from '@graphql-tools/merge';
import { Listr, ListrTask } from 'listr2';
import { CodegenContext, ensureContext } from './config.js';
import { getPluginByName } from './plugins.js';
Expand Down Expand Up @@ -85,6 +86,19 @@ export async function executeCodegen(

const cache = createCache();

// We need a simple string to uniqually identify the provided GraphQLSchema objects for the above cache.
// Because JavaScript does not provide access to its internal object ids, we need a workaround.
// Below is a common way to get unique ids for objects in JavaScript,
// by using a WeakMap and autoincrementing the id.
const jsObjectIds = new WeakMap<GraphQLSchema, number>();
let jsObjectIdCounter = 0;
function getJsObjectId(schema: GraphQLSchema): number {
if (!jsObjectIds.has(schema)) {
jsObjectIds.set(schema, jsObjectIdCounter++);
}
return jsObjectIds.get(schema)!;
}

function wrapTask(task: () => void | Promise<void>, source: string, taskName: string, ctx: Ctx) {
return () =>
context.profiler.run(async () => {
Expand Down Expand Up @@ -232,19 +246,32 @@ export async function executeCodegen(
async () => {
debugLog(`[CLI] Loading Schemas`);
const schemaPointerMap: any = {};
const parsedSchemas: GraphQLSchema[] = [];
const allSchemaDenormalizedPointers = [...rootSchemas, ...outputSpecificSchemas];

for (const denormalizedPtr of allSchemaDenormalizedPointers) {
if (typeof denormalizedPtr === 'string') {
if (isSchema(denormalizedPtr)) {
parsedSchemas.push(denormalizedPtr);
} else if (typeof denormalizedPtr === 'string') {
schemaPointerMap[denormalizedPtr] = {};
} else if (typeof denormalizedPtr === 'object') {
Object.assign(schemaPointerMap, denormalizedPtr);
}
}

const hash = JSON.stringify(schemaPointerMap);
const hash = JSON.stringify(schemaPointerMap) + parsedSchemas.map(getJsObjectId).join(',');
const result = await cache('schema', hash, async () => {
const outputSchemaAst = await context.loadSchema(schemaPointerMap);
// collect parsed schemas
const schemasToMerge: GraphQLSchema[] = [...parsedSchemas];
// collect schemas, provided by pointers
if (Object.keys(schemaPointerMap).length) {
schemasToMerge.push(await context.loadSchema(schemaPointerMap));
}
// merge all collected schemas into one
const outputSchemaAst =
schemasToMerge.length === 1
? schemasToMerge[0]
: buildASTSchema(mergeTypeDefs(schemasToMerge));
const outputSchema = getCachedDocumentNodeFromSchema(outputSchemaAst);
return {
outputSchemaAst,
Expand Down Expand Up @@ -322,18 +349,24 @@ export async function executeCodegen(
})
);

const importExtension = normalizeImportExtension({
const rawMergedConfig = {
...rootConfig,
emitLegacyCommonJSImports: config.emitLegacyCommonJSImports,
importExtension: config.importExtension,
});

const mergedConfig = {
...rootConfig,
...(typeof outputFileTemplateConfig === 'string'
? { value: outputFileTemplateConfig }
: outputFileTemplateConfig),
};

const importExtension = normalizeImportExtension({
emitLegacyCommonJSImports: rawMergedConfig.emitLegacyCommonJSImports,
importExtension: rawMergedConfig.importExtension,
});

const mergedConfig = {
...rawMergedConfig,
importExtension,
emitLegacyCommonJSImports: config.emitLegacyCommonJSImports ?? true,
emitLegacyCommonJSImports: rawMergedConfig.emitLegacyCommonJSImports ?? true,
};

const documentTransforms = Array.isArray(outputConfig.documentTransforms)
Expand Down Expand Up @@ -385,7 +418,7 @@ export async function executeCodegen(
const output = await codegen({
...outputArgs,
importExtension,
emitLegacyCommonJSImports: config.emitLegacyCommonJSImports ?? true,
emitLegacyCommonJSImports: rawMergedConfig.emitLegacyCommonJSImports ?? true,
cache,
});
result.push({
Expand Down
Loading
Loading