import { xLuceneTypeConfig , PropertyESTypeMapping , PropertyESTypes , } from '@terascope/types' ; import { firstToUpper } from '@terascope/utils' ; import BaseType , { ToGraphQLOptions } from './base-type' ; import { GraphQLType , TypeESMapping } from '../interfaces' ; export type NestedTypes = { [ field ] : BaseType ; } ; export default class GroupType extends BaseType { readonly types : NestedTypes ; constructor ( field , version , types ) { super ( field , types [ field ] . config , version ) ; this . types = types ; } toESMapping ( version ? ) { const { mapping , analyzer = { } , tokenizer = { } , } = this . types [ this . field ] . toESMapping ( version ) ; const baseMapping = mapping [ this . field ] as PropertyESTypeMapping ; if ( ! baseMapping . properties ) { baseMapping . properties = { } ; } for ( const [ field , type ] of Object . entries ( this . types ) ) { if ( field === this . field ) { continue ; } const fieldResult = type . toESMapping ( version ) ; const nestedField = this . _removeBase ( field ) ; const fieldMapping = fieldResult . mapping [ field ] as PropertyESTypes ; baseMapping . properties [ nestedField ] = fieldMapping ; Object . assign ( tokenizer , fieldResult . tokenizer ) ; Object . assign ( analyzer , fieldResult . analyzer ) ; } return { mapping , analyzer , tokenizer , } ; } toGraphQL ( options = { } ) { const { typeName = 'Object' , isInput , includePrivate } = options ; const customTypeName = this . _formatGQLTypeName ( options . useSnakeCase ? `_${ typeName }_${ this . field }_` : `${ typeName }${ firstToUpper ( this . field ) }` , options . isInput , options . useSnakeCase ? 'input_' : undefined ) ; const properties = [ ] ; const customTypes = [ ] ; for ( const [ field , type ] of Object . entries ( this . types ) ) { if ( field === this . field ) { continue ; } if ( isInput && includePrivate && this . _removeBase ( field ) . startsWith ( '_' ) ) { continue ; } const result = type . toGraphQL ( options ) ; properties . push ( this . _removeBase ( result . type ) ) ; customTypes . push ( ... result . customTypes ) ; } const props = [ ... properties ] . sort ( ) ; const defType = isInput ? 'input' : 'type' ; customTypes . push ( `
            ${ defType } ${ customTypeName } {
                ${ props . join ( '\n' ) }
            }
        ` ) ; return this . _formatGql ( customTypeName , customTypes ) ; } toXlucene ( ) { const configs = Object . values ( this . types ) . map ( ( type ) => type . toXlucene ( ) ) ; return Object . assign ( { } , ... configs ) ; } private _removeBase ( str ) { return str . replace ( `${ this . field }.` , '' ) . trim ( ) ; } }