-
Notifications
You must be signed in to change notification settings - Fork 8
/
GraphQLWs.swift
68 lines (52 loc) · 2.03 KB
/
GraphQLWs.swift
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
//
// GraphQLWs.swift
// Pioneer
//
// Created by d-exclaimation on 4:58 PM.
//
import Foundation
import Vapor
/// GraphQL Over Websocket Protocol namespace for `graphql-ws/graphql-transport-ws`
enum GraphQLWs: SubProtocol {
static let name: String = "graphql-transport-ws"
private static let Subscribe = "subscribe"
private static let Next = "next"
private static let Error = "error"
private static let Complete = "complete"
private static let Ping = "ping"
private static let Pong = "pong"
private static let ConnectionAck = "connection_ack"
private static let ConnectionInit = "connection_init"
static func decode<Resolver, Context>(_ data: Data) -> Pioneer<Resolver, Context>.Intent {
guard let msg = data.to(GraphQLMessage.self) else {
return .fatal(message: "Invalid operation message type")
}
switch (msg.type, msg.payload, msg.id) {
/// Start an operation whether it is `query`, `mutation`, and `subscriptions`
case (Subscribe, .some(let payload), .some(let oid)):
return parseOperation(oid: oid, payload: payload)
// Stop an operation using the OID
case (Complete, .none, .some(let oid)):
return .stop(oid: oid)
// Initialize handshake and confirm connection
case (ConnectionInit, _, .none):
return .initial
// A request to validate active connection
case (Ping, _, .none):
return .ping
// A callback from validation active connection
case (Pong, _, _):
return .ignore
default:
return .fatal(message: "Invalid operation message type")
}
}
static func initialize(ws: ProcessingConsumer) {
let ack = GraphQLMessage(type: ConnectionAck)
ws.send(msg: ack.jsonString)
}
static var next: String { Next }
static var complete: String { Complete }
static var error: String { Error }
static var keepAliveMessage: String { GraphQLMessage(type: ConnectionAck).jsonString }
}