/
schema.js
106 lines (89 loc) · 2.24 KB
/
schema.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
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
import {makeExecutableSchema, addMockFunctionsToSchema} from 'graphql-tools'
import OptionsSchema from './optionsSchema'
import ReadySchema from './readySchema'
import mocks from './mocks'
import MoveScalar from './moveScalar'
import {PubSub, withFilter} from 'graphql-subscriptions';
import InfoGenerator from './InfoGenerator'
const pubsub = new PubSub();
const ChessQSchema = [`
type Query {
createEngine: EngineResponse
uci(engineId: String!): UciResponse!
register(engineId: String!, name: String, code: String): String
registerLater(engineId: String!): String
setSpinOption(engineId: String!, name: String!, value: Int!): String!
setButtonOption(engineId: String!, name: String!): String!
setCheckOption(engineId: String!, name: String!, value: Boolean!): String!
setComboOption(engineId: String!, name: String!, value: String!): String!
quit(engineId: String!): String!
isready(engineId: String!): String!
}
type Mutation {
go: BestMove!
}
type Score {
cp: Int!
depth: Int!
nodes: Int!
time: Int!
pv: [Move!]!
}
type Depth {
depth: Int!
seldepth: Int!
nodes: Int
}
type Nps {
value: Int!
}
type BestMove {
value: Move!,
ponder: Move
}
union Info = Score | Depth | Nps | BestMove
type Subscription {
info: Info
}
`]
const schema = [
...ChessQSchema,
...OptionsSchema,
...ReadySchema
]
const TOPIC = 'info'
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms)
})
}
const options = {
typeDefs: schema,
resolvers: {
Query: {
isready: () => "readyok", // TODO: mocked
},
Move: MoveScalar,
Mutation: {
go: async () => {
let info;
for (info of InfoGenerator()) {
console.log(info.__typename)
pubsub.publish(TOPIC, {info})
await sleep(3000)
}
return info;
}
},
Subscription: {
info: {
subscribe: withFilter(() => pubsub.asyncIterator(TOPIC), (payload, variables) => {
return true
})
}
}
}
}
const executableSchema = makeExecutableSchema(options);
addMockFunctionsToSchema({schema: executableSchema, mocks, preserveResolvers: true})
export default executableSchema;