11/* @flow */
22/* eslint-disable no-param-reassign */
33
4- import { Resolver , TypeComposer } from 'graphql-compose' ;
5- import type { ResolveParams } from 'graphql-compose/lib/definition' ;
4+ import { Resolver , TypeComposer , isObject } from 'graphql-compose' ;
5+ import type { ResolveParams , ProjectionType } from 'graphql-compose/lib/definition' ;
66import type { FieldsMapByElasticType } from '../mappingConverter' ;
77import ElasticApiParser from '../ElasticApiParser' ;
88import type { ElasticApiVersion } from '../ElasticApiParser' ;
99import {
1010 getSearchBodyITC ,
1111 prepareBodyInResolve ,
1212} from '../elasticDSL/SearchBody' ;
13+ import { getSearchOutputTC } from '../types/SearchOutput' ;
1314
1415export type ElasticSearchResolverOpts = {
1516 [ name : string ] : mixed ,
@@ -19,7 +20,7 @@ export type ElasticSearchResolverOpts = {
1920
2021export default function createSearchResolver (
2122 fieldMap : FieldsMapByElasticType ,
22- tc ?: TypeComposer ,
23+ sourceTC ?: TypeComposer ,
2324 elasticClient ?: mixed ,
2425 opts ?: ElasticSearchResolverOpts = { }
2526) : Resolver < * , * > {
@@ -35,10 +36,12 @@ export default function createSearchResolver(
3536 // );
3637 // }
3738
39+ const prefix = opts . prefix || 'Es' ;
40+
3841 const parser = new ElasticApiParser ( {
3942 elasticClient,
4043 version : opts . elasticApiVersion || '5_0' ,
41- prefix : opts . prefix || 'Es' ,
44+ prefix,
4245 } ) ;
4346 const searchFC = parser . generateFieldConfig ( 'search' , {
4447 index : 'cv' ,
@@ -48,26 +51,83 @@ export default function createSearchResolver(
4851 const args = Object . assign ( { } , searchFC . args , {
4952 body : {
5053 type : getSearchBodyITC ( {
51- prefix : opts . prefix ,
54+ prefix,
5255 fieldMap,
5356 } ) . getType ( ) ,
5457 } ,
5558 } ) ;
5659
60+ delete args . index ; // index can not be changed, it hardcoded in searchFC
61+ delete args . type ; // type can not be changed, it hardcoded in searchFC
62+ delete args . explain ; // added automatically if requested _shard, _node, _explanation
63+ delete args . version ; // added automatically if requested _version
64+ delete args . _source ; // added automatically due projection
65+ delete args . _sourceExclude ; // added automatically due projection
66+ delete args . _sourceInclude ; // added automatically due projection
67+
5768 // $FlowFixMe
5869 return new Resolver ( {
59- type : 'JSON' , // [tc],
70+ // $FlowFixMe
71+ type : sourceTC ? getSearchOutputTC ( { prefix, fieldMap, sourceTC } ) : 'JSON' ,
6072 name : 'search' ,
6173 kind : 'query' ,
6274 args,
6375 resolve : ( rp : ResolveParams < * , * > ) => {
6476 if ( rp . args && rp . args . body ) {
6577 rp . args . body = prepareBodyInResolve ( rp . args . body , fieldMap ) ;
6678 }
79+
80+ const { projection = { } } = rp ;
81+ const { hits = { } } = projection ;
82+ // $FlowFixMe
83+ const { hits : hitsHits } = hits ;
84+
85+ if ( typeof hitsHits === 'object' ) {
86+ // Turn on explain if in projection requested this fields:
87+ if ( hitsHits . _shard || hitsHits . _node || hitsHits . _explanation ) {
88+ // $FlowFixMe
89+ rp . args . body . explain = true ;
90+ }
91+
92+ if ( hitsHits . _version ) {
93+ // $FlowFixMe
94+ rp . args . body . version = true ;
95+ }
96+
97+ if ( ! hitsHits . _source ) {
98+ // $FlowFixMe
99+ rp . args . body . _source = false ;
100+ } else {
101+ // $FlowFixMe
102+ rp . args . body . _source = toDottedList ( hitsHits . _source ) ;
103+ }
104+ }
105+
67106 // $FlowFixMe
68107 const res = searchFC . resolve ( rp . source , rp . args , rp . context , rp . info ) ;
69- // console.log(res);
108+
70109 return res ;
71110 } ,
72111 } ) ;
73112}
113+
114+ export function toDottedList ( projection : ProjectionType , prev : string [ ] ) : string [ ] | boolean {
115+ let result = [ ] ;
116+ Object . keys ( projection ) . forEach ( k => {
117+ if ( isObject ( projection [ k ] ) ) {
118+ // $FlowFixMe
119+ const tmp = toDottedList ( projection [ k ] , prev ? [ ...prev , k ] : [ k ] ) ;
120+ if ( Array . isArray ( tmp ) ) {
121+ result = result . concat ( tmp ) ;
122+ return ;
123+ }
124+ }
125+
126+ if ( prev ) {
127+ result . push ( [ ...prev , k ] . join ( '.' ) ) ;
128+ } else {
129+ result . push ( k ) ;
130+ }
131+ } ) ;
132+ return result . length > 0 ? result : true ;
133+ }
0 commit comments