-
Notifications
You must be signed in to change notification settings - Fork 1
/
GQLTrebuchetClient.ts
213 lines (188 loc) · 5.25 KB
/
GQLTrebuchetClient.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
import { Trebuchet } from '@mattkrick/trebuchet-client'
import { CacheConfig } from 'relay-runtime'
import { Sink } from 'relay-runtime/lib/network/RelayObservable'
interface TrebuchetCacheConfig extends CacheConfig {
metadata?: {
casualOrderingGroup?: string
[key: string]: unknown
}
}
export interface ErrorObj {
name: string
message: string
stack?: string
}
export interface OperationPayload {
documentId?: string
query?: string
variables?: {
[key: string]: any
}
uploadables?: {
[key: string]: File | Blob
}
cacheConfig?: TrebuchetCacheConfig
}
export interface GraphQLData {
[key: string]: any
}
export interface GraphQLResult {
data?: GraphQLData
errors?: Array<ErrorObj>
}
export interface Operation<T = any> {
isSubscription: boolean
id: string
payload: OperationPayload
sink: Sink<T>
}
export interface Operations {
[id: string]: Operation
}
export type OutgoingMessage = StartMessage | StopMessage
export interface StartMessage {
type: 'start'
id?: string
payload: OperationPayload
connectionId?: string
}
export interface StopMessage {
type: 'stop'
id: string
connectionId?: string
}
export interface IncomingDataMessage {
id: string
type: 'data'
payload: GraphQLResult
}
export interface IncomingErrorMessage {
id: string
type: 'error'
payload: {errors: Array<ErrorObj>}
}
export interface IncomingCompleteMessage {
id: string
type: 'complete'
payload: GraphQLResult
}
export type IncomingMessage = IncomingCompleteMessage | IncomingDataMessage | IncomingErrorMessage
class GQLTrebuchetClient {
operations: Operations = {}
private nextOperationId = 0
private pendingMessages = [] as IncomingMessage[]
constructor(public trebuchet: Trebuchet) {
trebuchet.on('data', (data) => {
this.dispatch(data as unknown as IncomingMessage)
})
trebuchet.on('reconnected', () => {
Object.keys(this.operations).forEach((opId) => {
const operation = this.operations[opId]
const {isSubscription, payload} = operation
// "at-most-once" message delivery guarantee for queries/mutations
if (!isSubscription) return
this.trebuchet.send({
id: opId,
type: 'start',
payload,
})
})
})
}
private dispatch(message: IncomingMessage) {
const {id: opId} = message
const operation = this.operations[opId]
if (!operation) return
const {sink, payload} = operation
const casualOrderingGroup = payload.cacheConfig?.metadata?.casualOrderingGroup ?? null
if (casualOrderingGroup) {
const expectedOpIdMismatch = Object.keys(this.operations).find(
(id) =>
id < opId &&
this.operations[id].payload.cacheConfig?.metadata?.casualOrderingGroup ===
casualOrderingGroup,
)
if (expectedOpIdMismatch) {
// the message dispatched is out of order. queue it until the correct one comes in
this.pendingMessages.push(message)
return
}
}
// the message received is the one that should be dispatched
switch (message.type) {
case 'complete':
delete this.operations[opId]
if (message.payload) {
sink.next(message.payload)
}
sink.complete()
break
case 'error':
delete this.operations[opId]
const {errors} = message.payload
const [firstError] = errors
sink.error(new Error(firstError.message))
break
case 'data':
sink.next(message.payload)
}
// attempt to flush the queue
if (casualOrderingGroup && this.pendingMessages.length > 0) {
const nextOpIdInGroup = Object.keys(this.operations).find(
(id) =>
this.operations[id].payload.cacheConfig?.metadata?.casualOrderingGroup ===
casualOrderingGroup,
)
const pendingMessageIdx = this.pendingMessages.findIndex(
(pendingMessage) => pendingMessage.id === nextOpIdInGroup,
)
if (pendingMessageIdx === -1) return
const [messageToFlush] = this.pendingMessages.splice(pendingMessageIdx, 1)
this.dispatch(messageToFlush)
}
}
private generateOperationId() {
return String(++this.nextOperationId)
}
private unsubscribe(opId: string) {
if (this.operations[opId]) {
delete this.operations[opId]
this.trebuchet.send({id: opId, type: 'stop'})
}
}
close(reason?: string) {
Object.keys(this.operations).forEach((opId) => {
this.unsubscribe(opId)
})
this.trebuchet.close(reason)
}
fetch(payload: OperationPayload, sink?: Sink<any> | null) {
if (sink) {
const opId = this.generateOperationId()
this.operations[opId] = {
isSubscription: false,
id: opId,
payload,
sink,
}
this.trebuchet.send({id: opId, type: 'start', payload})
} else {
this.trebuchet.send({type: 'start', payload})
}
}
subscribe(payload: OperationPayload, sink: Sink<any>) {
const opId = this.generateOperationId()
this.operations[opId] = {
isSubscription: true,
id: opId,
payload,
sink,
}
this.trebuchet.send({id: opId, type: 'start', payload})
const unsubscribe = () => {
this.unsubscribe(opId)
}
return {unsubscribe}
}
}
export default GQLTrebuchetClient