Repository navigation
Expand file tree
/
Copy pathcodegen.ts
More file actions
61 lines (55 loc) · 2 KB
/
Copy pathcodegen.ts
File metadata and controls
61 lines (55 loc) · 2 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
import { mkdir, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { codegen } from "@graphql-codegen/core";
import * as typescript from "@graphql-codegen/typescript";
import * as operations from "@graphql-codegen/typescript-operations";
import * as resolvers from "@graphql-codegen/typescript-resolvers";
import * as documentNode from "@graphql-codegen/typed-document-node";
import { loadFilesSync } from "@graphql-tools/load-files";
import { mergeTypeDefs } from "@graphql-tools/merge";
import { type DocumentNode } from "graphql";
async function generate() {
const schema = mergeTypeDefs(loadFilesSync("bookit/src/schemas/v1/*.gql"));
const documents = loadFilesSync<DocumentNode>("frontend/src/**/*.graphql").map((document) => ({
document,
}));
const config = { useTypeImports: true, enumsAsTypes: true };
const outputs = [
{
filename: "frontend/src/generated/graphql.ts",
content: await codegen({
schema,
documents,
config,
filename: "frontend/src/generated/graphql.ts",
plugins: [{ operations: {} }, { documentNode: {} }],
pluginMap: { operations, documentNode },
}),
},
{
filename: "bookit/src/generated/schema.ts",
content: await codegen({
schema,
documents: [],
filename: "bookit/src/generated/schema.ts",
config: {
...config,
contextType: "../utils/commonTypes#Context",
// Fields such as super_groups are resolved from the session user.
mappers: { User: "../models/user#User as SessionUser" },
},
plugins: [{ typescript: {} }, { resolvers: {} }],
pluginMap: { typescript, resolvers },
}),
},
];
for (const { filename, content } of outputs) {
await mkdir(dirname(filename), { recursive: true });
await writeFile(filename, `${content.trimEnd()}\n`);
console.log(`Generated ${filename}`);
}
}
void generate().catch((error) => {
console.error(error);
process.exitCode = 1;
});