-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (70 loc) · 1.69 KB
/
index.js
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
import Executor from '../src/Executor.js'
import make_schema from '../src/make_schema.js'
import debug from 'debug'
import { readFileSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { pipeline } from 'stream'
const directory = dirname(fileURLToPath(import.meta.url))
const log = debug('batch').extend('example')
const interval = 500
let event = 0
const max_event = 3
const executor = new Executor({
context : () => ({}),
formatError: x => x,
schema : make_schema({
document : readFileSync(join(directory, 'schema.gql'), 'utf-8'),
resolvers: {
Query: {
ping() {
return 'pong chin chan'
},
},
Subscription: {
onEvent: {
async *subscribe() {
for (;;) {
await new Promise(resolve => setTimeout(resolve, interval))
yield { onEvent: ++event }
}
},
},
},
},
}),
subscription: {},
})
pipeline(
await executor.execute({
document: /* GraphQL */ `
query foo {
ping
peng: ping
}
query bar {
ping
}
subscription workerA {
onEvent
}
subscription workerB {
onEvent
}
`,
variables: {},
}),
async source => {
for await (const chunk of source) {
const { operation_type, operation_name, data, errors } = chunk
log('operation_type %O', operation_type)
log('operation_name %O', operation_name)
log('data %O', data)
log('errors %O\n============', errors)
if (event >= max_event) return
}
},
() => {
log('client stream terminated')
},
)