@@ -7,7 +7,30 @@ const { readFile, stat } = fsPromises;
7
7
8
8
const DEFAULT_IGNORED_EXTENSIONS = [ 'spec' , 'test' , 'd' , 'map' ] ;
9
9
const DEFAULT_EXTENSIONS = [ 'gql' , 'graphql' , 'graphqls' , 'ts' , 'js' ] ;
10
- const DEFAULT_EXPORT_NAMES = [ 'typeDefs' , 'schema' ] ;
10
+ const DEFAULT_EXPORT_NAMES = [ 'schema' , 'typeDef' , 'typeDefs' , 'resolver' , 'resolvers' ] ;
11
+ const DEFAULT_EXTRACT_EXPORTS_FACTORY = ( exportNames : string [ ] ) => ( fileExport : any ) : any | null => {
12
+ if ( ! fileExport ) {
13
+ return null ;
14
+ }
15
+
16
+ if ( fileExport . default ) {
17
+ for ( const exportName of exportNames ) {
18
+ if ( fileExport . default [ exportName ] ) {
19
+ return fileExport . default [ exportName ] ;
20
+ }
21
+ }
22
+
23
+ return fileExport . default ;
24
+ }
25
+
26
+ for ( const exportName of exportNames ) {
27
+ if ( fileExport [ exportName ] ) {
28
+ return fileExport [ exportName ] ;
29
+ }
30
+ }
31
+
32
+ return fileExport ;
33
+ } ;
11
34
12
35
function asArray < T > ( obj : T | T [ ] ) : T [ ] {
13
36
if ( obj instanceof Array ) {
@@ -56,30 +79,6 @@ function buildGlob(
56
79
return `${ basePath } ${ recursive ? '/**' : '' } /${ ignored } +(${ ext } )` ;
57
80
}
58
81
59
- function extractExports ( fileExport : any , exportNames : string [ ] ) : any | null {
60
- if ( ! fileExport ) {
61
- return null ;
62
- }
63
-
64
- if ( fileExport . default ) {
65
- for ( const exportName of exportNames ) {
66
- if ( fileExport . default [ exportName ] ) {
67
- return fileExport . default [ exportName ] ;
68
- }
69
- }
70
-
71
- return fileExport . default ;
72
- }
73
-
74
- for ( const exportName of exportNames ) {
75
- if ( fileExport [ exportName ] ) {
76
- return fileExport [ exportName ] ;
77
- }
78
- }
79
-
80
- return fileExport ;
81
- }
82
-
83
82
/**
84
83
* Additional options for loading files
85
84
*/
@@ -100,6 +99,8 @@ export interface LoadFilesOptions {
100
99
recursive ?: boolean ;
101
100
// Set to `true` to ignore files named `index.js` and `index.ts`
102
101
ignoreIndex ?: boolean ;
102
+ // Custom export extractor function
103
+ extractExports ?: ( fileExport : any ) => any ;
103
104
}
104
105
105
106
const LoadFilesDefaultOptions : LoadFilesOptions = {
@@ -134,6 +135,9 @@ export function loadFilesSync<T = any>(
134
135
options . globOptions
135
136
) ;
136
137
138
+ const extractExports = execOptions . extractExports || DEFAULT_EXTRACT_EXPORTS_FACTORY ( execOptions . exportNames ) ;
139
+ const requireMethod = execOptions . requireMethod || require ;
140
+
137
141
return relevantPaths
138
142
. map ( path => {
139
143
if ( ! checkExtension ( path , options ) ) {
@@ -147,33 +151,8 @@ export function loadFilesSync<T = any>(
147
151
const extension = extname ( path ) ;
148
152
149
153
if ( extension === formatExtension ( 'js' ) || extension === formatExtension ( 'ts' ) || execOptions . useRequire ) {
150
- const fileExports = ( execOptions . requireMethod ? execOptions . requireMethod : require ) ( path ) ;
151
- const extractedExport = extractExports ( fileExports , execOptions . exportNames ) ;
152
-
153
- if ( extractedExport . typeDefs && extractedExport . resolvers ) {
154
- return extractedExport ;
155
- }
156
-
157
- if ( extractedExport . schema ) {
158
- return extractedExport . schema ;
159
- }
160
-
161
- if ( extractedExport . typeDef ) {
162
- return extractedExport . typeDef ;
163
- }
164
-
165
- if ( extractedExport . typeDefs ) {
166
- return extractedExport . typeDefs ;
167
- }
168
-
169
- if ( extractedExport . resolver ) {
170
- return extractedExport . resolver ;
171
- }
172
-
173
- if ( extractedExport . resolvers ) {
174
- return extractedExport . resolvers ;
175
- }
176
-
154
+ const fileExports = requireMethod ( path ) ;
155
+ const extractedExport = extractExports ( fileExports ) ;
177
156
return extractedExport ;
178
157
} else {
179
158
return readFileSync ( path , { encoding : 'utf-8' } ) ;
@@ -232,7 +211,9 @@ export async function loadFiles(
232
211
options . globOptions
233
212
) ;
234
213
235
- const require$ = ( path : string ) => import ( path ) . catch ( async ( ) => require ( path ) ) ;
214
+ const extractExports = execOptions . extractExports || DEFAULT_EXTRACT_EXPORTS_FACTORY ( execOptions . exportNames ) ;
215
+ const defaultRequireMethod = ( path : string ) => import ( path ) . catch ( async ( ) => require ( path ) ) ;
216
+ const requireMethod = execOptions . requireMethod || defaultRequireMethod ;
236
217
237
218
return Promise . all (
238
219
relevantPaths
@@ -241,8 +222,8 @@ export async function loadFiles(
241
222
const extension = extname ( path ) ;
242
223
243
224
if ( extension === formatExtension ( 'js' ) || extension === formatExtension ( 'ts' ) || execOptions . useRequire ) {
244
- const fileExports = await ( execOptions . requireMethod ? execOptions . requireMethod : require$ ) ( path ) ;
245
- const extractedExport = extractExports ( fileExports , execOptions . exportNames ) ;
225
+ const fileExports = await requireMethod ( path ) ;
226
+ const extractedExport = extractExports ( fileExports ) ;
246
227
247
228
if ( extractedExport . resolver ) {
248
229
return extractedExport . resolver ;
0 commit comments