-
Notifications
You must be signed in to change notification settings - Fork 407
/
Copy pathgraphql-validate.js
79 lines (70 loc) · 2.32 KB
/
graphql-validate.js
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
import fs from 'fs';
import path from 'path';
import graphql from 'graphql';
import { diff } from '@graphql-inspector/core';
function ignoreDirectiveChanges(obj) {
return obj.changes.filter((change) => !change.type.startsWith('DIRECTIVE'));
}
// Validate JSON schema
const raw = fs.readFileSync('graphql.json');
const schema = graphql.buildClientSchema(JSON.parse(raw));
graphql.assertValidSchema(schema);
console.log('GraphQL JSON schema validated successfully.');
// Validate standard schema
const rawStd = fs.readFileSync('schema.graphqls', 'utf8');
const schemaStd = graphql.buildSchema(rawStd);
graphql.assertValidSchema(schemaStd);
console.log('GraphQL standard schema validated successfully.');
// Compare and make sure JSON and standard schemas match.
diff(schema, schemaStd, [ignoreDirectiveChanges])
.then((changes) => {
if (changes.length === 0) {
console.log('GraphQL schemas match.');
return;
}
throw new Error(
`Found differences between JSON and standard:\n${JSON.stringify(
changes,
null,
2
)}`
);
})
.catch(console.error);
function validateGraphql(dir) {
fs.readdir(dir, (_, files) => {
files.forEach((file) => {
const filePath = path.join(dir, file);
console.log(`Validating file: ${filePath}`);
if (fs.statSync(filePath).isFile()) {
const lines = fs.readFileSync(filePath, 'utf8').split('\n');
let prev = null;
lines.forEach((line) => {
if (prev && prev.startsWith('>> ') && line.startsWith('<< ')) {
const output = JSON.parse(line.substring(3));
// Validate the success Query
if (!'errors' in output) {
const query = graphql.parse(line.substring(3));
const result = graphql.validate(schema, query);
if (result.length === 0) {
console.log(`GraphQL test ${file} validated successfully.`);
} else {
throw new Error(
`GraphQL query ${file} failed validation:\n${JSON.stringify(
result,
null,
2
)}`
);
}
}
}
prev = line;
});
} else {
validateGraphql(filePath);
}
});
});
}
validateGraphql('tests/graphql');