-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathpatchql.js
More file actions
79 lines (68 loc) · 1.96 KB
/
patchql.js
File metadata and controls
79 lines (68 loc) · 1.96 KB
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
const pull = require('pull-stream')
const nest = require('depnest')
const { ApolloClient } = require('apollo-client')
const { InMemoryCache } = require('apollo-cache-inmemory')
const { createHttpLink } = require('apollo-link-http')
const gql = require('graphql-tag').default
// NOTE also depends on graphql module
exports.needs = nest({
'sbot.async.run': 'first'
})
exports.gives = nest({
'app.sync.initialise': true,
'graphql.async.query': true
})
const mutation = gql`
mutation process($chunkSize: Int) {
process(chunkSize: $chunkSize) {
chunkSize,
latestSequence
}
}
`
exports.create = function (api) {
var graphql
return nest({
'app.sync.initialise': startPatchql,
'graphql.async.query': graphQuery
})
function startPatchql () {
if (process.env.PATCHQL === 'false') return
graphql = GraphqlClient()
var latestSequence
var nextSequence
api.sbot.async.run(server => {
server.jsbotPatchql.start({}, () => {
indexLoop()
})
})
function indexLoop () {
graphql.mutate({ mutation, variables: { chunkSize: 10e3 } })
.then(res => {
nextSequence = res.data.process.latestSequence
if (latestSequence === nextSequence) setTimeout(indexLoop, 5e3)
else {
latestSequence = nextSequence
console.log('patchql latestSequence:', latestSequence)
indexLoop()
}
})
.catch(err => {
console.error(err)
return setTimeout(indexLoop, 2e3)
})
}
}
function graphQuery ({ query, variables }, cb) {
if (!graphql) return setTimeout(() => graphQuery({ query, variables }, cb), 1e3)
graphql.query({ query, variables })
.then(res => cb(null, res))
.catch(err => cb(err))
}
function GraphqlClient () {
return new ApolloClient({
link: createHttpLink({ uri: 'http://localhost:8080/graphql' }), // set by jsbot-patchql
cache: new InMemoryCache()
})
}
}