-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGraphqlTableConfig.ts
469 lines (419 loc) · 15.4 KB
/
GraphqlTableConfig.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import { ApolloClient, InMemoryCache, gql, concat,HttpLink,ApolloLink } from '@apollo/client/core';
import { Request } from "express"
import TerminusClient from '@terminusdb/terminusdb-client'
import { AdvancedSearchField,Logger } from './typeDef'
const endpoint = process.env.SERVER_ENDPOINT || ""
const key = process.env.USER_KEY || ""
const user = process.env.USER_NAME || ""
type FieldObj = {kind:string,type:AdvancedKeyType,name:string}
type FieldElement = {fields?:{[key:string]:FieldObj},
type?:AdvancedKeyType,values?:string[],
order_by?:[],
[key: string]:object | string | undefined}
type ObjectMap = {[name:string]:FieldElement|undefined}
type TableFilterObj = { filter: { type: "boolean"|"number"|"string" }, options?:boolean }
type GraphQLSchemaType = {
name:string,
kind?:AdvancedKeyType,
fields?:GraphQLSchemaType[],
enumValues?:{name:string}[]
args?:[]
}
type FieldKeysItem = {
Header: string,
id: string,
accessor: string,
disableFilters?: boolean,
disableSortBy?: boolean,
filter?:object
}
type AdvancedKeyType = "String" | "BigInt" |"Int" | "BigFloat" |"Decimal"| "Float" |"Boolean"|"ENUM"|"OBJECT"|"DateTime"|"Date"
const advancedSearchMatchType : Record<AdvancedKeyType,AdvancedSearchField> = {
"String":{
"label":"replaceLabel",
"type":"text",
"valueSources":[
"value"
]
},
"BigInt":{
"label":"replaceLabel",
"type":"number",
"valueSources":[
"value"
]
},
"Int":{
"label":"replaceLabel",
"type":"number",
"valueSources":[
"value"
]
},
"BigFloat":{
"label":"replaceLabel",
"type":"number",
"valueSources":[
"value"
]
},
"Decimal":{
"label":"replaceLabel",
"type":"text",
"valueSources":[
"value"
],
"operators":[
"eq",
"ne",
"lt",
"le",
"gt",
"ge"
]
},
"Float":{
"label":"replaceLabel",
"type":"number",
"valueSources":[
"value"
]
},
"Boolean":{
"label":"replaceLabel",
"type":"boolean",
"defaultValue":true,
"fieldSettings":{
"labelYes":"true",
"labelNo":"false"
}
},
"ENUM":{
"label":"replaceLabel",
"valueSources":[
"value"
],
"operators":[
"select_equals",
"select_not_equals"
],
"defaultOperator":"select_equals",
"type":"select",
"fieldSettings":{
"listValues":[
]
}
},
"OBJECT":{
"label":"replaceLabel",
"type":"!group",
"subfields":{
}
},
"DateTime":{
"label":"replaceLabel",
"type":"datetime",
"valueSources":[
"value"
]
},
"Date":{
"label":"replaceLabel",
"type":"date",
"valueSources":[
"value"
]
}
}
const query = gql(`{__schema
{ types{...FullType}
directives{name description locations args{...InputValue}}}}
fragment FullType on __Type{kind name description fields(includeDeprecated:true){name description args{...InputValue}type{...TypeRef}isDeprecated deprecationReason}inputFields{...InputValue}interfaces{...TypeRef}enumValues(includeDeprecated:true){name description isDeprecated deprecationReason}possibleTypes{...TypeRef}}fragment InputValue on __InputValue{name description type{...TypeRef}defaultValue}fragment TypeRef on __Type{kind name ofType{kind name ofType{kind name ofType{kind name ofType{kind name ofType{kind name ofType{kind name ofType{kind name}}}}}}}}`)
// woqlClient.connectionConfig.branchBase("graphql")
class GraphqlTableConfig {
// constructor (req)
request : Request
user : string | undefined
password : string | undefined
orgName : string | undefined
dbName : string | undefined
logger : Logger
constructor(req : Request) {
this.request = req
this.password = req.context.password
this.user = req.context.user
// user that has admin privileges for the terminuscms team
this.orgName = req.params.org
this.dbName = req.params.db
this.logger = req.context.logger
// every dataproduct has the related change_request database
}
async getSchema (organization:string, dbname:string, type:string, typeid:string) {
const woqlClient = new TerminusClient.WOQLClient(endpoint, { user: this.user, key: this.password, organization:organization, db: dbname })
const userPassEnc = btoa(`${this.user}:${this.password}`)
if(type==="branch"){
woqlClient.checkout(typeid)
}else{
woqlClient.ref(typeid)
}
const terminusdbURL = woqlClient.connectionConfig.branchBase('graphql')
const httpLink = new HttpLink({ uri: terminusdbURL });
const authMiddleware = new ApolloLink((operation, forward) => {
// add the authorization to the headers
operation.setContext(({ headers = {} }) => ({
headers: {
...headers,
authorization: `Basic ${userPassEnc}`}
}));
return forward(operation);
})
const cache = new InMemoryCache({
});
const value = concat(authMiddleware, httpLink)
const client = new ApolloClient({
cache:cache,
link: value,
});
const result = await client.query({ query })
const schemaDoc = await woqlClient.getDocument({ graph_type: 'schema', as_list: true })
const tableConfig = this.formatSchema(result, schemaDoc)
return tableConfig
}
getOfType (ofType:any) : string {
if (ofType && ofType.name) return ofType.name
if (ofType.ofType) return this.getOfType(ofType.ofType)
return 'notype'
}
getType (field:any) {
if (field.type) {
// KIND WILL BE OBJECT OR SCALAR IN THIS CASE KIND IS NOT A LIST
if (field.type.name) return { type: field.type.name, kind: field.type.kind }
// KIND TYPE LIST
if (field.type.ofType) {
const className = this.getOfType(field.type.ofType)
return { type: className, kind: field.type.ofType.kind }
}
}
}
advSearchFilterFormatter (typesObj:any, fieldKey:string, fieldTypeValue:AdvancedKeyType, isList:boolean) {
try{
let advField:AdvancedSearchField = advancedSearchMatchType[fieldTypeValue] || {}
if (advField !== undefined) {
advField = JSON.parse(JSON.stringify(advField))
advField.label = fieldKey
} else if (typesObj[fieldTypeValue] && typesObj[fieldTypeValue].type === 'ENUM') {
advField = JSON.parse(JSON.stringify(advancedSearchMatchType.ENUM))
advField.label = fieldKey
advField.fieldSettings = {listValues : typesObj[fieldTypeValue].values}
}
if (isList)advField.mode = 'ARRAY'
advField.typevalue = fieldTypeValue
return advField
}catch(err:any){
this.logger.error("advSearchFilterFormatter",fieldKey,fieldTypeValue)
throw err
}
}
getFilterType (type:string, isList:boolean=false) {
const filterType:{[key:string]:TableFilterObj} = {
Boolean: { filter: { type: 'boolean' } },
Float: { filter: { type: 'number' } },
BigInt: { filter: { type: 'number' } },
Int: { filter: { type: 'number' } }
}
const filter:TableFilterObj = filterType[type] || { filter: { type: 'string' } }
if (isList) {
filter.options = isList
}
return filter
}
formatSchema(result:any, schemaDoc:any) {
const types: GraphQLSchemaType[] = result.data.__schema.types
const typesObj01:any = types[0]
const name = typesObj01.name
const typesObj:ObjectMap = {}
types.forEach(({name , kind, fields, enumValues}) => {
const itemMatch = schemaDoc.find((doc:any) => doc['@id'] === name ||
`${doc['@id']}_Filter` === name || `${doc['@id']}_Ordering` === name)
if (itemMatch) {
const ordering = itemMatch['@metadata'] && itemMatch['@metadata'].order_by ? { order_by: itemMatch['@metadata'].order_by } : {}
typesObj[name] = { type: kind, ...ordering }
// to be review
let fieldsObj:any = {}
if (fields) {
fields.forEach(field => {
if (field.name.startsWith('_') === false && field.args && field.args.length === 0) {
fieldsObj[field.name] = this.getType(field)
}
})
typesObj[name] = {...typesObj[name], fields : fieldsObj}
} else if (enumValues) {
const enumValuesTmp:string[] = []
enumValues.forEach(({name:enumValueName}) => {
enumValuesTmp.push(enumValueName)
})
typesObj[name]= {...typesObj[name], values : enumValuesTmp}
}
}
})
const objQuery:any= {}
const objQueryOpenAI:any = {}
const typesObjKeys = Object.keys(typesObj)
const advancedSearchObj :any={}//{[key:string]:{[key:string]:{}}|string}= {}
const tablesColumnsConfig:{[key:string]:{}|string} = {}
typesObjKeys.forEach(key => {
const itemObj = typesObj[key]
if (itemObj && itemObj.type === 'OBJECT') {
const fieldsKeys:string[] = itemObj.order_by || Object.keys(itemObj.fields || [])
const fieldKeysArr:FieldKeysItem[] = [{
Header: 'ID',
id: '_id',
accessor: '_id',
disableFilters: true,
disableSortBy: true
}]
fieldsKeys.forEach(fieldKey => {
// {type:Int|String|ClassName, kind:OBJECT|SCALAR|LIST}
const fieldObj = itemObj.fields ? itemObj.fields[fieldKey] : null
if (fieldObj) {
const fieldTypeValue = fieldObj.type
const isList = fieldObj.kind === 'LIST' ? { mode: 'ARRAY', typevalue: fieldTypeValue } : { typevalue: fieldTypeValue }
// check if it is a LIST of value
const fieldTypeValueObj = typesObj[fieldTypeValue] || null
if(fieldTypeValueObj){
if (fieldTypeValueObj.type === 'OBJECT') {
// const linkedDocument = typesObj[fieldTypeValue]
if (fieldTypeValueObj.fields && fieldTypeValueObj.fields.name) {
fieldKey = `${fieldKey}---name`
} else {
fieldKey = `${fieldKey}---id`
}
fieldKeysArr.push({
Header: fieldKey.toUpperCase(),
id: fieldKey,
accessor: fieldKey,
disableFilters: true,
disableSortBy: true,
filter: { type: 'string', options: isList }
})
} else if (typesObj[fieldTypeValue] && fieldTypeValueObj.type === 'ENUM') {
fieldKeysArr.push({
Header: fieldKey.toUpperCase(),
id: fieldKey,
accessor: fieldKey,
filter: { type: 'list', options: { dataprovider: fieldTypeValueObj.values, ...isList } }
})
}
}else {
const filter = this.getFilterType(fieldTypeValue)
const columnObj:FieldKeysItem= {
Header: fieldKey.toUpperCase(),
id: fieldKey,
accessor: fieldKey,
...filter
}
if (fieldTypeValue === 'DateTime') {
columnObj.disableFilters = true
columnObj.disableSortBy = true
}
fieldKeysArr.push(columnObj)
}
}
})// foreach
tablesColumnsConfig[key] = fieldKeysArr
//}
}
})
typesObjKeys.forEach(key => {
const itemObj = typesObj[key]
// const fieldsArr = []
if (itemObj && itemObj.type === 'OBJECT') {
const fieldsKeys = Object.keys(itemObj.fields || {})
const fieldKeysArr: string[] = []
const fieldKeysArrOpenAI: string[] = []
if (!advancedSearchObj[key]) {
advancedSearchObj[key] = {}
}
/* if (key === 'Area') {
const r = 1 + 45
} */
fieldsKeys.forEach(fieldKey => {
const fieldObj = itemObj.fields && itemObj.fields[fieldKey] ? itemObj.fields[fieldKey] : null
if (fieldObj) {
const fieldTypeValue = fieldObj.type
const isList = fieldObj.kind === 'LIST' ? true : false
const typesObjValue = typesObj[fieldTypeValue]
if (typesObjValue && typesObjValue.type === 'OBJECT') {
// const linkedDocument = typesObj[fieldTypeValue]
const name = typesObjValue.fields && typesObjValue.fields.name ? 'name' : ''
const subfields = typesObjValue.fields || {}
const subfieldsKeys = Object.keys(subfields || {})
fieldKeysArr.push(`${fieldKey}{
_id
${name}
}`)
const tmpSubfield:string[] = []
subfieldsKeys.forEach(sub => {
if (!typesObj[sub] || typesObjValue.type !== 'OBJECT') {
tmpSubfield.push(sub)
}
})
if (tmpSubfield.length === 0) {
tmpSubfield.push('_id')
}
fieldKeysArrOpenAI.push(`${fieldKey}{
${tmpSubfield.join(' \n ')}
}`)
// this could be a list too
const advField = JSON.parse(JSON.stringify(advancedSearchMatchType.OBJECT))
advField.label = fieldKey
advancedSearchObj[key][fieldKey] = advField
if (typesObj[fieldTypeValue]) {
subfieldsKeys.forEach(sub => {
const subTypeObj = subfields[sub]
if (subTypeObj) {
const subTypeValue = subTypeObj.type
advField.subfields[sub] = this.advSearchFilterFormatter(typesObj, sub, subTypeValue, isList)
}
})
}
// advField.subfields =advancedSearchObj[fieldTypeValue] ;
// }else{
// advancedSearchObj[fieldTypeValue]={}
// advField.subfields =advancedSearchObj[fieldTypeValue] ;
// }
} else {
// return primityve type
advancedSearchObj[key][fieldKey] = this.advSearchFilterFormatter(typesObj, fieldKey, fieldTypeValue, isList)
fieldKeysArr.push(fieldKey)
fieldKeysArrOpenAI.push(fieldKey)
}
}
})
const filter = typesObj[`${key}_Filter`] ? `, $filter:${key}_Filter` : ''
const filterVar = typesObj[`${key}_Filter`] ? ', filter:$filter' : ''
const order = typesObj[`${key}_Ordering`] ? `, $orderBy:${key}_Ordering` : ''
const orderVar = typesObj[`${key}_Ordering`] ? ', orderBy:$orderBy' : ''
objQueryOpenAI[key] = {
query: `
query($id: ID){
${key}(id: $id){
${fieldKeysArrOpenAI.join(' \n ')}
}
}`
}
objQuery[key] = {
query: `
query ${key}Query($offset: Int, $limit: Int ${filter} ${order} ){
${key}(include_children:false, offset: $offset, limit: $limit ${filterVar} ${orderVar}){
_id
${fieldKeysArr.join(' \n ')}
}
}`
}
}
})
return { objQueryOpenAI, objQuery, advancedSearchObj, tablesColumnsConfig }
}
}
export default GraphqlTableConfig