File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments