-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.ts
90 lines (82 loc) · 2.94 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 { resolvers } from "graphql-scalars";
import { readdirSync } from "fs";
import type { CodegenConfig } from "@graphql-codegen/cli";
import type { ClientPresetConfig } from "@graphql-codegen/client-preset";
import type { TypeScriptPluginConfig } from "@graphql-codegen/typescript";
import type { GraphQLScalarTypeExtensions } from "graphql";
const graphqlScalarsClientDefs = Object.entries(resolvers).reduce<
Record<string, GraphQLScalarTypeExtensions["codegenScalarType"]>
>((acc, [key, value]) => {
const { extensions } = value.toConfig();
if (!("codegenScalarType" in extensions)) {
return acc;
}
acc[key] = extensions.codegenScalarType;
return acc;
}, {});
const presetConfig: ClientPresetConfig = {
fragmentMasking: {
unmaskFunctionName: "getFragmentData",
},
};
// NOTE: this configuration object requires the patch in `.yarn/patches/@graphql-codegen-client-preset-npm-4.3.3-ec8a799259.patch` which is automatically applied by Yarn
// If you get errors with types not matching up between GraphQL an @ukdanceblue/common, then the patch probably isn't working
const config: TypeScriptPluginConfig = {
useTypeImports: true,
enumsAsConst: true,
enumValues: {
AuthSource: "@ukdanceblue/common#AuthSource",
AccessLevel: "@ukdanceblue/common#AccessLevel",
DbRole: "@ukdanceblue/common#DbRole",
CommitteeRole: "@ukdanceblue/common#CommitteeRole",
CommitteeIdentifier: "@ukdanceblue/common#CommitteeIdentifier",
// ErrorCode: "@ukdanceblue/common#ErrorCode",
MembershipPositionType: "@ukdanceblue/common#MembershipPositionType",
TeamLegacyStatus: "@ukdanceblue/common#TeamLegacyStatus",
TeamType: "@ukdanceblue/common#TeamType",
SortDirection: "@ukdanceblue/common#SortDirection",
// Comparator: "@ukdanceblue/common#Comparator",
NumericComparator: "@ukdanceblue/common#NumericComparator",
StringComparator: "@ukdanceblue/common#StringComparator",
// IsComparator: "@ukdanceblue/common#IsComparator",
},
scalars: {
LuxonDateRange: "string",
LuxonDuration: "string",
GlobalId: "string",
...graphqlScalarsClientDefs,
},
strictScalars: true,
immutableTypes: true,
};
const generates: CodegenConfig["generates"] = {};
// We do this readdir thing in case a partial copy of the monorepo is being used
const packages = readdirSync("./packages");
if (packages.includes("mobile")) {
generates["packages/mobile/src/graphql/"] = {
preset: "client",
presetConfig,
documents: ["packages/mobile/src"],
};
}
if (packages.includes("portal")) {
generates["packages/portal/graphql/"] = {
preset: "client",
presetConfig,
documents: ["packages/portal/src"],
};
}
const codegenConfig: CodegenConfig = {
schema: "schema.graphql",
hooks: {
onError: (e) => {
console.error("Error generating GraphQL client:", e);
},
},
noSilentErrors: true,
emitLegacyCommonJSImports: false,
generates,
ignoreNoDocuments: true,
config,
};
export default codegenConfig;