|
1 | 1 | import fs from 'fs';
|
| 2 | +import path from 'path'; |
2 | 3 | import graphql from 'graphql';
|
3 | 4 | import { diff } from '@graphql-inspector/core';
|
4 | 5 |
|
@@ -35,36 +36,44 @@ diff(schema, schemaStd, [ignoreDirectiveChanges])
|
35 | 36 | })
|
36 | 37 | .catch(console.error);
|
37 | 38 |
|
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); |
43 | 43 |
|
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 | + }); |
59 | 73 | } 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); |
67 | 75 | }
|
68 |
| - } |
| 76 | + }); |
69 | 77 | });
|
70 |
| -}); |
| 78 | +} |
| 79 | +validateGraphql('tests/graphql'); |
0 commit comments