@@ -8,7 +8,7 @@ export interface DirectoryToAstOptions {
8
8
exclude ?: RegExp | ( ( path : string , kind : 'dir' | 'file' , filename : string ) => boolean ) ;
9
9
}
10
10
11
- export type AstNodeKinds = 'rootType' | 'dir' | 'file' ;
11
+ export type AstNodeKinds = 'rootType' | 'dir' | 'file' | 'root' ;
12
12
13
13
export interface AstBaseNode {
14
14
kind : AstNodeKinds ;
@@ -39,10 +39,13 @@ export interface AstFileNode extends AstBaseNode {
39
39
} ;
40
40
}
41
41
42
- export interface AstResult {
43
- query ?: AstRootTypeNode ;
44
- mutation ?: AstRootTypeNode ;
45
- subscription ?: AstRootTypeNode ;
42
+ type RootTypeNames = 'query' | 'mutation' | 'subscription' ;
43
+
44
+ export interface AstRootNode extends AstBaseNode {
45
+ kind : 'root' ;
46
+ children : {
47
+ [ T in RootTypeNames ] ?: AstRootTypeNode ;
48
+ } ;
46
49
}
47
50
48
51
export const defaultOptions : DirectoryToAstOptions = {
@@ -52,7 +55,7 @@ export const defaultOptions: DirectoryToAstOptions = {
52
55
export function directoryToAst (
53
56
m : NodeModule ,
54
57
options : DirectoryToAstOptions = defaultOptions
55
- ) : AstResult {
58
+ ) : AstRootNode {
56
59
// if no path was passed in, assume the equivelant of __dirname from caller
57
60
// otherwise, resolve path relative to the equivalent of __dirname
58
61
const schemaPath = options ?. relativePath
@@ -66,7 +69,12 @@ export function directoryToAst(
66
69
}
67
70
} ) ;
68
71
69
- const result = { } as AstResult ;
72
+ const result = {
73
+ kind : 'root' ,
74
+ name : basename ( schemaPath ) ,
75
+ absPath : schemaPath ,
76
+ children : { } ,
77
+ } as AstRootNode ;
70
78
71
79
fs . readdirSync ( schemaPath ) . forEach ( ( filename ) => {
72
80
const absPath = join ( schemaPath , filename ) ;
@@ -76,8 +84,8 @@ export function directoryToAst(
76
84
const re = / ^ ( q u e r y | m u t a t i o n | s u b s c r i p t i o n ) ( \. ( .* ) ) ? $ / i;
77
85
const found = dirName . match ( re ) ;
78
86
if ( found ) {
79
- const opType = found [ 1 ] . toLowerCase ( ) as keyof AstResult ;
80
- let rootTypeAst = result [ opType ] ;
87
+ const opType = found [ 1 ] . toLowerCase ( ) as keyof AstRootNode [ 'children' ] ;
88
+ let rootTypeAst = result . children [ opType ] ;
81
89
if ( ! rootTypeAst )
82
90
rootTypeAst = {
83
91
kind : 'rootType' ,
@@ -101,7 +109,7 @@ export function directoryToAst(
101
109
rootTypeAst . namespaceConfig = astDir . namespaceConfig ;
102
110
}
103
111
}
104
- result [ opType ] = rootTypeAst ;
112
+ result . children [ opType ] = rootTypeAst ;
105
113
}
106
114
}
107
115
}
0 commit comments