forked from stakewise/v3-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.ts
More file actions
101 lines (84 loc) · 2.64 KB
/
codegen.ts
File metadata and controls
101 lines (84 loc) · 2.64 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import type { CodegenConfig } from '@graphql-codegen/cli'
import { Network } from './src/utils/enums'
import configs from './src/utils/configs'
// For every network we have same gql shema, so we can use just Mainnet here
const urls = configs[Network.Mainnet].api
// 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