-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathvalidate-graphql.js
More file actions
180 lines (160 loc) · 5.14 KB
/
validate-graphql.js
File metadata and controls
180 lines (160 loc) · 5.14 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import fs from 'fs';
import path from 'path';
import { buildClientSchema, parse, validate } from 'graphql';
// Load the introspection schema
const schemaJson = JSON.parse(fs.readFileSync('/tmp/unchained-schema.json', 'utf8'));
const schema = buildClientSchema(schemaJson.data);
// Find all markdown files
function findMarkdownFiles(dir) {
const results = [];
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
results.push(...findMarkdownFiles(fullPath));
} else if (item.endsWith('.md')) {
results.push(fullPath);
}
}
return results;
}
// Extract GraphQL code blocks from markdown
function extractGraphQLBlocks(content, filePath) {
const blocks = [];
// Match ```graphql blocks
const graphqlRegex = /```graphql\n([\s\S]*?)```/g;
let match;
while ((match = graphqlRegex.exec(content)) !== null) {
const lineNumber = content.substring(0, match.index).split('\n').length;
blocks.push({
code: match[1].trim(),
lineNumber,
filePath,
type: 'graphql',
});
}
// Also match ```gql blocks
const gqlRegex = /```gql\n([\s\S]*?)```/g;
while ((match = gqlRegex.exec(content)) !== null) {
const lineNumber = content.substring(0, match.index).split('\n').length;
blocks.push({
code: match[1].trim(),
lineNumber,
filePath,
type: 'gql',
});
}
return blocks;
}
// Validate a GraphQL document
function validateGraphQL(code, schema) {
try {
const document = parse(code);
const errors = validate(schema, document);
return { parsed: true, errors };
} catch (parseError) {
return { parsed: false, parseError: parseError.message };
}
}
// Main
const docsDir = '/Users/pozylon/Repositories/unchained/docs/docs';
const mdFiles = findMarkdownFiles(docsDir);
console.log(`Found ${mdFiles.length} markdown files\n`);
const allBlocks = [];
const results = {
valid: [],
invalid: [],
parseErrors: [],
};
for (const file of mdFiles) {
const content = fs.readFileSync(file, 'utf8');
const blocks = extractGraphQLBlocks(content, file);
allBlocks.push(...blocks);
}
console.log(`Found ${allBlocks.length} GraphQL code blocks\n`);
console.log('='.repeat(80));
console.log('VALIDATING GRAPHQL SNIPPETS');
console.log('='.repeat(80));
for (const block of allBlocks) {
const result = validateGraphQL(block.code, schema);
const relPath = block.filePath.replace('/Users/pozylon/Repositories/unchained/docs/docs/', '');
if (!result.parsed) {
results.parseErrors.push({
...block,
error: result.parseError,
});
console.log(`\n❌ PARSE ERROR in ${relPath}:${block.lineNumber}`);
console.log(` Error: ${result.parseError}`);
console.log(` Code snippet (first 200 chars):\n ${block.code.substring(0, 200)}...`);
} else if (result.errors.length > 0) {
results.invalid.push({
...block,
errors: result.errors,
});
console.log(`\n❌ VALIDATION ERROR in ${relPath}:${block.lineNumber}`);
for (const err of result.errors) {
console.log(` - ${err.message}`);
}
console.log(` Code snippet (first 300 chars):\n ${block.code.substring(0, 300)}...`);
} else {
results.valid.push(block);
}
}
console.log('\n' + '='.repeat(80));
console.log('SUMMARY');
console.log('='.repeat(80));
console.log(`✅ Valid: ${results.valid.length}`);
console.log(`❌ Invalid: ${results.invalid.length}`);
console.log(`❌ Parse errors: ${results.parseErrors.length}`);
// Output detailed results
if (results.invalid.length > 0 || results.parseErrors.length > 0) {
console.log('\n' + '='.repeat(80));
console.log('FILES WITH ERRORS:');
console.log('='.repeat(80));
const errorFiles = new Map();
for (const block of [...results.invalid, ...results.parseErrors]) {
const relPath = block.filePath.replace('/Users/pozylon/Repositories/unchained/docs/docs/', '');
if (!errorFiles.has(relPath)) {
errorFiles.set(relPath, []);
}
errorFiles.get(relPath).push(block);
}
for (const [file, blocks] of errorFiles) {
console.log(`\n📄 ${file}`);
for (const block of blocks) {
console.log(
` Line ${block.lineNumber}: ${block.error || block.errors.map((e) => e.message).join('; ')}`,
);
}
}
}
// Write detailed JSON report
fs.writeFileSync(
'/tmp/graphql-validation-report.json',
JSON.stringify(
{
summary: {
total: allBlocks.length,
valid: results.valid.length,
invalid: results.invalid.length,
parseErrors: results.parseErrors.length,
},
invalid: results.invalid.map((b) => ({
file: b.filePath.replace('/Users/pozylon/Repositories/unchained/docs/docs/', ''),
line: b.lineNumber,
errors: b.errors.map((e) => e.message),
code: b.code,
})),
parseErrors: results.parseErrors.map((b) => ({
file: b.filePath.replace('/Users/pozylon/Repositories/unchained/docs/docs/', ''),
line: b.lineNumber,
error: b.error,
code: b.code,
})),
},
null,
2,
),
);
console.log('\n\nDetailed report written to /tmp/graphql-validation-report.json');