-
Notifications
You must be signed in to change notification settings - Fork 3
/
gatsby-node.ts
73 lines (65 loc) · 2.13 KB
/
gatsby-node.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
import { resolve } from "path";
import listTypes, { SchemaTypeGroup } from "./src/listTypes";
import { loadFiles } from "graphql-import-files";
import type { GatsbyNode } from "gatsby";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { addMocksToSchema } from "@graphql-tools/mock";
import { graphql } from "graphql";
import { watch } from "fs";
let latestGroups: SchemaTypeGroup[] = [];
let latestPagesForTypes: Record<string, string> = {};
let hasCreatedPages = false;
const nodeID = "updated-schema-id";
const contents: GatsbyNode = {
async sourceNodes({ actions: { createNode }, reporter }) {
reporter.info("Source nodes");
let seq = 1;
async function processSchemaFile() {
reporter.info("Processing schema files…");
const typeDefs = loadFiles(`./schema/*.graphql`);
const schema = makeExecutableSchema({ typeDefs });
const schemaWithMocks = addMocksToSchema({ schema });
async function runQuery(queryString: string) {
return await graphql({ schema: schemaWithMocks, source: queryString });
}
const { groups, pageForType } = await listTypes(runQuery);
latestGroups = groups;
latestPagesForTypes = pageForType;
createNode({
id: nodeID,
internal: {
type: "UpdatedSchema",
contentDigest: `${seq}`,
ignoreType: true,
},
});
seq += 1;
}
processSchemaFile();
watch("./schema", processSchemaFile);
},
async createPages({
actions: { createPage },
getNodeAndSavePathDependency,
reporter,
}) {
reporter.info("Creating pages…");
const templatePath = resolve(
`${__dirname}/src/components/typeTemplate.tsx`
);
for (const group of latestGroups) {
const page = {
path: group.outputName,
component: templatePath,
};
createPage({
...page,
context: { group, pageForType: latestPagesForTypes },
});
getNodeAndSavePathDependency(nodeID, group.outputName);
}
hasCreatedPages = true;
},
};
export const createPages = contents.createPages;
export const sourceNodes = contents.sourceNodes;