Skip to content

Commit b51daba

Browse files
ikusakov2ikusakovikusakoveddeee888
authored
Bugfix: enums from external fragments were not generated along with operations (#10565)
* integration test * cleanups * cleanups2 * fixed a bug with external fragments * cleanup * keep generated files, refactored visitorwq * cleanup * changeset * adding missing dependency * delete temporary files * cleanup * cleanups * Adding back the integration tests + fix them * merged + fixes * cleanup * cleanup * patching near-operation-file preset * cleanup * adding duplicates case to integration tests * Fixing bug with duplicates generated when using extractAllFieldsToTypesCompact * cleanup * prevent running prettier on generated code * cleanups * cleanup * adding unit tests * update changeset * cleanups * try resolving graphql 16 error * remove unused deps * remove unused deps * cleanups * Revert and apply minimal yarn.lock changes --------- Co-authored-by: ikusakov <[email protected]> Co-authored-by: Igor Kusakov <[email protected]> Co-authored-by: Eddy Nguyen <[email protected]> Co-authored-by: Eddy Nguyen <[email protected]>
1 parent c0bdb72 commit b51daba

22 files changed

+2043
-30
lines changed

.changeset/upset-bars-call.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/typescript-operations': minor
3+
'@graphql-codegen/visitor-plugin-common': minor
4+
---
5+
6+
Fixing 2 bugs: 1) including enums from external fragments; 2) extractAllFieldsToTypesCompact does not create duplicates

dev-test-apollo-tooling/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The `dev-test-apollo-tooling` package is an example of migrating from Apollo tooling to GraphQL Codegen. It attempts to generate output as close as possible to Apollo tooling’s output. Note: **this package is a work in progress** and currently requires a patch to `near-operation-file`. We will fix this package soon.
2+
3+
How to run this package:
4+
5+
1. Make sure you have the correct Yarn version installed (check the root package.json, `packageManager` entry). Otherwise, you might run into unexplained bugs.
6+
2. In the monorepo root, run:
7+
8+
yarn clean && yarn install && yarn build
9+
10+
3. Patch `near-operation-file` manually (the automatic patch doesn’t always work). In the monorepo root, run:
11+
12+
yarn postinstall
13+
14+
4. Go to the `dev-test-apollo-tooling` directory and run:
15+
16+
cd dev-test-apollo-tooling
17+
yarn install
18+
yarn start
19+
20+
This will generate type files in `dev-test-apollo-tooling/src/__generated__/*`.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env ts-node
2+
3+
import { generate } from '@graphql-codegen/cli';
4+
import type { Types } from '@graphql-codegen/plugin-helpers';
5+
6+
export const GENERATED = '__generated__';
7+
export const GLOBAL_TYPES_FILE = 'globalTypes.ts';
8+
export const TS_GENERATED_FILE_HEADER = `\
9+
/* tslint:disable */
10+
/* eslint-disable */
11+
// @generated
12+
// This file was automatically generated and should not be edited.
13+
`;
14+
15+
/**
16+
* The following GraphQL Codegen config matches as closely as possible
17+
* to the old apollo-tooling codegen
18+
* @see https://github.com/apollographql/apollo-tooling/issues/2053
19+
* */
20+
const GRAPHQL_CODEGEN_CONFIG = {
21+
useTypeImports: true,
22+
namingConvention: 'keep', // Keeps naming as-is
23+
avoidOptionals: false, // Allow '?' on variables fields
24+
nonOptionalTypename: true, // Forces `__typename` on all selection sets
25+
skipTypeNameForRoot: true, // Don't generate __typename for root types
26+
omitOperationSuffix: true, // Don't add 'Query', 'Mutation' or 'Subscription' suffixes to operation result types
27+
fragmentSuffix: '', // Don't add 'Fragment' suffix to fragment result types
28+
extractAllFieldsToTypesCompact: true, // Extracts all fields to separate types (similar to apollo-codegen behavior)
29+
printFieldsOnNewLines: true, // Prints each field on a new line (similar to apollo-codegen behavior)
30+
importTypesNamespace: '', // Disable namespace prefix on imported types (preset config)
31+
enumType: 'native',
32+
generatesOperationTypes: true,
33+
};
34+
35+
export const main = async () => {
36+
const cwd = process.cwd();
37+
38+
const localSchemaFilePath = `${cwd}/schema.graphql`;
39+
40+
const includes = ['src'];
41+
42+
const generateFiles: { [scanPath: string]: Types.ConfiguredOutput } = {};
43+
44+
// Prepare the required structure for GraphQL Codegen
45+
includes.forEach((include: string) => {
46+
generateFiles[include] = {
47+
preset: 'near-operation-file', // This preset tells the codegen to generate multiple files instead of one
48+
presetConfig: {
49+
extension: '.ts', // Matches the existing Apollo-Codegen file naming
50+
// FIXME: The following config is required, but it is not needed with the recent version of typescript-operations.
51+
// So - when the new version of near-operation-file' is available - fix this.
52+
baseTypesPath: 'unused',
53+
folder: GENERATED, // Output folder for generated files
54+
importTypesNamespace: '', // Disable namespace prefix on imported types
55+
},
56+
plugins: [
57+
'typescript-operations',
58+
{
59+
add: {
60+
content: TS_GENERATED_FILE_HEADER,
61+
},
62+
},
63+
],
64+
};
65+
});
66+
67+
await generate(
68+
{
69+
schema: localSchemaFilePath,
70+
documents: [
71+
// matching js extensins as well - there are cases where js files are not converted to typescript yet
72+
// (but the package is typescript)
73+
...includes.map((include: any) => `${include}/**/*.{js,jsx,ts,tsx}`),
74+
`!**/${GENERATED}/**`,
75+
],
76+
config: GRAPHQL_CODEGEN_CONFIG,
77+
generates: generateFiles,
78+
silent: false,
79+
debug: true,
80+
verbose: true,
81+
},
82+
true // overwrite existing files
83+
);
84+
};
85+
86+
if (import.meta.url === process.argv[1] || import.meta.url === `file://${process.argv[1]}`) {
87+
main().catch(e => {
88+
console.error(e);
89+
process.exit(1);
90+
});
91+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "dev-test-apollo-tooling",
3+
"description": "A setup which mimics Apollo tooling generation as close as possible",
4+
"version": "0.0.1",
5+
"type": "module",
6+
"dependencies": {
7+
"@apollo/client": "3.13.8",
8+
"@graphql-codegen/cli": "*",
9+
"@graphql-codegen/plugin-helpers": "*",
10+
"@graphql-codegen/typescript-operations": "*",
11+
"@graphql-codegen/visitor-plugin-common": "*",
12+
"@graphql-codegen/typed-document-node": "*"
13+
},
14+
"devDependencies": {
15+
"@types/node": "^25.0.3",
16+
"tsx": "4.7.0",
17+
"typescript": "^5.9.3",
18+
"vitest": "4.0.4"
19+
},
20+
"files": [
21+
"cli"
22+
],
23+
"scripts": {
24+
"start": "tsx cli/index.ts",
25+
"start:debug": "NODE_OPTIONS='--trace-warnings' tsx cli/index.ts",
26+
"start:verbose": "DEBUG='*' tsx cli/index.ts",
27+
"test": "vitest --no-watch"
28+
},
29+
"sideEffects": false
30+
}

0 commit comments

Comments
 (0)