-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate-graphql.mjs
82 lines (72 loc) · 2.09 KB
/
validate-graphql.mjs
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
import { unified } from "unified";
import { visit } from "unist-util-visit";
import parse from "remark-parse";
import stringify from "remark-stringify";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { graphql, buildClientSchema, validate } from "graphql";
import { parse as parser } from "graphql";
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const schema = require("./graphql.schema.json");
// Obtain the target file
const [, , file] = process.argv;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
if (!file) {
throw new Error("Provide the target file");
}
console.log(`Reviewing file: ${file}`);
const nodes = [];
// Obtain the markdown contents
const sourceFileResolved = path.resolve(__dirname, file);
const contents = fs.readFileSync(sourceFileResolved);
function validator() {
return transformer;
function transformer(tree) {
visit(tree, "code", function (node, index) {
nodes.push(node);
});
}
}
const graphqlSchemaObj = buildClientSchema(schema);
// Parse the AST
const tree = unified()
.use(parse)
.use(validator)
.use(stringify)
.process(contents, function (err) {
if (err) {
console.error(err);
}
Promise.all(
nodes.map((node) => {
return new Promise((success, fail) => {
if (node.lang !== "graphql") {
success();
return;
}
const doc = parser(node.value);
const errors = validate(graphqlSchemaObj, doc);
if (errors.length) {
// console.dir(errors, { depth: null });
fail(
new Error(
`Invalid graphql encountered at ${node.position.start.line}:${
node.position.start.column
}, ${errors.map((e) => e.message).join(" ")}`
)
);
return;
}
success();
});
})
)
.then(() => process.exit(0))
.catch((err) => {
console.error(err);
process.exit(255);
});
});