|  | 
|  | 1 | +/* @flow */ | 
|  | 2 | + | 
|  | 3 | +import { Resolver, TypeComposer, InputTypeComposer, type ResolveParams } from 'graphql-compose'; | 
|  | 4 | +import type { FieldsMapByElasticType } from '../mappingConverter'; | 
|  | 5 | +import ElasticApiParser from '../ElasticApiParser'; | 
|  | 6 | +import { getUpdateByIdOutputTC } from '../types/UpdateByIdOutput'; | 
|  | 7 | +import { getTypeName, getOrSetType, desc } from '../utils'; | 
|  | 8 | + | 
|  | 9 | +export type ElasticResolverOpts = { | 
|  | 10 | +  prefix?: ?string, | 
|  | 11 | +  elasticIndex: string, | 
|  | 12 | +  elasticType: string, | 
|  | 13 | +  elasticClient: Object, | 
|  | 14 | +}; | 
|  | 15 | + | 
|  | 16 | +export default function createUpdateByIdResolver( | 
|  | 17 | +  fieldMap: FieldsMapByElasticType, | 
|  | 18 | +  sourceTC: TypeComposer, | 
|  | 19 | +  opts: ElasticResolverOpts | 
|  | 20 | +): Resolver { | 
|  | 21 | +  if (!fieldMap || !fieldMap._all) { | 
|  | 22 | +    throw new Error( | 
|  | 23 | +      'First arg for Resolver updateById() should be fieldMap of FieldsMapByElasticType type.' | 
|  | 24 | +    ); | 
|  | 25 | +  } | 
|  | 26 | + | 
|  | 27 | +  if (!sourceTC || sourceTC.constructor.name !== 'TypeComposer') { | 
|  | 28 | +    throw new Error('Second arg for Resolver updateById() should be instance of TypeComposer.'); | 
|  | 29 | +  } | 
|  | 30 | + | 
|  | 31 | +  const prefix = opts.prefix || 'Es'; | 
|  | 32 | + | 
|  | 33 | +  const parser = new ElasticApiParser({ | 
|  | 34 | +    elasticClient: opts.elasticClient, | 
|  | 35 | +    prefix, | 
|  | 36 | +  }); | 
|  | 37 | + | 
|  | 38 | +  const updateByIdFC = parser.generateFieldConfig('update', { | 
|  | 39 | +    index: opts.elasticIndex, | 
|  | 40 | +    type: opts.elasticType, | 
|  | 41 | +    _source: true, | 
|  | 42 | +  }); | 
|  | 43 | + | 
|  | 44 | +  const argsConfigMap = { | 
|  | 45 | +    id: 'String!', | 
|  | 46 | +    record: getRecordITC(fieldMap).getTypeAsRequired(), | 
|  | 47 | +  }; | 
|  | 48 | + | 
|  | 49 | +  const type = getUpdateByIdOutputTC({ prefix, fieldMap, sourceTC }); | 
|  | 50 | + | 
|  | 51 | +  return new Resolver({ | 
|  | 52 | +    type, | 
|  | 53 | +    name: 'updateById', | 
|  | 54 | +    kind: 'mutation', | 
|  | 55 | +    args: argsConfigMap, | 
|  | 56 | +    resolve: async (rp: ResolveParams<*, *>) => { | 
|  | 57 | +      const { source, args, context, info } = rp; | 
|  | 58 | + | 
|  | 59 | +      args.body = { | 
|  | 60 | +        doc: { | 
|  | 61 | +          ...args.record, | 
|  | 62 | +        }, | 
|  | 63 | +      }; | 
|  | 64 | +      delete args.record; | 
|  | 65 | + | 
|  | 66 | +      const res = await updateByIdFC.resolve(source, args, context, info); | 
|  | 67 | + | 
|  | 68 | +      const { _index, _type, _id, _version, result, get } = res || {}; | 
|  | 69 | +      const { _source } = get || {}; | 
|  | 70 | + | 
|  | 71 | +      return { | 
|  | 72 | +        _id, | 
|  | 73 | +        _index, | 
|  | 74 | +        _type, | 
|  | 75 | +        _version, | 
|  | 76 | +        result, | 
|  | 77 | +        ..._source, | 
|  | 78 | +      }; | 
|  | 79 | +    }, | 
|  | 80 | +  }); | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | +export function getRecordITC(fieldMap: FieldsMapByElasticType): InputTypeComposer { | 
|  | 84 | +  const name = getTypeName('Record', {}); | 
|  | 85 | +  const description = desc(`The record from Elastic Search`); | 
|  | 86 | +  return getOrSetType(name, () => | 
|  | 87 | +    InputTypeComposer.create({ | 
|  | 88 | +      name, | 
|  | 89 | +      description, | 
|  | 90 | +      fields: { ...fieldMap._all }, | 
|  | 91 | +    }) | 
|  | 92 | +  ); | 
|  | 93 | +} | 
0 commit comments