import { DataTypeFieldConfig , xLuceneTypeConfig } from '@terascope/types' ; import * as ts from '@terascope/utils' ; import { GraphQLType , TypeESMapping } from '../interfaces' ; import { formatGQLComment } from '../graphql-helper' ; export interface IBaseType { new ( field , config ) : BaseType ; } export type ToGraphQLOptions = { typeName ? : string ; isInput ? : boolean ; includePrivate ? : boolean ; useSnakeCase ? : boolean ; } ; export default abstract class BaseType { readonly field : string ; readonly config : DataTypeFieldConfig ; readonly version : number ; constructor ( field , config , version = 1 ) { this . version = version ; this . field = field ; this . config = config ; } abstract toESMapping ( version ? ) ; abstract toGraphQL ( options ? ) ; abstract toXlucene ( ) ; protected _formatGql ( type , customType ? ) { const desc = this . config . description ; if ( this . config . array ) { return { type : formatGQLType ( `${ this . field }: [${ type }]` , desc ) , customTypes : makeCustomTypes ( customType ) , } ; } return { type : formatGQLType ( `${ this . field }: ${ type }` , desc ) , customTypes : makeCustomTypes ( customType ) } ; } _formatGQLTypeName ( typeName , isInput ? , inputSuffix = 'Input' ) { return [ 'DT' , typeName , isInput ? inputSuffix : '' , `V${ this . version }` ] . join ( '' ) ; } } function makeCustomTypes ( customType ? ) { if ( ! customType ?. length ) return [ ] ; return ts . castArray ( customType ) ; } export function formatGQLType ( type , desc ? ) { if ( ! desc ) return type ; return `${ formatGQLComment ( desc ) }\n${ type }` ; }