Skip to content

Commit 5419183

Browse files
Adds documentation and scoping
1 parent 18445ca commit 5419183

File tree

6 files changed

+48
-47
lines changed

6 files changed

+48
-47
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# GraphQLWS
22

3-
A description of this package.
3+
This implements the [graphql-ws WebSocket subprotocol](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md).
4+
It is mainly intended for Server support, but there is a basic client implementation included.
5+
6+
Features:
7+
- Server implementation that implements defined protocol conversations
8+
- Client and Server types that wrap messengers
9+
- Codable Server and Client message structures

Sources/GraphQLWS/Client.swift

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import Foundation
44
import GraphQL
55

6-
/// Adds client-side [graphql-ws protocol](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md)
7-
/// support, namely parsing and adding callbacks for each type of server respose.
8-
class Client {
6+
/// Client is an open-ended implementation of the client side of the protocol. It parses and adds callbacks for each type of server respose.
7+
public class Client {
98
let messenger: Messenger
109

1110
var onConnectionError: (ConnectionErrorResponse, Client) -> Void = { _, _ in }
@@ -23,7 +22,7 @@ class Client {
2322
///
2423
/// - Parameters:
2524
/// - messenger: The messenger to bind the client to.
26-
init(
25+
public init(
2726
messenger: Messenger
2827
) {
2928
self.messenger = messenger
@@ -107,48 +106,48 @@ class Client {
107106

108107
/// Define the callback run on receipt of a `connection_error` message
109108
/// - Parameter callback: The callback to assign
110-
func onConnectionError(_ callback: @escaping (ConnectionErrorResponse, Client) -> Void) {
109+
public func onConnectionError(_ callback: @escaping (ConnectionErrorResponse, Client) -> Void) {
111110
self.onConnectionError = callback
112111
}
113112

114113
/// Define the callback run on receipt of a `connection_ack` message
115114
/// - Parameter callback: The callback to assign
116-
func onConnectionAck(_ callback: @escaping (ConnectionAckResponse, Client) -> Void) {
115+
public func onConnectionAck(_ callback: @escaping (ConnectionAckResponse, Client) -> Void) {
117116
self.onConnectionAck = callback
118117
}
119118

120119
/// Define the callback run on receipt of a `connection_ka` message
121120
/// - Parameter callback: The callback to assign
122-
func onConnectionKeepAlive(_ callback: @escaping (ConnectionKeepAliveResponse, Client) -> Void) {
121+
public func onConnectionKeepAlive(_ callback: @escaping (ConnectionKeepAliveResponse, Client) -> Void) {
123122
self.onConnectionKeepAlive = callback
124123
}
125124

126125
/// Define the callback run on receipt of a `data` message
127126
/// - Parameter callback: The callback to assign
128-
func onData(_ callback: @escaping (DataResponse, Client) -> Void) {
127+
public func onData(_ callback: @escaping (DataResponse, Client) -> Void) {
129128
self.onData = callback
130129
}
131130

132131
/// Define the callback run on receipt of an `error` message
133132
/// - Parameter callback: The callback to assign
134-
func onError(_ callback: @escaping (ErrorResponse, Client) -> Void) {
133+
public func onError(_ callback: @escaping (ErrorResponse, Client) -> Void) {
135134
self.onError = callback
136135
}
137136

138137
/// Define the callback run on receipt of any message
139138
/// - Parameter callback: The callback to assign
140-
func onComplete(_ callback: @escaping (CompleteResponse, Client) -> Void) {
139+
public func onComplete(_ callback: @escaping (CompleteResponse, Client) -> Void) {
141140
self.onComplete = callback
142141
}
143142

144143
/// Define the callback run on receipt of a `complete` message
145144
/// - Parameter callback: The callback to assign
146-
func onMessage(_ callback: @escaping (String, Client) -> Void) {
145+
public func onMessage(_ callback: @escaping (String, Client) -> Void) {
147146
self.onMessage = callback
148147
}
149148

150149
/// Send a `connection_init` request through the messenger
151-
func sendConnectionInit(payload: ConnectionInitAuth?) {
150+
public func sendConnectionInit(payload: ConnectionInitAuth?) {
152151
messenger.send(
153152
ConnectionInitRequest(
154153
payload: payload
@@ -157,7 +156,7 @@ class Client {
157156
}
158157

159158
/// Send a `start` request through the messenger
160-
func sendStart(payload: GraphQLRequest, id: String) {
159+
public func sendStart(payload: GraphQLRequest, id: String) {
161160
messenger.send(
162161
StartRequest(
163162
payload: payload,
@@ -167,7 +166,7 @@ class Client {
167166
}
168167

169168
/// Send a `stop` request through the messenger
170-
func sendStop(id: String) {
169+
public func sendStop(id: String) {
171170
messenger.send(
172171
StopRequest(
173172
id: id
@@ -176,7 +175,7 @@ class Client {
176175
}
177176

178177
/// Send a `connection_terminate` request through the messenger
179-
func sendConnectionTerminate() {
178+
public func sendConnectionTerminate() {
180179
messenger.send(
181180
ConnectionTerminateRequest().toJSON(encoder)
182181
)

Sources/GraphQLWS/Messenger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
import NIO
55

66
/// Protocol for an object that can send and recieve messages
7-
protocol Messenger {
7+
public protocol Messenger {
88
func send<S>(_ message: S) -> Void where S: Collection, S.Element == Character
99
func onRecieve(callback: @escaping (String) -> Void) -> Void
1010
func close() -> Void

Sources/GraphQLWS/Requests.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import Foundation
44
import GraphQL
55

6-
/// Reference for graphql-ws protocol: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
7-
///
86
/// We also require that an 'authToken' field is provided in the 'payload' during the connection
97
/// init message. For example:
108
/// ```
@@ -22,14 +20,15 @@ struct Request: Equatable, JsonEncodable {
2220
}
2321

2422
/// A websocket `connection_init` request from the client to the server
25-
struct ConnectionInitRequest: Equatable, JsonEncodable {
23+
public struct ConnectionInitRequest: Equatable, JsonEncodable {
2624
var type = RequestMessageType.GQL_CONNECTION_INIT
27-
let payload: ConnectionInitAuth?
25+
public let payload: ConnectionInitAuth?
2826
}
2927

28+
// TODO: Make this structure user-defined
3029
/// Authorization format for a websocket `connection_init` request from the client to the server
31-
struct ConnectionInitAuth: Equatable, JsonEncodable {
32-
let authToken: String
30+
public struct ConnectionInitAuth: Equatable, JsonEncodable {
31+
public let authToken: String
3332
}
3433

3534
/// A websocket `start` request from the client to the server

Sources/GraphQLWS/Responses.swift

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
import Foundation
44
import GraphQL
55

6-
/// Reference for graphql-ws protocol: https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md
7-
86
/// A general response. This object's type is used to triage to other, more specific response objects.
9-
struct Response: Equatable, JsonEncodable {
7+
public struct Response: Equatable, JsonEncodable {
108
let type: ResponseMessageType
119
}
1210

1311
/// A websocket `connection_ack` response from the server to the client
14-
struct ConnectionAckResponse: Equatable, JsonEncodable {
12+
public struct ConnectionAckResponse: Equatable, JsonEncodable {
1513
let type: ResponseMessageType
16-
let payload: [String: Map]?
14+
public let payload: [String: Map]?
1715

1816
init(_ payload: [String: Map]? = nil) {
1917
self.type = .GQL_CONNECTION_ACK
@@ -22,9 +20,9 @@ struct ConnectionAckResponse: Equatable, JsonEncodable {
2220
}
2321

2422
/// A websocket `connection_error` response from the server to the client
25-
struct ConnectionErrorResponse: Equatable, JsonEncodable {
23+
public struct ConnectionErrorResponse: Equatable, JsonEncodable {
2624
let type: ResponseMessageType
27-
let payload: [String: Map]?
25+
public let payload: [String: Map]?
2826

2927
init(_ payload: [String: Map]? = nil) {
3028
self.type = .GQL_CONNECTION_ERROR
@@ -33,9 +31,9 @@ struct ConnectionErrorResponse: Equatable, JsonEncodable {
3331
}
3432

3533
/// A websocket `ka` response from the server to the client
36-
struct ConnectionKeepAliveResponse: Equatable, JsonEncodable {
34+
public struct ConnectionKeepAliveResponse: Equatable, JsonEncodable {
3735
let type: ResponseMessageType
38-
let payload: [String: Map]?
36+
public let payload: [String: Map]?
3937

4038
init(_ payload: [String: Map]? = nil) {
4139
self.type = .GQL_CONNECTION_KEEP_ALIVE
@@ -44,10 +42,10 @@ struct ConnectionKeepAliveResponse: Equatable, JsonEncodable {
4442
}
4543

4644
/// A websocket `data` response from the server to the client
47-
struct DataResponse: Equatable, JsonEncodable {
45+
public struct DataResponse: Equatable, JsonEncodable {
4846
let type: ResponseMessageType
49-
let payload: GraphQLResult?
50-
let id: String
47+
public let payload: GraphQLResult?
48+
public let id: String
5149

5250
init(_ payload: GraphQLResult? = nil, id: String) {
5351
self.type = .GQL_DATA
@@ -57,9 +55,9 @@ struct DataResponse: Equatable, JsonEncodable {
5755
}
5856

5957
/// A websocket `complete` response from the server to the client
60-
struct CompleteResponse: Equatable, JsonEncodable {
58+
public struct CompleteResponse: Equatable, JsonEncodable {
6159
let type: ResponseMessageType
62-
let id: String
60+
public let id: String
6361

6462
init(_: GraphQLResult? = nil, id: String) {
6563
self.type = .GQL_COMPLETE
@@ -68,10 +66,10 @@ struct CompleteResponse: Equatable, JsonEncodable {
6866
}
6967

7068
/// A websocket `error` response from the server to the client
71-
struct ErrorResponse: Equatable, JsonEncodable {
69+
public struct ErrorResponse: Equatable, JsonEncodable {
7270
let type: ResponseMessageType
73-
let payload: [GraphQLError]
74-
let id: String
71+
public let payload: [GraphQLError]
72+
public let id: String
7573

7674
init(_ errors: [Error], id: String) {
7775
let graphQLErrors = errors.map { error -> GraphQLError in

Sources/GraphQLWS/Server.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ import GraphQLRxSwift
66
import NIO
77
import RxSwift
88

9-
/// Adds server-side [graphql-ws subprotocol](https://github.com/apollographql/subscriptions-transport-ws/blob/master/PROTOCOL.md)
10-
/// support. This handles the majority of query processing according to the procol definition, allowing a few callbacks for customization.
11-
class Server {
9+
/// Server implements the server-side portion of the protocol, allowing a few callbacks for customization.
10+
public class Server {
1211
let messenger: Messenger
1312

1413
let onExecute: (GraphQLRequest) -> EventLoopFuture<GraphQLResult>
@@ -30,7 +29,7 @@ class Server {
3029
/// - messenger: The messenger to bind the server to.
3130
/// - onExecute: Callback run during `start` resolution for non-streaming queries. Typically this is `API.execute`.
3231
/// - onSubscribe: Callback run during `start` resolution for streaming queries. Typically this is `API.subscribe`.
33-
init(
32+
public init(
3433
messenger: Messenger,
3534
onExecute: @escaping (GraphQLRequest) -> EventLoopFuture<GraphQLResult>,
3635
onSubscribe: @escaping (GraphQLRequest) -> EventLoopFuture<SubscriptionResult>
@@ -111,19 +110,19 @@ class Server {
111110
/// Define the callback run during `connection_init` resolution that allows authorization using the `payload`.
112111
/// Throw to indicate that authorization has failed.
113112
/// - Parameter callback: The callback to assign
114-
func auth(_ callback: @escaping (ConnectionInitRequest) throws -> Void) {
113+
public func auth(_ callback: @escaping (ConnectionInitRequest) throws -> Void) {
115114
self.auth = callback
116115
}
117116

118117
/// Define the callback run when the communication is shut down, either by the client or server
119118
/// - Parameter callback: The callback to assign
120-
func onExit(_ callback: @escaping () -> Void) {
119+
public func onExit(_ callback: @escaping () -> Void) {
121120
self.onExit = callback
122121
}
123122

124123
/// Define the callback run on receipt of any message
125124
/// - Parameter callback: The callback to assign
126-
func onMessage(_ callback: @escaping (String) -> Void) {
125+
public func onMessage(_ callback: @escaping (String) -> Void) {
127126
self.onMessage = callback
128127
}
129128

0 commit comments

Comments
 (0)