@@ -13,40 +13,17 @@ import {
13
13
GraphQLNonNull ,
14
14
} from 'graphql' ;
15
15
import { GraphQLJSON , upperFirst , TypeComposer } from 'graphql-compose' ;
16
+ import { reorderKeys } from './utils' ;
16
17
17
18
import type {
18
19
GraphQLArgumentConfig ,
19
20
GraphQLFieldConfig ,
21
+ GraphQLFieldConfigMap ,
20
22
GraphQLFieldConfigArgumentMap ,
21
23
GraphQLFieldMap ,
22
24
GraphQLInputType ,
23
25
} from 'graphql/type/definition' ; // eslint-disable-line
24
26
25
- export type ElasticApiVersion =
26
- | '5_0'
27
- | '5_x'
28
- | '2_4'
29
- | '2_3'
30
- | '2_2'
31
- | '2_1'
32
- | '2_0'
33
- | '1_7'
34
- | '1_6'
35
- | '1_5'
36
- | '1_4'
37
- | '1_3'
38
- | '1_2'
39
- | '1_1'
40
- | '1_0'
41
- | '0_90' ;
42
-
43
- export type ElasticApiParserOptsT = {
44
- elasticClient ?: any , // Elastic client
45
- version ?: ElasticApiVersion ,
46
- prefix ?: string ,
47
- elasticApiFilePath ?: string ,
48
- } ;
49
-
50
27
export type ElasticParamConfigT = {
51
28
type : string ,
52
29
name ?: string ,
@@ -84,22 +61,33 @@ export type ElasticParsedSourceT = {
84
61
} ,
85
62
} ;
86
63
64
+ export type ElasticApiParserOptsT = {
65
+ elasticClient ?: any , // Elastic client
66
+ apiVersion ?: string ,
67
+ prefix ?: string ,
68
+ elasticApiFilePath ?: string ,
69
+ } ;
70
+
87
71
export default class ElasticApiParser {
88
72
cachedEnums : {
89
73
[ fieldName : string ] : { [ valsStringified : string ] : GraphQLEnumType } ,
90
74
} ;
91
- version : string ;
75
+ apiVersion : string ;
92
76
prefix: string ;
93
77
elasticClient: any ;
94
78
parsedSource: ElasticParsedSourceT ;
95
79
96
80
constructor ( opts : ElasticApiParserOptsT = { } ) {
97
- // derived from installed package `elasticsearch`
98
- // from ../../node_modules/elasticsearch/src/lib/apis/VERSION.js
99
- this . version = opts . version || '5_0' ;
81
+ // avaliable varsions can be found in installed package `elasticsearch`
82
+ // in file /node_modules/elasticsearch/src/lib/apis/index.js
83
+ this . apiVersion = opts . apiVersion ||
84
+ ( opts . elasticClient &&
85
+ opts . elasticClient . config &&
86
+ opts . elasticClient . config . apiVersion ) ||
87
+ '_default' ;
100
88
const apiFilePath = path . resolve (
101
89
opts . elasticApiFilePath ||
102
- `./node_modules/elasticsearch/src/lib/apis/ ${ this . version } .js`
90
+ ElasticApiParser . findApiVersionFile ( this . apiVersion )
103
91
) ;
104
92
const source = ElasticApiParser . loadApiFile ( apiFilePath ) ;
105
93
this . parsedSource = ElasticApiParser . parseSource ( source ) ;
@@ -109,11 +97,48 @@ export default class ElasticApiParser {
109
97
this . cachedEnums = { } ;
110
98
}
111
99
100
+ static loadFile ( absolutePath : string ) : string {
101
+ return fs . readFileSync ( absolutePath , 'utf8' ) ;
102
+ }
103
+
112
104
static loadApiFile ( absolutePath : string ) : string {
113
- const code = fs . readFileSync ( absolutePath , 'utf8' ) ;
105
+ let code ;
106
+ try {
107
+ code = ElasticApiParser . loadFile ( absolutePath ) ;
108
+ } catch ( e ) {
109
+ throw new Error (
110
+ `Cannot load Elastic API source file from ${ absolutePath } `
111
+ ) ;
112
+ }
114
113
return ElasticApiParser . cleanUpSource ( code ) ;
115
114
}
116
115
116
+ static loadApiListFile ( absolutePath : string ) : string {
117
+ let code ;
118
+ try {
119
+ code = ElasticApiParser . loadFile ( absolutePath ) ;
120
+ } catch ( e ) {
121
+ throw new Error (
122
+ `Cannot load Elastic API file with avaliable versions from ${ absolutePath } `
123
+ ) ;
124
+ }
125
+ return code ;
126
+ }
127
+
128
+ static findApiVersionFile ( version : string ) : string {
129
+ const apiFolder = './node_modules/elasticsearch/src/lib/apis/' ;
130
+ const apiListFile = path . resolve ( apiFolder , 'index.js' ) ;
131
+ const apiListCode = ElasticApiParser . loadApiListFile ( apiListFile ) ;
132
+ const re = new RegExp ( `\\'${ version } \\':\\srequire\\(\\'(.+)\\'\\)` , 'gi' ) ;
133
+ const match = re . exec ( apiListCode ) ;
134
+ if ( match && match [ 1 ] ) {
135
+ return path . resolve ( apiFolder , `${ match [ 1 ] } .js` ) ;
136
+ }
137
+ throw new Error (
138
+ `Can not found Elastic version '${ version } ' in ${ apiListFile } `
139
+ ) ;
140
+ }
141
+
117
142
static cleanUpSource ( code : string ) : string {
118
143
// remove invalid markup
119
144
// {<<api-param-type-boolean,`Boolean`>>} converted to {Boolean}
@@ -229,12 +254,23 @@ export default class ElasticApiParser {
229
254
return result ;
230
255
}
231
256
232
- generateFieldMap ( ) : GraphQLFieldMap < * , * > {
257
+ generateFieldMap ( ) : GraphQLFieldConfigMap < * , * > {
233
258
const result = { } ;
234
259
Object . keys ( this . parsedSource ) . forEach ( methodName => {
235
260
result [ methodName ] = this . generateFieldConfig ( methodName ) ;
236
261
} ) ;
237
- return this . reassembleNestedFields ( result ) ;
262
+
263
+ const fieldMap = this . reassembleNestedFields ( result ) ;
264
+ return reorderKeys ( fieldMap , [
265
+ 'cat' ,
266
+ 'cluster' ,
267
+ 'indices' ,
268
+ 'ingest' ,
269
+ 'nodes' ,
270
+ 'snapshot' ,
271
+ 'tasks' ,
272
+ 'search' ,
273
+ ] ) ;
238
274
}
239
275
240
276
generateFieldConfig (
@@ -433,7 +469,7 @@ export default class ElasticApiParser {
433
469
if ( ! result [ names [ 0 ] ] ) {
434
470
result [ names [ 0 ] ] = {
435
471
type : new GraphQLObjectType ( {
436
- name : `${ this . prefix } Methods_ ${ upperFirst ( names [ 0 ] ) } ` ,
472
+ name : `${ this . prefix } _ ${ upperFirst ( names [ 0 ] ) } ` ,
437
473
// $FlowFixMe
438
474
fields : ( ) => { } ,
439
475
} ) ,
0 commit comments