-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathindex.ts
More file actions
83 lines (74 loc) · 2.66 KB
/
index.ts
File metadata and controls
83 lines (74 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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);
});
}