@@ -13,7 +13,7 @@ import {
1313 GraphQLNonNull ,
1414} from 'graphql' ;
1515import { GraphQLJSON , upperFirst , TypeComposer } from 'graphql-compose' ;
16- import { getSearchBodyITC } from './ElasticDSL/SearchBody' ;
16+ import { getSearchBodyITC , prepareSearchArgs } from './ElasticDSL/SearchBody' ;
1717
1818import type {
1919 GraphQLArgumentConfig ,
@@ -24,7 +24,7 @@ import type {
2424
2525export type ElasticApiParserOptsT = {
2626 version ?:
27- '5_0'
27+ | '5_0'
2828 | '5_x'
2929 | '2_4'
3030 | '2_3'
@@ -81,15 +81,40 @@ export default class ElasticApiParser {
8181 // from ../../node_modules/elasticsearch/src/lib/apis/VERSION.js
8282 this . version = opts . version || '5_0' ;
8383 this . prefix = opts . prefix || 'Elastic' ;
84- this . elasticApiFilesPath = opts . elasticApiFilesPath || './node_modules/elasticsearch/src/lib/apis/' ;
84+ this . elasticApiFilesPath = opts . elasticApiFilesPath ||
85+ './node_modules/elasticsearch/src/lib/apis/' ;
8586 this . cachedEnums = { } ;
8687 }
8788
88- run ( ) {
89+ run ( ) : GraphQLFieldMap < * , * > {
8990 this . cachedEnums = { } ;
90- const apiFilePath = path . resolve ( this . elasticApiFilesPath , `${ this . version } .js` ) ;
91+ const apiFilePath = path . resolve (
92+ this . elasticApiFilesPath ,
93+ `${ this . version } .js`
94+ ) ;
9195 const source = this . loadApiFile ( apiFilePath ) ;
92- return this . parseSource ( source ) ;
96+ return this . addTypedSearch ( this . parseSource ( source ) ) ;
97+ }
98+
99+ addTypedSearch ( fields : GraphQLFieldMap < * , * > ) : GraphQLFieldMap < * , * > {
100+ if ( fields . search ) {
101+ const { type, description, args, resolve } = fields . search ;
102+ const searchTyped = {
103+ type,
104+ description,
105+ resolve : prepareSearchArgs ( resolve ) ,
106+ // $FlowFixMe
107+ args : Object . assign ( { } , args , {
108+ body : {
109+ type : getSearchBodyITC ( {
110+ prefix : this . prefix ,
111+ } ) . getType ( ) ,
112+ } ,
113+ } ) ,
114+ } ;
115+ return { searchTyped, ...fields } ;
116+ }
117+ return fields ;
93118 }
94119
95120 parseSource ( source : string ) : GraphQLFieldMap < * , * > {
@@ -126,12 +151,6 @@ export default class ElasticApiParser {
126151
127152 const elasticMethod = this . getMethodName ( item . ctx . string ) ;
128153
129- if ( elasticMethod === 'search' ) {
130- argMap . body . type = getSearchBodyITC ( {
131- prefix : this . prefix ,
132- } ) . getType ( ) ;
133- }
134-
135154 result [ item . ctx . string ] = {
136155 type : GraphQLJSON ,
137156 description,
@@ -232,7 +251,7 @@ export default class ElasticApiParser {
232251 } ;
233252 if ( paramCfg . default ) {
234253 result . defaultValue = paramCfg . default ;
235- } else if ( fieldName === 'format' ) {
254+ } else if ( fieldName === 'format' ) {
236255 result . defaultValue = 'json' ;
237256 }
238257
@@ -265,7 +284,9 @@ export default class ElasticApiParser {
265284 // eg '@param {anything} params.operationThreading - ?'
266285 return GraphQLJSON ;
267286 default :
268- console . log ( `New type '${ paramCfg . type } ' in elastic params setting for field ${ fieldName } .` ) ; // eslint-disable-line
287+ console . log (
288+ `New type '${ paramCfg . type } ' in elastic params setting for field ${ fieldName } .`
289+ ) ; // eslint-disable-line
269290 return GraphQLJSON ;
270291 }
271292 }
@@ -369,7 +390,9 @@ export default class ElasticApiParser {
369390 // $FlowFixMe
370391 fields : ( ) => { } ,
371392 } ) ,
372- resolve : ( ) => { return { } ; } ,
393+ resolve : ( ) => {
394+ return { } ;
395+ } ,
373396 } ;
374397 }
375398 TypeComposer . create ( result [ name [ 0 ] ] . type ) . setField ( name [ 1 ] , fields [ k ] ) ;
0 commit comments