Skip to content

Commit 4a7b9a1

Browse files
committed
scripts: validate graphql
Signed-off-by: jsvisa <[email protected]>
1 parent bd2146f commit 4a7b9a1

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

scripts/graphql-validate.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import path from 'path';
23
import graphql from 'graphql';
34
import { diff } from '@graphql-inspector/core';
45

@@ -35,36 +36,44 @@ diff(schema, schemaStd, [ignoreDirectiveChanges])
3536
})
3637
.catch(console.error);
3738

38-
fs.readdir('tests/graphql', (_, files) => {
39-
files.forEach((file) => {
40-
if (!fs.lstatSync(`tests/graphql/${file}`).isDirectory()) {
41-
return;
42-
}
39+
function validateGraphql(dir) {
40+
fs.readdir(dir, (_, files) => {
41+
files.forEach((file) => {
42+
const filePath = path.join(dir, file);
4343

44-
const query = graphql.parse(
45-
fs.readFileSync(`tests/graphql/${file}/request.gql`, 'utf8')
46-
);
47-
const output = JSON.parse(
48-
fs.readFileSync(`tests/graphql/${file}/response.json`, 'utf8')
49-
);
50-
if (!('statusCode' in output) || !('responses' in output)) {
51-
throw new Error(
52-
`GraphQL response ${file} without 'statusCode' or 'responses' keys`
53-
);
54-
}
55-
if (output['statusCode'] === 200) {
56-
const result = graphql.validate(schema, query);
57-
if (result.length === 0) {
58-
console.log(`GraphQL request ${file} validated successfully.`);
44+
console.log(`Validating file: ${filePath}`);
45+
if (fs.statSync(filePath).isFile()) {
46+
const lines = fs.readFileSync(filePath, 'utf8').split('\n');
47+
48+
let prev = null;
49+
lines.forEach((line) => {
50+
if (prev && prev.startsWith('>> ') && line.startsWith('<< ')) {
51+
const output = JSON.parse(line.substring(3));
52+
53+
// Validate the success Query
54+
if (!'errors' in output) {
55+
const query = graphql.parse(line.substring(3));
56+
const result = graphql.validate(schema, query);
57+
if (result.length === 0) {
58+
console.log(`GraphQL test ${file} validated successfully.`);
59+
} else {
60+
throw new Error(
61+
`GraphQL query ${file} failed validation:\n${JSON.stringify(
62+
result,
63+
null,
64+
2
65+
)}`
66+
);
67+
}
68+
}
69+
}
70+
71+
prev = line;
72+
});
5973
} else {
60-
throw new Error(
61-
`GraphQL query ${file} failed validation:\n${JSON.stringify(
62-
result,
63-
null,
64-
2
65-
)}`
66-
);
74+
validateGraphql(filePath);
6775
}
68-
}
76+
});
6977
});
70-
});
78+
}
79+
validateGraphql('tests/graphql');

0 commit comments

Comments
 (0)