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
1 change: 1 addition & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"ignore": [
"website",
"example-*",
"dev-test*",
"@graphql-codegen/client-preset-swc-plugin",
"example-apollo-client-swc-plugin",
"example-react-nextjs-swr"
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- master

pull_request:
branches:
- master
Expand Down Expand Up @@ -137,10 +138,10 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest] # remove windows to speed up the tests
node_version: [20, 22, 24]
node_version: [22, 24]
graphql_version: [15, 16]
include:
- node-version: 20
- node-version: 22
os: windows-latest
graphql_version: 16
steps:
Expand Down
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ packages/presets/swc-plugin/tests/fixtures
# Ignore intentional error files
packages/graphql-codegen-cli/tests/test-files/schema-dir/error-schema.graphql
packages/graphql-codegen-cli/tests/test-files/error-document.graphql

# Ignore dev-tests with no prettier requirement
dev-test/test-schema/flow-types.flow.js
dev-test-apollo-tooling/src/__generated__/
23 changes: 23 additions & 0 deletions dev-test-apollo-tooling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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.

How to run this package:

1. Make sure you have the correct Yarn version installed (check the root package.json,
`packageManager` entry). Otherwise, you might run into unexplained bugs.
2. In the monorepo root, run:

yarn clean && yarn install && yarn build

3. Patch `near-operation-file` manually (the automatic patch doesn’t always work). In the monorepo
root, run:

yarn postinstall

4. Go to the `dev-test-apollo-tooling` directory and run:

cd dev-test-apollo-tooling yarn install yarn start

This will generate type files in `dev-test-apollo-tooling/src/__generated__/*`.
83 changes: 83 additions & 0 deletions dev-test-apollo-tooling/cli/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#!/usr/bin/env ts-node
import { generate } from '@graphql-codegen/cli';
import type { Types } from '@graphql-codegen/plugin-helpers';

export const GENERATED = '__generated__';
export const GLOBAL_TYPES_FILE = 'globalTypes.ts';
export const TS_GENERATED_FILE_HEADER = `\
/* tslint:disable */
/* eslint-disable */
// @generated
// This file was automatically generated and should not be edited.
`;

/**
* The following GraphQL Codegen config matches as closely as possible
* to the old apollo-tooling codegen
* @see https://github.com/apollographql/apollo-tooling/issues/2053
* */
const GRAPHQL_CODEGEN_CONFIG = {
useTypeImports: true,
namingConvention: 'keep', // Keeps naming as-is
avoidOptionals: false, // Allow '?' on variables fields
nonOptionalTypename: true, // Forces `__typename` on all selection sets
skipTypeNameForRoot: true, // Don't generate __typename for root types
omitOperationSuffix: true, // Don't add 'Query', 'Mutation' or 'Subscription' suffixes to operation result types
fragmentSuffix: '', // Don't add 'Fragment' suffix to fragment result types
extractAllFieldsToTypesCompact: true, // Extracts all fields to separate types (similar to apollo-codegen behavior)
printFieldsOnNewLines: true, // Prints each field on a new line (similar to apollo-codegen behavior)
enumType: 'native',
generateOperationTypes: true,
};

export const main = async () => {
const cwd = process.cwd();

const localSchemaFilePath = `${cwd}/schema.graphql`;

const includes = ['src'];

const generatePaths: { [scanPath: string]: Types.ConfiguredOutput } = {};

// Prepare the required structure for GraphQL Codegen
// eslint-disable-next-line unicorn/no-array-for-each
includes.forEach((include: string) => {
generatePaths[include] = {
preset: 'near-operation-file', // This preset tells the codegen to generate multiple files instead of one
presetConfig: {
extension: '.ts',
folder: GENERATED, // Output folder for generated files
},
plugins: [
'typescript-operations',
{
add: {
content: TS_GENERATED_FILE_HEADER,
},
},
],
};
});

await generate({
schema: localSchemaFilePath,
documents: [
...includes.map((include: any) => `${include}/**/*.{js,jsx,ts,tsx}`),
`!**/${GENERATED}/**`,
],
config: GRAPHQL_CODEGEN_CONFIG,
generates: generatePaths,
silent: false,
overwrite: true,
debug: false,
verbose: false,
});
};

if (import.meta.url === process.argv[1] || import.meta.url === `file://${process.argv[1]}`) {
main().catch(e => {
// eslint-disable-next-line no-console
console.error(e);
process.exit(1);
});
}
31 changes: 31 additions & 0 deletions dev-test-apollo-tooling/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "dev-test-apollo-tooling",
"version": "0.0.1",
"type": "module",
"description": "A setup which mimics Apollo tooling generation as close as possible",
"private": true,
"files": [
"cli"
],
"scripts": {
"start": "tsx cli/index.ts",
"start:debug": "NODE_OPTIONS='--trace-warnings' tsx cli/index.ts",
"start:verbose": "DEBUG='*' tsx cli/index.ts",
"test": "vitest --no-watch"
},
"dependencies": {
"@apollo/client": "3.13.8",
"@graphql-codegen/cli": "*",
"@graphql-codegen/plugin-helpers": "*",
"@graphql-codegen/typed-document-node": "*",
"@graphql-codegen/typescript-operations": "*",
"@graphql-codegen/visitor-plugin-common": "*"
},
"devDependencies": {
"@types/node": "^25.0.3",
"tsx": "4.7.0",
"typescript": "^5.9.3",
"vitest": "4.0.4"
},
"sideEffects": false
}
Loading
Loading