|
1 | 1 | # GraphQLWS
|
2 | 2 |
|
3 | 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. |
| 4 | +It is mainly intended for server support, but there is a basic client implementation included. |
5 | 5 |
|
6 | 6 | Features:
|
7 | 7 | - Server implementation that implements defined protocol conversations
|
8 | 8 | - Client and Server types that wrap messengers
|
9 | 9 | - Codable Server and Client message structures
|
| 10 | +- Custom authentication support |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +To use this package, include it in your `Package.swift` dependencies: |
| 15 | + |
| 16 | +```swift |
| 17 | +.package(url: "git@gitlab.com:PassiveLogic/platform/GraphQLWS.git", from: "<version>"), |
| 18 | +``` |
| 19 | + |
| 20 | +Then create a class to implement the `Messenger` protocol. Here's an example using |
| 21 | +[`WebSocketKit`](https://github.com/vapor/websocket-kit): |
| 22 | + |
| 23 | +```swift |
| 24 | +import WebSocketKit |
| 25 | +import GraphQLWS |
| 26 | + |
| 27 | +/// Messenger wrapper for WebSockets |
| 28 | +class WebSocketMessenger: Messenger { |
| 29 | + private weak var websocket: WebSocket? |
| 30 | + private var onRecieve: (String) -> Void = { _ in } |
| 31 | + |
| 32 | + init(websocket: WebSocket) { |
| 33 | + self.websocket = websocket |
| 34 | + websocket.onText { _, message in |
| 35 | + self.onRecieve(message) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + func send<S>(_ message: S) where S: Collection, S.Element == Character { |
| 40 | + guard let websocket = websocket else { return } |
| 41 | + websocket.send(message) |
| 42 | + } |
| 43 | + |
| 44 | + func onRecieve(callback: @escaping (String) -> Void) { |
| 45 | + self.onRecieve = callback |
| 46 | + } |
| 47 | + |
| 48 | + func error(_ message: String, code: Int) { |
| 49 | + guard let websocket = websocket else { return } |
| 50 | + websocket.send("\(code): \(message)") |
| 51 | + } |
| 52 | + |
| 53 | + func close() { |
| 54 | + guard let websocket = websocket else { return } |
| 55 | + _ = websocket.close() |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +Next create a `Server`, provide the messenger you just defined, and wrap the API `execute` and `subscribe` commands: |
| 61 | + |
| 62 | +```swift |
| 63 | +routes.webSocket( |
| 64 | + "graphqlSubscribe", |
| 65 | + onUpgrade: { request, websocket in |
| 66 | + let messenger = WebSocketMessenger(websocket: websocket) |
| 67 | + let server = GraphQLWS.Server<EmptyInitPayload?>( |
| 68 | + messenger: messenger, |
| 69 | + onExecute: { graphQLRequest in |
| 70 | + api.execute( |
| 71 | + request: graphQLRequest.query, |
| 72 | + context: context, |
| 73 | + on: self.eventLoop, |
| 74 | + variables: graphQLRequest.variables, |
| 75 | + operationName: graphQLRequest.operationName |
| 76 | + ) |
| 77 | + }, |
| 78 | + onSubscribe: { graphQLRequest in |
| 79 | + api.subscribe( |
| 80 | + request: graphQLRequest.query, |
| 81 | + context: context, |
| 82 | + on: self.eventLoop, |
| 83 | + variables: graphQLRequest.variables, |
| 84 | + operationName: graphQLRequest.operationName |
| 85 | + ) |
| 86 | + } |
| 87 | + ) |
| 88 | + } |
| 89 | +) |
| 90 | +``` |
| 91 | + |
| 92 | +### Authentication |
| 93 | + |
| 94 | +This package exposes authentication hooks on the `connection_init` message. To perform custom authentication, |
| 95 | +provide a codable type to the Server init and define an `auth` callback on the server. For example: |
| 96 | + |
| 97 | +```swift |
| 98 | +struct UsernameAndPasswordInitPayload: Equatable & Codable { |
| 99 | + let username: String |
| 100 | + let password: String |
| 101 | +} |
| 102 | + |
| 103 | +let server = GraphQLWS.Server<UsernameAndPasswordInitPayload>( |
| 104 | + messenger: messenger, |
| 105 | + onExecute: { ... }, |
| 106 | + onSubscribe: { ... } |
| 107 | +) |
| 108 | +server.auth { payload in |
| 109 | + guard payload.username == "admin" else { |
| 110 | + throw Abort(.unauthorized) |
| 111 | + } |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +This example would require `connection_init` message from the client to look like this: |
| 116 | + |
| 117 | +```json |
| 118 | +{ |
| 119 | + "type": "connection_init", |
| 120 | + "payload": { |
| 121 | + "username": "admin", |
| 122 | + "password": "supersafe" |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +If the `payload` field is not required on your server, you may make Server's generic declaration optional like `Server<Payload?>` |
10 | 128 |
|
11 | 129 | ## Memory Management
|
12 | 130 |
|
13 |
| -Memory ownership among the Server, Client, and Messager may seem a little backwards. This is because the Swift/Vapor WebSocket |
| 131 | +Memory ownership among the Server, Client, and Messenger may seem a little backwards. This is because the Swift/Vapor WebSocket |
14 | 132 | implementation persists WebSocket objects long after their callback and they are expected to retain strong memory references to the
|
15 |
| -objects required for responses. In order to align cleanly and avoid memory cycles, Server and Client are injected strongly into Messager |
16 |
| -callbacks, and only hold weak references to their Messager. This means that Messager objects (or their enclosing WebSocket) must |
17 |
| -be persisted to have the connected Server or Client objects function. That is, if a Server's Messager falls out of scope and deinitializes, |
| 133 | +objects required for responses. In order to align cleanly and avoid memory cycles, Server and Client are injected strongly into Messenger |
| 134 | +callbacks, and only hold weak references to their Messenger. This means that Messenger objects (or their enclosing WebSocket) must |
| 135 | +be persisted to have the connected Server or Client objects function. That is, if a Server's Messenger falls out of scope and deinitializes, |
18 | 136 | the Server will no longer respond to messages.
|
0 commit comments