-
Notifications
You must be signed in to change notification settings - Fork 13
/
group-type.ts
104 lines (82 loc) · 3.07 KB
/
group-type.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import {
xLuceneTypeConfig,
PropertyESTypeMapping,
PropertyESTypes,
ClientMetadata,
} from '@terascope/types';
import { firstToUpper } from '@terascope/utils';
import BaseType, { ToGraphQLOptions } from './base-type';
import { GraphQLType, TypeESMapping } from '../interfaces';
export type NestedTypes = { [field: string]: BaseType };
export default class GroupType extends BaseType {
readonly types: NestedTypes;
constructor(field: string, version: number, types: NestedTypes) {
super(field, types[field].config, version);
this.types = types;
}
toESMapping(config: ClientMetadata): TypeESMapping {
const {
mapping,
analyzer = {},
tokenizer = {},
} = this.types[this.field].toESMapping(config);
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(config);
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: ToGraphQLOptions = {}):GraphQLType {
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: string[] = [];
const customTypes: string[] = [];
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(): xLuceneTypeConfig {
const configs = Object.values(this.types).map((type) => type.toXlucene());
return Object.assign({}, ...configs);
}
private _removeBase(str: string) {
return str.replace(`${this.field}.`, '').trim();
}
}