-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathwrite-graphql-schema.ts
More file actions
59 lines (51 loc) · 1.93 KB
/
write-graphql-schema.ts
File metadata and controls
59 lines (51 loc) · 1.93 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
import { writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { minifyIntrospectionQuery } from "@urql/introspection";
import { introspectionFromSchema, lexicographicSortSchema, printSchema } from "graphql";
import { makeLogger } from "@/lib/logger";
const logger = makeLogger("write-graphql-schema");
const MONOREPO_ROOT = resolve(import.meta.dirname, "../../../../../");
const ENSSDK_ROOT = resolve(MONOREPO_ROOT, "packages/enssdk/");
const GENERATED_DIR = resolve(ENSSDK_ROOT, "src/omnigraph/generated");
async function _writeGraphQLSchema() {
const { schema: unsortedSchema } = await import("@/omnigraph-api/schema");
const schema = lexicographicSortSchema(unsortedSchema);
const sdl = printSchema(schema);
const introspection = minifyIntrospectionQuery(introspectionFromSchema(schema), {
/**
* We include Scalar types in the introspection in order to power automatic bigint deserialization
* within the Omnigraph's urql GraphCache.
*/
includeScalars: true,
});
await Promise.all([
writeFile(resolve(GENERATED_DIR, "schema.graphql"), sdl),
writeFile(
resolve(GENERATED_DIR, "introspection.ts"),
`export const introspection = ${JSON.stringify(introspection)} as const;\n`,
),
]);
}
/**
* Attempts to write the GraphQL Schema, swallowing any errors.
*/
export async function writeGraphQLSchema() {
try {
await _writeGraphQLSchema();
logger.info(`Wrote SDL to ${GENERATED_DIR}`);
} catch (error) {
logger.warn(error, `Unable to write SDL to ${GENERATED_DIR}`);
}
}
// when executed directly (`pnpm generate:gqlschema`), write generated schema and produce an exit code
if (resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
try {
await _writeGraphQLSchema();
console.log(`Wrote SDL to ${GENERATED_DIR}`);
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
}