Skip to content

Commit 77b4cee

Browse files
Merge pull request #7 from GraphQLSwift/feat/concurrency
Converts to Swift Concurrency
2 parents 5efdea7 + c470e17 commit 77b4cee

File tree

15 files changed

+542
-560
lines changed

15 files changed

+542
-560
lines changed

.github/workflows/test.yml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
name: test
22
on:
3+
push:
4+
branches: [ main ]
35
pull_request:
4-
push: { branches: [ main ] }
5-
6+
branches: [ main ]
7+
workflow_dispatch:
68
jobs:
9+
lint:
10+
uses: graphqlswift/ci/.github/workflows/lint.yaml@main
711
test:
8-
strategy:
9-
matrix:
10-
os: [ubuntu-latest, macos-latest]
11-
runs-on: ${{ matrix.os }}
12-
steps:
13-
- uses: fwal/setup-swift@v1
14-
- uses: actions/checkout@v2
15-
- name: Run tests
16-
run: swift test
12+
uses: graphqlswift/ci/.github/workflows/test.yaml@main
13+
with:
14+
include_android: false

Package.resolved

Lines changed: 30 additions & 58 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,29 @@ import PackageDescription
44

55
let package = Package(
66
name: "GraphQLTransportWS",
7+
platforms: [.macOS(.v10_15)],
78
products: [
89
.library(
910
name: "GraphQLTransportWS",
1011
targets: ["GraphQLTransportWS"]
1112
),
1213
],
1314
dependencies: [
14-
.package(name: "Graphiti", url: "https://github.com/GraphQLSwift/Graphiti.git", from: "1.0.0"),
15-
.package(name: "GraphQL", url: "https://github.com/GraphQLSwift/GraphQL.git", from: "2.2.1"),
16-
.package(name: "GraphQLRxSwift", url: "https://github.com/GraphQLSwift/GraphQLRxSwift.git", from: "0.0.4"),
17-
.package(name: "RxSwift", url: "https://github.com/ReactiveX/RxSwift.git", from: "6.1.0"),
18-
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio.git", from: "2.33.0"),
15+
.package(url: "https://github.com/GraphQLSwift/Graphiti.git", from: "3.0.0"),
16+
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "4.0.1"),
1917
],
2018
targets: [
2119
.target(
2220
name: "GraphQLTransportWS",
2321
dependencies: [
2422
.product(name: "Graphiti", package: "Graphiti"),
25-
.product(name: "GraphQLRxSwift", package: "GraphQLRxSwift"),
2623
.product(name: "GraphQL", package: "GraphQL"),
27-
.product(name: "NIO", package: "swift-nio"),
28-
.product(name: "RxSwift", package: "RxSwift")
29-
]),
24+
]
25+
),
3026
.testTarget(
3127
name: "GraphQLTransportWSTests",
3228
dependencies: ["GraphQLTransportWS"]
3329
),
34-
]
30+
],
31+
swiftLanguageVersions: [.v5, .version("6")]
3532
)

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,32 @@ import GraphQLTransportWS
2727
/// Messenger wrapper for WebSockets
2828
class WebSocketMessenger: Messenger {
2929
private weak var websocket: WebSocket?
30-
private var onReceive: (String) -> Void = { _ in }
31-
30+
private var onReceive: (String) async throws -> Void = { _ in }
31+
3232
init(websocket: WebSocket) {
3333
self.websocket = websocket
3434
websocket.onText { _, message in
35-
self.onReceive(message)
35+
try await self.onReceive(message)
3636
}
3737
}
38-
39-
func send<S>(_ message: S) where S: Collection, S.Element == Character {
38+
39+
func send<S>(_ message: S) where S: Collection, S.Element == Character async throws {
4040
guard let websocket = websocket else { return }
41-
websocket.send(message)
41+
try await websocket.send(message)
4242
}
43-
44-
func onReceive(callback: @escaping (String) -> Void) {
43+
44+
func onReceive(callback: @escaping (String) async throws -> Void) {
4545
self.onReceive = callback
4646
}
47-
48-
func error(_ message: String, code: Int) {
47+
48+
func error(_ message: String, code: Int) async throws {
4949
guard let websocket = websocket else { return }
50-
websocket.send("\(code): \(message)")
50+
try await websocket.send("\(code): \(message)")
5151
}
52-
53-
func close() {
52+
53+
func close() async throws {
5454
guard let websocket = websocket else { return }
55-
_ = websocket.close()
55+
try await websocket.close()
5656
}
5757
}
5858
```
@@ -67,7 +67,7 @@ routes.webSocket(
6767
let server = GraphQLTransportWS.Server<EmptyInitPayload?>(
6868
messenger: messenger,
6969
onExecute: { graphQLRequest in
70-
api.execute(
70+
try await api.execute(
7171
request: graphQLRequest.query,
7272
context: context,
7373
on: self.eventLoop,
@@ -76,7 +76,7 @@ routes.webSocket(
7676
)
7777
},
7878
onSubscribe: { graphQLRequest in
79-
api.subscribe(
79+
try await api.subscribe(
8080
request: graphQLRequest.query,
8181
context: context,
8282
on: self.eventLoop,
@@ -128,8 +128,8 @@ If the `payload` field is not required on your server, you may make Server's gen
128128

129129
## Memory Management
130130

131-
Memory ownership among the Server, Client, and Messenger may seem a little backwards. This is because the Swift/Vapor WebSocket
132-
implementation persists WebSocket objects long after their callback and they are expected to retain strong memory references to the
131+
Memory ownership among the Server, Client, and Messenger may seem a little backwards. This is because the Swift/Vapor WebSocket
132+
implementation persists WebSocket objects long after their callback and they are expected to retain strong memory references to the
133133
objects required for responses. In order to align cleanly and avoid memory cycles, Server and Client are injected strongly into Messenger
134134
callbacks, and only hold weak references to their Messenger. This means that Messenger objects (or their enclosing WebSocket) must
135135
be persisted to have the connected Server or Client objects function. That is, if a Server's Messenger falls out of scope and deinitializes,

0 commit comments

Comments
 (0)