-
Notifications
You must be signed in to change notification settings - Fork 26
/
codegen.ts
91 lines (82 loc) · 2.89 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
import type { CodegenConfig } from "@graphql-codegen/cli";
import type { Types } from "@graphql-codegen/plugin-helpers";
import { join, sep } from "path";
const basePath = join("src", "graphql");
// FIXUP: to be replaced with new deployed API.
const explorerSchema = "https://api.cartesiscan.io/graphql";
const explorerAPIQueries = join(".", "graphql", "queries.graphql");
const rollupsSchema = join(".", "graphql", "rollups", "schema.graphql");
const rollupsDocuments = join(".", "graphql", "rollups", "queries.graphql");
console.info(`Codegen will use schema URL: ${explorerSchema}`);
interface CommonConfig extends Pick<Types.Config, "schema" | "documents"> {
dirname: string;
}
type Generates = Partial<Pick<Types.Config, "generates">>;
const setup: CommonConfig[] = [
{
dirname: "rollups",
schema: rollupsSchema,
documents: rollupsDocuments,
},
{
dirname: "explorer",
schema: explorerSchema,
documents: explorerAPIQueries,
},
];
const generateConfig = (configs: CommonConfig[]): Generates => {
return configs.reduce<Generates>(
(generates, { dirname, documents, schema }) => {
const path = join(basePath, dirname);
return {
...generates,
[join(path, "types.ts")]: {
schema,
documents,
plugins: ["typescript"],
},
[join(path, "operations.ts")]: {
schema,
documents,
preset: "import-types",
plugins: [
"typescript-operations",
"typescript-urql",
"named-operations-object",
],
presetConfig: {
typesPath: `.${sep}types`,
},
config: {
withHooks: false,
// named-operations-object plugin configs
enumAsTypes: true,
useConsts: true,
identifierName: "allOperations",
},
},
[join(path, "hooks", "queries.tsx")]: {
schema,
documents,
plugins: ["typescript-urql"],
config: {
withHooks: true,
importOperationTypesFrom: "Operations",
documentMode: "external",
importDocumentNodeExternallyFrom: join(
"..",
"operations.ts",
),
},
},
};
},
{},
);
};
const codegenConfig: CodegenConfig = {
generates: {
...generateConfig(setup),
},
};
export default codegenConfig;