Skip to content

Commit dcb4e31

Browse files
chore: Formatting
1 parent 3f125f5 commit dcb4e31

6 files changed

Lines changed: 45 additions & 52 deletions

File tree

Sources/GraphQLTransportWS/Client.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public actor Client<InitPayload: Equatable & Codable> {
3838
/// - Parameter incoming: The server message sequence that the client should react to.
3939
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws -> Void where A.Element == String {
4040
for try await message in incoming {
41-
try await self.onMessage(message, self)
41+
try await onMessage(message, self)
4242

4343
// Detect and ignore error responses.
4444
if message.starts(with: "44") {
@@ -47,45 +47,45 @@ public actor Client<InitPayload: Equatable & Codable> {
4747
}
4848

4949
guard let json = message.data(using: .utf8) else {
50-
try await self.error(.invalidEncoding())
50+
try await error(.invalidEncoding())
5151
return
5252
}
5353

5454
let response: Response
5555
do {
56-
response = try self.decoder.decode(Response.self, from: json)
56+
response = try decoder.decode(Response.self, from: json)
5757
} catch {
5858
try await self.error(.noType())
5959
return
6060
}
6161

6262
switch response.type {
6363
case .connectionAck:
64-
guard let connectionAckResponse = try? self.decoder.decode(ConnectionAckResponse.self, from: json) else {
65-
try await self.error(.invalidResponseFormat(messageType: .connectionAck))
64+
guard let connectionAckResponse = try? decoder.decode(ConnectionAckResponse.self, from: json) else {
65+
try await error(.invalidResponseFormat(messageType: .connectionAck))
6666
return
6767
}
68-
try await self.onConnectionAck(connectionAckResponse, self)
68+
try await onConnectionAck(connectionAckResponse, self)
6969
case .next:
70-
guard let nextResponse = try? self.decoder.decode(NextResponse.self, from: json) else {
71-
try await self.error(.invalidResponseFormat(messageType: .next))
70+
guard let nextResponse = try? decoder.decode(NextResponse.self, from: json) else {
71+
try await error(.invalidResponseFormat(messageType: .next))
7272
return
7373
}
74-
try await self.onNext(nextResponse, self)
74+
try await onNext(nextResponse, self)
7575
case .error:
76-
guard let errorResponse = try? self.decoder.decode(ErrorResponse.self, from: json) else {
77-
try await self.error(.invalidResponseFormat(messageType: .error))
76+
guard let errorResponse = try? decoder.decode(ErrorResponse.self, from: json) else {
77+
try await error(.invalidResponseFormat(messageType: .error))
7878
return
7979
}
80-
try await self.onError(errorResponse, self)
80+
try await onError(errorResponse, self)
8181
case .complete:
82-
guard let completeResponse = try? self.decoder.decode(CompleteResponse.self, from: json) else {
83-
try await self.error(.invalidResponseFormat(messageType: .complete))
82+
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: json) else {
83+
try await error(.invalidResponseFormat(messageType: .complete))
8484
return
8585
}
86-
try await self.onComplete(completeResponse, self)
86+
try await onComplete(completeResponse, self)
8787
default:
88-
try await self.error(.invalidType())
88+
try await error(.invalidType())
8989
}
9090
}
9191
}

Sources/GraphQLTransportWS/GraphqlTransportWSError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct GraphQLTransportWSError: Error {
8989

9090
/// Error codes for miscellaneous issues
9191
public enum ErrorCode: Int, CustomStringConvertible, Sendable {
92-
// Miscellaneous
92+
/// Miscellaneous
9393
case miscellaneous = 4400
9494

9595
// Internal errors

Sources/GraphQLTransportWS/Messenger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Foundation
44
public protocol Messenger: Sendable {
55
/// Send a message through this messenger
66
/// - Parameter message: The message to send
7-
func send<S: Sendable>(_ message: S) async throws -> Void where S: Collection, S.Element == Character
7+
func send<S: Sendable & Collection>(_ message: S) async throws -> Void where S.Element == Character
88

99
/// Close the messenger
1010
func close() async throws

Sources/GraphQLTransportWS/Server.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public actor Server<
1212
SubscriptionSequenceType.Element == GraphQLResult
1313
{
1414
let messenger: Messenger
15-
15+
1616
let onInit: (InitPayload) async throws -> InitPayloadResult
1717
let onExecute: (GraphQLRequest, InitPayloadResult) async throws -> GraphQLResult
1818
let onSubscribe: (GraphQLRequest, InitPayloadResult) async throws -> SubscriptionSequenceType
19-
19+
2020
let onMessage: (String) async throws -> Void
2121
let onOperationComplete: (String) async throws -> Void
2222
let onOperationError: (String, [Error]) async throws -> Void
@@ -44,7 +44,7 @@ public actor Server<
4444
onSubscribe: @escaping (GraphQLRequest, InitPayloadResult) async throws -> SubscriptionSequenceType,
4545
onMessage: @escaping (String) async throws -> Void = { _ in },
4646
onOperationComplete: @escaping (String) async throws -> Void = { _ in },
47-
onOperationError: @escaping (String, [Error]) async throws -> Void = { _, _ in },
47+
onOperationError: @escaping (String, [Error]) async throws -> Void = { _, _ in }
4848
) {
4949
self.messenger = messenger
5050
self.onInit = onInit
@@ -59,7 +59,7 @@ public actor Server<
5959
/// - Parameter incoming: The client message sequence that the server should react to.
6060
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws -> Void where A.Element == String {
6161
for try await message in incoming {
62-
try await self.onMessage(message)
62+
try await onMessage(message)
6363

6464
// Detect and ignore error responses.
6565
if message.starts(with: "44") {
@@ -68,13 +68,13 @@ public actor Server<
6868
}
6969

7070
guard let json = message.data(using: .utf8) else {
71-
try await self.error(.invalidEncoding())
71+
try await error(.invalidEncoding())
7272
return
7373
}
7474

7575
let request: Request
7676
do {
77-
request = try self.decoder.decode(Request.self, from: json)
77+
request = try decoder.decode(Request.self, from: json)
7878
} catch {
7979
try await self.error(.noType())
8080
return
@@ -83,25 +83,25 @@ public actor Server<
8383
// handle incoming message
8484
switch request.type {
8585
case .connectionInit:
86-
guard let connectionInitRequest = try? self.decoder.decode(ConnectionInitRequest<InitPayload>.self, from: json) else {
87-
try await self.error(.invalidRequestFormat(messageType: .connectionInit))
86+
guard let connectionInitRequest = try? decoder.decode(ConnectionInitRequest<InitPayload>.self, from: json) else {
87+
try await error(.invalidRequestFormat(messageType: .connectionInit))
8888
return
8989
}
90-
try await self.onConnectionInit(connectionInitRequest, messenger)
90+
try await onConnectionInit(connectionInitRequest, messenger)
9191
case .subscribe:
92-
guard let subscribeRequest = try? self.decoder.decode(SubscribeRequest.self, from: json) else {
93-
try await self.error(.invalidRequestFormat(messageType: .subscribe))
92+
guard let subscribeRequest = try? decoder.decode(SubscribeRequest.self, from: json) else {
93+
try await error(.invalidRequestFormat(messageType: .subscribe))
9494
return
9595
}
96-
try await self.onSubscribe(subscribeRequest)
96+
try await onSubscribe(subscribeRequest)
9797
case .complete:
98-
guard let completeRequest = try? self.decoder.decode(CompleteRequest.self, from: json) else {
99-
try await self.error(.invalidRequestFormat(messageType: .complete))
98+
guard let completeRequest = try? decoder.decode(CompleteRequest.self, from: json) else {
99+
try await error(.invalidRequestFormat(messageType: .complete))
100100
return
101101
}
102-
try await self.onOperationComplete(completeRequest)
102+
try await onOperationComplete(completeRequest)
103103
default:
104-
try await self.error(.invalidType())
104+
try await error(.invalidType())
105105
}
106106
}
107107
}

Tests/GraphQLTransportWSTests/GraphQLTransportWSTests.swift

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import Foundation
2-
32
import GraphQL
4-
import Testing
5-
63
import GraphQLTransportWS
4+
import Testing
75

86
@Suite
97
struct GraphqlTransportWSTests {
@@ -24,11 +22,10 @@ struct GraphqlTransportWSTests {
2422
)
2523
},
2624
onSubscribe: { graphQLRequest, _ in
27-
let subscription = try await api.subscribe(
25+
try await api.subscribe(
2826
request: graphQLRequest.query,
2927
context: context
3028
).get()
31-
return subscription
3229
}
3330
)
3431
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>.makeStream()
@@ -51,7 +48,6 @@ struct GraphqlTransportWSTests {
5148
Task {
5249
try await client.listen(to: serverStream)
5350
}
54-
5551

5652
try await client.sendStart(
5753
payload: GraphQLRequest(
@@ -69,7 +65,7 @@ struct GraphqlTransportWSTests {
6965
}
7066
#expect(
7167
messages ==
72-
["\(ErrorCode.notInitialized): Connection not initialized"]
68+
["\(ErrorCode.notInitialized): Connection not initialized"]
7369
)
7470
}
7571

@@ -89,11 +85,10 @@ struct GraphqlTransportWSTests {
8985
)
9086
},
9187
onSubscribe: { graphQLRequest, _ in
92-
let subscription = try await api.subscribe(
88+
try await api.subscribe(
9389
request: graphQLRequest.query,
9490
context: context
9591
).get()
96-
return subscription
9792
}
9893
)
9994
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>.makeStream()
@@ -128,7 +123,7 @@ struct GraphqlTransportWSTests {
128123
}
129124
#expect(
130125
messages ==
131-
["\(ErrorCode.unauthorized): Unauthorized"]
126+
["\(ErrorCode.unauthorized): Unauthorized"]
132127
)
133128
}
134129

@@ -137,7 +132,7 @@ struct GraphqlTransportWSTests {
137132
let api = TestAPI()
138133
let context = TestContext()
139134
let id = UUID().description
140-
135+
141136
let server = Server<TokenInitPayload, Void, AsyncThrowingStream<GraphQLResult, Error>>(
142137
messenger: serverMessenger,
143138
onInit: { _ in },
@@ -148,11 +143,10 @@ struct GraphqlTransportWSTests {
148143
)
149144
},
150145
onSubscribe: { graphQLRequest, _ in
151-
let subscription = try await api.subscribe(
146+
try await api.subscribe(
152147
request: graphQLRequest.query,
153148
context: context
154149
).get()
155-
return subscription
156150
}
157151
)
158152
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>.makeStream()
@@ -200,7 +194,7 @@ struct GraphqlTransportWSTests {
200194
}
201195
#expect(
202196
messages.count ==
203-
3, // 1 connection_ack, 1 next, 1 complete
197+
3 // 1 connection_ack, 1 next, 1 complete
204198
)
205199
}
206200

@@ -211,7 +205,7 @@ struct GraphqlTransportWSTests {
211205
let id = UUID().description
212206
var dataIndex = 1
213207
let dataIndexMax = 3
214-
208+
215209
let (subscribeReadyStream, subscribeReadyContinuation) = AsyncStream<Void>.makeStream()
216210
let server = Server<TokenInitPayload, Void, AsyncThrowingStream<GraphQLResult, Error>>(
217211
messenger: serverMessenger,
@@ -288,7 +282,7 @@ struct GraphqlTransportWSTests {
288282
result.append(message)
289283
}
290284
#expect(
291-
messages.count == 5, // 1 connection_ack, 3 next, 1 complete
285+
messages.count == 5 // 1 connection_ack, 3 next, 1 complete
292286
)
293287
}
294288

Tests/GraphQLTransportWSTests/Utils/TestMessenger.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
import Foundation
3-
43
@testable import GraphQLTransportWS
54

65
/// Messenger for simple testing that doesn't require starting up a websocket server.
@@ -15,7 +14,7 @@ actor TestMessenger: Messenger {
1514
self.continuation = continuation
1615
}
1716

18-
func send<S: Sendable>(_ message: S) async throws where S: Collection, S.Element == Character {
17+
func send<S: Sendable & Collection>(_ message: S) async throws where S.Element == Character {
1918
continuation.yield(String(message))
2019
}
2120

0 commit comments

Comments
 (0)