|
| 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 | +} |
0 commit comments