Skip to content

Commit 0deb40b

Browse files
committed
scripts: graphql schema validate
Signed-off-by: jsvisa <delweng@gmail.com>
1 parent 7af4342 commit 0deb40b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

scripts/graphql-schema-validate.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import fs from 'fs';
2+
import graphql from 'graphql';
3+
4+
// Validate JSON schema
5+
const raw = fs.readFileSync('graphql/schemas/schema.gql', 'utf8');
6+
const schema = graphql.buildSchema(raw);
7+
graphql.assertValidSchema(schema);
8+
console.log('GraphQL schema validated successfully.');
9+
10+
fs.readdir('graphql/tests', (_, files) => {
11+
files.forEach((file) => {
12+
const query = graphql.parse(
13+
fs.readFileSync(`graphql/tests/${file}/request.gql`, 'utf8')
14+
);
15+
const output = JSON.parse(
16+
fs.readFileSync(`graphql/tests/${file}/response.json`, 'utf8')
17+
);
18+
if (!('statusCode' in output) || !('responses' in output)) {
19+
throw new Error(
20+
`GraphQL response ${file} without 'statusCode' or 'responses' keys`
21+
);
22+
}
23+
if (output['statusCode'] === 200) {
24+
const result = graphql.validate(schema, query);
25+
if (result.length === 0) {
26+
console.log(`GraphQL request ${file} validated successfully.`);
27+
} else {
28+
throw new Error(
29+
`GraphQL query ${file} failed validation:\n${JSON.stringify(
30+
result,
31+
null,
32+
2
33+
)}`
34+
);
35+
}
36+
}
37+
});
38+
});

0 commit comments

Comments
 (0)