@@ -8,7 +8,7 @@ export interface DirectoryToAstOptions {
88 exclude ?: RegExp | ( ( path : string , kind : 'dir' | 'file' , filename : string ) => boolean ) ;
99}
1010
11- export type AstNodeKinds = 'rootType' | 'dir' | 'file' ;
11+ export type AstNodeKinds = 'rootType' | 'dir' | 'file' | 'root' ;
1212
1313export interface AstBaseNode {
1414 kind : AstNodeKinds ;
@@ -39,10 +39,13 @@ export interface AstFileNode extends AstBaseNode {
3939 } ;
4040}
4141
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+ } ;
4649}
4750
4851export const defaultOptions : DirectoryToAstOptions = {
@@ -52,7 +55,7 @@ export const defaultOptions: DirectoryToAstOptions = {
5255export function directoryToAst (
5356 m : NodeModule ,
5457 options : DirectoryToAstOptions = defaultOptions
55- ) : AstResult {
58+ ) : AstRootNode {
5659 // if no path was passed in, assume the equivelant of __dirname from caller
5760 // otherwise, resolve path relative to the equivalent of __dirname
5861 const schemaPath = options ?. relativePath
@@ -66,7 +69,12 @@ export function directoryToAst(
6669 }
6770 } ) ;
6871
69- const result = { } as AstResult ;
72+ const result = {
73+ kind : 'root' ,
74+ name : basename ( schemaPath ) ,
75+ absPath : schemaPath ,
76+ children : { } ,
77+ } as AstRootNode ;
7078
7179 fs . readdirSync ( schemaPath ) . forEach ( ( filename ) => {
7280 const absPath = join ( schemaPath , filename ) ;
@@ -76,8 +84,8 @@ export function directoryToAst(
7684 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;
7785 const found = dirName . match ( re ) ;
7886 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 ] ;
8189 if ( ! rootTypeAst )
8290 rootTypeAst = {
8391 kind : 'rootType' ,
@@ -101,7 +109,7 @@ export function directoryToAst(
101109 rootTypeAst . namespaceConfig = astDir . namespaceConfig ;
102110 }
103111 }
104- result [ opType ] = rootTypeAst ;
112+ result . children [ opType ] = rootTypeAst ;
105113 }
106114 }
107115 }
0 commit comments