-
Notifications
You must be signed in to change notification settings - Fork 5
/
codegen.ts
121 lines (99 loc) · 3.08 KB
/
codegen.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import type { CodegenConfig } from '@graphql-codegen/cli'
import { Network } from './src/utils/enums'
import configs from './src/utils/configs'
let network: Network = Network.Holesky
if (process.env.NETWORK === 'mainnet') {
network = Network.Mainnet
}
if (process.env.NETWORK === 'gnosis') {
network = Network.Gnosis
}
const config = configs[network]
const subgraphIndex = Number(process.env.SUBGRAPH_INDEX || 0)
const subgraphUrl = Array.isArray(config.api.subgraph)
? config.api.subgraph[subgraphIndex]
: config.api.subgraph
const urls: Record<string, string> = {
backend: config.api.backend,
subgraph: subgraphUrl,
}
console.log(`Generating types for network: ${config.network.id}`, urls)
// https://the-guild.dev/graphql/codegen/plugins/typescript/typescript
const typesConfig = {
maybeValue: 'T',
defaultScalarType: 'string', // sets BigDecimal to string instead of any
noExport: true, // replaced with namespace
enumsAsTypes: true,
skipTypename: true,
avoidOptionals: true, // uses a: Maybe instead of a?: Maybe
dedupeFragments: true,
}
// https://the-guild.dev/graphql/codegen/plugins/typescript/typescript-urql
const requestsConfig = {
maybeValue: 'T',
defaultScalarType: 'string',
operationResultSuffix: 'Payload', // gives suffix to payload type
noExport: false, // disables export by default
noGraphQLTag: true,
skipTypename: true,
avoidOptionals: true, // uses a: Maybe instead of a?: Maybe
dedupeFragments: false, // removes fragment duplication
arrayInputCoercion: false, // strict array types
}
type Source = keyof typeof urls
const getSchemaOutput = (source: Source): CodegenConfig['generates'][string] => {
return {
schema: urls[source],
plugins: [ 'schema-ast' ],
}
}
const getTypesOutput = (source: Source): CodegenConfig['generates'][string] => {
return {
schema: urls[source],
config: typesConfig,
plugins: [ 'typescript' ],
}
}
const getRequestsOutput = (source: Source): CodegenConfig['generates'][string] => {
return {
schema: urls[source],
config: requestsConfig,
plugins: [
'typescript-operations',
],
preset: 'near-operation-file',
documents: [ `./src/graphql/${source}/**/*.graphql` ],
presetConfig: {
baseTypesPath: `types/graphql/${source}.d.ts`,
extension: '.graphql.ts',
},
}
}
const generateConfig = (): CodegenConfig => {
const generates: CodegenConfig['generates'] = {}
Object.keys(urls).forEach((source) => {
const outputs = {
schema: {
path: `src/graphql/${source}/schema.graphql`,
config: getSchemaOutput(source as Source),
},
types: {
path: `src/types/graphql/${source}.d.ts`,
config: getTypesOutput(source as Source),
},
hooks: {
path: `src/graphql/${source}`,
config: getRequestsOutput(source as Source),
},
}
Object.keys(outputs).forEach((outputType) => {
const { path, config } = outputs[outputType as keyof typeof outputs]
generates[path] = config
})
})
return {
generates,
}
}
const result = generateConfig()
export default result