This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
forked from acaldas/document-model-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.ts
90 lines (86 loc) · 2.59 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
import { type CodegenConfig } from "@graphql-codegen/cli";
const tsConfig = {
strict: true,
strictScalars: true,
scalars: {
Unknown: "unknown",
DateTime: "string",
Attachment: "string",
Address: "`${string}:0x${string}`",
},
enumsAsTypes: true,
allowEnumStringTypes: true,
avoidOptionals: {
field: true,
},
useIndexSignature: true,
noSchemaStitching: true,
skipTypename: false,
// maybeValue: "T | null | undefined",
inputMaybeValue: "T | null | undefined",
};
function schemaConfig(name: string): CodegenConfig["generates"] {
return {
[`src/generated/${name}/types.ts`]: {
schema: [
{
[`src/schemas/${name}.graphql`]: {
skipGraphQLImport: false,
},
},
],
plugins: ["typescript"],
config: tsConfig,
},
[`src/generated/${name}/resolvers.ts`]: {
schema: [
{
[`src/schemas/${name}.graphql`]: {
skipGraphQLImport: false,
},
},
],
plugins: [
{
add: {
content: "import * as SchemaTypes from './types';",
},
},
"typescript-resolvers",
],
config: { ...tsConfig, namespacedImportName: "SchemaTypes" },
},
[`src/generated/${name}/zod.ts`]: {
schema: `src/schemas/${name}.graphql`,
plugins: ["typescript-validation-schema"],
config: {
importFrom: `./`,
schema: "zod",
...tsConfig,
scalarSchemas: {
Unknown: "z.unknown()",
DateTime: "z.string().datetime()",
Attachment: "z.string()",
Address:
"z.custom<`${string}:0x${string}`>((val) => /^[a-zA-Z0-9]+:0x[a-fA-F0-9]{40}$/.test(val as string))",
},
directives: {
equals: {
value: ["regex", "/^$1$/"],
},
},
withObjectType: true,
},
},
};
}
const config: CodegenConfig = {
overwrite: true,
generates: {
...schemaConfig("document"),
...schemaConfig("budget-statement"),
...schemaConfig("document-model"),
...schemaConfig("scope-framework"),
},
};
export default config;