-
Notifications
You must be signed in to change notification settings - Fork 4
/
content-item-filter.ts
65 lines (56 loc) · 1.59 KB
/
content-item-filter.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
import fetch from 'sync-fetch'
import { makeAddPgTableConditionPlugin } from 'graphile-utils'
const ContentItemFilterPlugin = makeAddPgTableConditionPlugin(
'public',
'ContentItem',
'search',
(build) => ({
description:
'Filters the list to ContentItems that have a specific keyword.',
type: build.graphql.GraphQLString,
defaultValue: '',
}),
(value, helpers, build) => {
const { sql, sqlTableAlias } = helpers
var data = {
query: {
query_string: {
query: value,
},
},
fields: ['id'],
_source: false,
}
var url = 'http://localhost:9200/_search'
const response = fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
var json = response.json()
var values = []
for (let i = 0; i < json.hits.hits.length; i++) {
const element = json.hits.hits[i]
values.push(`('${element['_id']}',${element['_score']})`)
}
console.log(values)
var temp = `JOIN (VALUES ${values.join(
',',
)}) as x (id, ordering) on uid = x.id`
const customQueryBuilder = helpers.queryBuilder as any
customQueryBuilder['join'] = function (expr: any): void {
this.checkLock('join')
this.data.join.push(expr)
}
customQueryBuilder.join(sql.raw(temp))
helpers.queryBuilder.orderBy(sql.fragment`x.ordering`, false)
return sql.raw(
`uid IN (${json.hits.hits
.map((entry: any) => `'${entry['_id']}'`)
.join(',')})`,
)
},
)
export default ContentItemFilterPlugin