Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
name: test
on:
push:
branches: [ main ]
pull_request:
push: { branches: [ main ] }

branches: [ main ]
workflow_dispatch:
jobs:
lint:
uses: graphqlswift/ci/.github/workflows/lint.yaml@main
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: fwal/setup-swift@v1
- uses: actions/checkout@v2
- name: Run tests
run: swift test
uses: graphqlswift/ci/.github/workflows/test.yaml@main
with:
include_android: false
39 changes: 6 additions & 33 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 7 additions & 10 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,29 @@ import PackageDescription

let package = Package(
name: "GraphQLTransportWS",
platforms: [.macOS(.v10_15)],
products: [
.library(
name: "GraphQLTransportWS",
targets: ["GraphQLTransportWS"]
),
],
dependencies: [
.package(name: "Graphiti", url: "https://github.com/GraphQLSwift/Graphiti.git", from: "1.0.0"),
.package(name: "GraphQL", url: "https://github.com/GraphQLSwift/GraphQL.git", from: "2.2.1"),
.package(name: "GraphQLRxSwift", url: "https://github.com/GraphQLSwift/GraphQLRxSwift.git", from: "0.0.4"),
.package(name: "RxSwift", url: "https://github.com/ReactiveX/RxSwift.git", from: "6.1.0"),
.package(name: "swift-nio", url: "https://github.com/apple/swift-nio.git", from: "2.33.0"),
.package(url: "https://github.com/GraphQLSwift/Graphiti.git", from: "3.0.0"),
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "4.0.0"),
],
targets: [
.target(
name: "GraphQLTransportWS",
dependencies: [
.product(name: "Graphiti", package: "Graphiti"),
.product(name: "GraphQLRxSwift", package: "GraphQLRxSwift"),
.product(name: "GraphQL", package: "GraphQL"),
.product(name: "NIO", package: "swift-nio"),
.product(name: "RxSwift", package: "RxSwift")
]),
]
),
.testTarget(
name: "GraphQLTransportWSTests",
dependencies: ["GraphQLTransportWS"]
),
]
],
swiftLanguageVersions: [.v5, .version("6")]
)
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,32 @@ import GraphQLTransportWS
/// Messenger wrapper for WebSockets
class WebSocketMessenger: Messenger {
private weak var websocket: WebSocket?
private var onReceive: (String) -> Void = { _ in }
private var onReceive: (String) async throws -> Void = { _ in }

init(websocket: WebSocket) {
self.websocket = websocket
websocket.onText { _, message in
self.onReceive(message)
try await self.onReceive(message)
}
}
func send<S>(_ message: S) where S: Collection, S.Element == Character {

func send<S>(_ message: S) where S: Collection, S.Element == Character async throws {
guard let websocket = websocket else { return }
websocket.send(message)
try await websocket.send(message)
}
func onReceive(callback: @escaping (String) -> Void) {

func onReceive(callback: @escaping (String) async throws -> Void) {
self.onReceive = callback
}
func error(_ message: String, code: Int) {

func error(_ message: String, code: Int) async throws {
guard let websocket = websocket else { return }
websocket.send("\(code): \(message)")
try await websocket.send("\(code): \(message)")
}
func close() {

func close() async throws {
guard let websocket = websocket else { return }
_ = websocket.close()
try await websocket.close()
}
}
```
Expand All @@ -67,7 +67,7 @@ routes.webSocket(
let server = GraphQLTransportWS.Server<EmptyInitPayload?>(
messenger: messenger,
onExecute: { graphQLRequest in
api.execute(
try await api.execute(
request: graphQLRequest.query,
context: context,
on: self.eventLoop,
Expand All @@ -76,7 +76,7 @@ routes.webSocket(
)
},
onSubscribe: { graphQLRequest in
api.subscribe(
try await api.subscribe(
request: graphQLRequest.query,
context: context,
on: self.eventLoop,
Expand Down Expand Up @@ -128,8 +128,8 @@ If the `payload` field is not required on your server, you may make Server's gen

## Memory Management

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