-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema.go
222 lines (206 loc) · 6.2 KB
/
schema.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"errors"
"fmt"
"github.com/elastic/go-elasticsearch/v7"
"github.com/graphql-go/graphql"
"log"
"strings"
"time"
)
func buildRootSchemaFromMappings(mappings map[string]interface{}, es *elasticsearch.Client) (graphql.Schema, error) {
fields := buildSchemaFromMappings(mappings, es)
if len(fields) == 0 {
return graphql.Schema{}, errors.New("no valid mappings provided, no schemas were built")
}
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields}
schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(rootQuery)}
return graphql.NewSchema(schemaConfig)
}
func buildSchemaFromMappings(mappings map[string]interface{}, es *elasticsearch.Client) graphql.Fields {
fields := graphql.Fields{}
for index, mapping := range mappings {
schemas := buildSchemasFromMapping(
index,
mapping.(map[string]interface{})["mappings"].(map[string]interface{}),
es,
)
for _, schema := range schemas {
schema := schema // pin https://github.com/golang/go/wiki/CommonMistakes#using-reference-to-loop-iterator-variable
fields[schema.Name] = &schema
}
}
return fields
}
func buildSchemasFromMapping(index string, mapping map[string]interface{}, es *elasticsearch.Client) []graphql.Field {
schemas := make([]graphql.Field, 0, 3)
properties, ok := mapping["properties"].(map[string]interface{})
if !ok {
log.Print("mapping for index ", index, " did not contain properties, ignoring")
return schemas
}
addName(index)
index = getGraphQLName(index)
start := time.Now()
documentType := buildDocumentTypeFromMapping(index, properties)
log.Print("built document type for ", index, " in ", time.Since(start))
start = time.Now()
booleanArgs := buildBooleanQueryTypes(index, properties)
log.Print("built boolean argument type for ", index, " in ", time.Since(start))
schemas = append(schemas, graphql.Field{
Name: index,
Type: graphql.NewList(documentType),
Args: graphql.FieldConfigArgument{
"size": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.Int),
},
"boolean_query": booleanArgs,
},
Resolve: NewDocumentListResolver(index, es),
})
schemas = append(schemas, graphql.Field{
Name: index + "_by_id",
Type: documentType,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.ID),
},
},
Resolve: NewDocumentResolver(index, es),
})
if Config.EnableAggregations {
start = time.Now()
documentAggregationType := buildDocumentAggregationTypeFromMapping(index, properties)
log.Print("built document aggregation type for ", index, " in ", time.Since(start))
schemas = append(schemas, graphql.Field{
Name: index + "_aggregations",
Type: documentAggregationType,
Args: graphql.FieldConfigArgument{
"boolean_query": booleanArgs,
},
Resolve: NewAggregationResolver(index, es),
})
}
return schemas
}
func buildDocumentTypeFromMapping(index string, mapping map[string]interface{}) *graphql.Object {
fields := getFields(mapping, nil)
fields["id"] = &graphql.Field{
Name: "id",
Type: graphql.ID,
Args: nil,
Resolve: nil,
}
return graphql.NewObject(graphql.ObjectConfig{
Name: index + "_document",
Fields: fields,
})
}
type AggregationType string
const (
AggregationTypeMin AggregationType = "min"
AggregationTypeMax AggregationType = "max"
AggregationTypeAvg AggregationType = "avg"
AggregationTypeSum AggregationType = "sum"
AggregationTypeCardinality AggregationType = "cardinality"
AggregationTypePercentiles AggregationType = "percentiles"
)
var aggregationTypes = []AggregationType{
AggregationTypeMin,
AggregationTypeMax,
AggregationTypeAvg,
AggregationTypeSum,
AggregationTypeCardinality,
AggregationTypePercentiles,
}
func buildDocumentAggregationTypeFromMapping(index string, mapping map[string]interface{}) *graphql.Object {
fields := make(graphql.Fields)
for _, aggregationType := range aggregationTypes {
subFields := getFields(mapping, &aggregationType)
name := index + "_" + string(aggregationType) + "_aggregation_document"
fields[string(aggregationType)] = &graphql.Field{
Name: name,
Type: graphql.NewObject(graphql.ObjectConfig{
Name: name,
Fields: subFields,
}),
//Args: nil,
//Resolve: NewAggregationResolver(index, es),
}
}
return graphql.NewObject(graphql.ObjectConfig{
Name: index + "_aggregation_document",
Fields: fields,
})
}
func buildKeyValueFloatType() graphql.Type {
fields := make(graphql.Fields)
fields["key"] = &graphql.Field{
Name: "key",
Type: graphql.Float,
}
fields["value"] = &graphql.Field{
Name: "value",
Type: graphql.Float,
}
return graphql.NewObject(graphql.ObjectConfig{
Name: "key_value",
Fields: fields,
})
}
var keyValueType = buildKeyValueFloatType()
func getFieldType(scalarType string, aggregationType *AggregationType) (graphql.Output, error) {
if aggregationType == nil {
return mapToGraphQLScalarType(scalarType)
}
switch *aggregationType {
case AggregationTypeCardinality:
return graphql.Int, nil
case AggregationTypePercentiles:
return graphql.NewList(keyValueType), nil
default:
return mapToGraphQLScalarType(scalarType)
}
}
func getFields(mapping map[string]interface{}, aggregationType *AggregationType) graphql.Fields {
fields := make(graphql.Fields)
for name, field := range mapping {
if Config.FieldIgnorePrefix != "" && strings.HasPrefix(name, Config.FieldIgnorePrefix) {
continue
}
addName(name)
graphQLName := getGraphQLName(name)
// TODO: don't ignore complex types
fieldInfo := field.(map[string]interface{})
fieldTypeString, ok := fieldInfo["type"].(string)
if !ok {
continue
}
fieldType, err := getFieldType(fieldTypeString, aggregationType)
if err == nil {
fields[graphQLName] = &graphql.Field{
Name: graphQLName,
Type: fieldType,
Args: nil,
Resolve: nil,
}
}
}
return fields
}
func mapToGraphQLScalarType(scalarType string) (*graphql.Scalar, error) {
switch scalarType {
case "float":
return graphql.Float, nil
case "long":
return graphql.Int, nil
case "string":
return graphql.String, nil
case "date":
return graphql.DateTime, nil
case "boolean":
return graphql.Boolean, nil
default:
return nil, fmt.Errorf("no scalar type %s", scalarType)
}
}