Skip to content

Commit 93e8e79

Browse files
Merge pull request #9 from GraphQLSwift/refactor/swift-format
Converts to swift-format
2 parents 5a3b62e + 8f8c77a commit 93e8e79

10 files changed

Lines changed: 166 additions & 85 deletions

File tree

.swift-format

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"version": 1,
3+
"indentation" : {
4+
"spaces" : 4
5+
},
6+
"lineBreakBeforeEachArgument": true
7+
}

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let package = Package(
99
.library(
1010
name: "GraphQLWS",
1111
targets: ["GraphQLWS"]
12-
),
12+
)
1313
],
1414
dependencies: [
1515
.package(url: "https://github.com/GraphQLSwift/Graphiti.git", from: "3.0.0"),

Sources/GraphQLWS/Client.swift

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ public actor Client<InitPayload: Equatable & Codable> {
2727
/// - onComplete: The callback run on receipt of a `complete` message
2828
public init(
2929
messenger: Messenger,
30-
onConnectionError: @escaping (ConnectionErrorResponse, Client) async throws -> Void = { _, _ in },
31-
onConnectionAck: @escaping (ConnectionAckResponse, Client) async throws -> Void = { _, _ in },
32-
onConnectionKeepAlive: @escaping (ConnectionKeepAliveResponse, Client) async throws -> Void = { _, _ in },
30+
onConnectionError: @escaping (ConnectionErrorResponse, Client) async throws -> Void = {
31+
_,
32+
_ in
33+
},
34+
onConnectionAck: @escaping (ConnectionAckResponse, Client) async throws -> Void = { _, _ in
35+
},
36+
onConnectionKeepAlive:
37+
@escaping (ConnectionKeepAliveResponse, Client) async throws -> Void = { _, _ in },
3338
onData: @escaping (DataResponse, Client) async throws -> Void = { _, _ in },
3439
onError: @escaping (ErrorResponse, Client) async throws -> Void = { _, _ in },
3540
onComplete: @escaping (CompleteResponse, Client) async throws -> Void = { _, _ in }
@@ -45,7 +50,8 @@ public actor Client<InitPayload: Equatable & Codable> {
4550

4651
/// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
4752
/// - Parameter incoming: The server message sequence that the client should react to.
48-
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws -> Void where A.Element == String {
53+
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws
54+
where A.Element == String {
4955
for try await message in incoming {
5056
// Detect and ignore error responses.
5157
if message.starts(with: "44") {
@@ -68,19 +74,34 @@ public actor Client<InitPayload: Equatable & Codable> {
6874

6975
switch response.type {
7076
case .GQL_CONNECTION_ERROR:
71-
guard let connectionErrorResponse = try? decoder.decode(ConnectionErrorResponse.self, from: json) else {
77+
guard
78+
let connectionErrorResponse = try? decoder.decode(
79+
ConnectionErrorResponse.self,
80+
from: json
81+
)
82+
else {
7283
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR))
7384
return
7485
}
7586
try await onConnectionError(connectionErrorResponse, self)
7687
case .GQL_CONNECTION_ACK:
77-
guard let connectionAckResponse = try? decoder.decode(ConnectionAckResponse.self, from: json) else {
88+
guard
89+
let connectionAckResponse = try? decoder.decode(
90+
ConnectionAckResponse.self,
91+
from: json
92+
)
93+
else {
7894
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_ERROR))
7995
return
8096
}
8197
try await onConnectionAck(connectionAckResponse, self)
8298
case .GQL_CONNECTION_KEEP_ALIVE:
83-
guard let connectionKeepAliveResponse = try? decoder.decode(ConnectionKeepAliveResponse.self, from: json) else {
99+
guard
100+
let connectionKeepAliveResponse = try? decoder.decode(
101+
ConnectionKeepAliveResponse.self,
102+
from: json
103+
)
104+
else {
84105
try await error(.invalidResponseFormat(messageType: .GQL_CONNECTION_KEEP_ALIVE))
85106
return
86107
}
@@ -98,7 +119,8 @@ public actor Client<InitPayload: Equatable & Codable> {
98119
}
99120
try await onError(errorResponse, self)
100121
case .GQL_COMPLETE:
101-
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: json) else {
122+
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: json)
123+
else {
102124
try await error(.invalidResponseFormat(messageType: .GQL_COMPLETE))
103125
return
104126
}

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
public protocol Messenger: Sendable {
55
/// Send a message through this messenger
66
/// - Parameter message: The message to send
7-
func send<S: Sendable & Collection>(_ message: S) async throws -> Void where S.Element == Character
7+
func send<S: Sendable & Collection>(_ message: S) async throws where S.Element == Character
88

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

Sources/GraphQLWS/Requests.swift

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ public struct ConnectionInitRequest<InitPayload: Codable & Equatable>: Equatable
1818
public init(from decoder: any Decoder) throws {
1919
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
2020
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_CONNECTION_INIT {
21-
throw DecodingError.dataCorrupted(.init(
22-
codingPath: decoder.codingPath,
23-
debugDescription: "type must be `\(RequestMessageType.GQL_CONNECTION_INIT.type)`"
24-
))
21+
throw DecodingError.dataCorrupted(
22+
.init(
23+
codingPath: decoder.codingPath,
24+
debugDescription:
25+
"type must be `\(RequestMessageType.GQL_CONNECTION_INIT.type)`"
26+
)
27+
)
2528
}
2629
payload = try container.decode(InitPayload.self, forKey: .payload)
2730
}
@@ -41,10 +44,12 @@ public struct StartRequest: Equatable, JsonEncodable {
4144
public init(from decoder: any Decoder) throws {
4245
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
4346
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_START {
44-
throw DecodingError.dataCorrupted(.init(
45-
codingPath: decoder.codingPath,
46-
debugDescription: "type must be `\(RequestMessageType.GQL_START.type)`"
47-
))
47+
throw DecodingError.dataCorrupted(
48+
.init(
49+
codingPath: decoder.codingPath,
50+
debugDescription: "type must be `\(RequestMessageType.GQL_START.type)`"
51+
)
52+
)
4853
}
4954
payload = try container.decode(GraphQLRequest.self, forKey: .payload)
5055
id = try container.decode(String.self, forKey: .id)
@@ -62,11 +67,14 @@ public struct StopRequest: Equatable, JsonEncodable {
6267

6368
public init(from decoder: any Decoder) throws {
6469
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
65-
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_CONNECTION_TERMINATE {
66-
throw DecodingError.dataCorrupted(.init(
67-
codingPath: decoder.codingPath,
68-
debugDescription: "type must be `\(RequestMessageType.GQL_STOP.type)`"
69-
))
70+
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_CONNECTION_TERMINATE
71+
{
72+
throw DecodingError.dataCorrupted(
73+
.init(
74+
codingPath: decoder.codingPath,
75+
debugDescription: "type must be `\(RequestMessageType.GQL_STOP.type)`"
76+
)
77+
)
7078
}
7179
id = try container.decode(String.self, forKey: .id)
7280
}
@@ -80,11 +88,15 @@ public struct ConnectionTerminateRequest: Equatable, JsonEncodable {
8088

8189
public init(from decoder: any Decoder) throws {
8290
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
83-
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_CONNECTION_TERMINATE {
84-
throw DecodingError.dataCorrupted(.init(
85-
codingPath: decoder.codingPath,
86-
debugDescription: "type must be `\(RequestMessageType.GQL_CONNECTION_TERMINATE.type)`"
87-
))
91+
if try container.decode(RequestMessageType.self, forKey: .type) != .GQL_CONNECTION_TERMINATE
92+
{
93+
throw DecodingError.dataCorrupted(
94+
.init(
95+
codingPath: decoder.codingPath,
96+
debugDescription:
97+
"type must be `\(RequestMessageType.GQL_CONNECTION_TERMINATE.type)`"
98+
)
99+
)
88100
}
89101
}
90102
}

Sources/GraphQLWS/Responses.swift

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ public struct ConnectionAckResponse: Equatable, JsonEncodable {
1818
public init(from decoder: any Decoder) throws {
1919
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
2020
if try container.decode(ResponseMessageType.self, forKey: .type) != .GQL_CONNECTION_ACK {
21-
throw DecodingError.dataCorrupted(.init(
22-
codingPath: decoder.codingPath,
23-
debugDescription: "type must be `\(ResponseMessageType.GQL_CONNECTION_ACK.type)`"
24-
))
21+
throw DecodingError.dataCorrupted(
22+
.init(
23+
codingPath: decoder.codingPath,
24+
debugDescription:
25+
"type must be `\(ResponseMessageType.GQL_CONNECTION_ACK.type)`"
26+
)
27+
)
2528
}
2629
payload = try container.decodeIfPresent([String: Map].self, forKey: .payload)
2730
}
@@ -39,10 +42,13 @@ public struct ConnectionErrorResponse: Equatable, JsonEncodable {
3942
public init(from decoder: any Decoder) throws {
4043
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
4144
if try container.decode(ResponseMessageType.self, forKey: .type) != .GQL_CONNECTION_ERROR {
42-
throw DecodingError.dataCorrupted(.init(
43-
codingPath: decoder.codingPath,
44-
debugDescription: "type must be `\(ResponseMessageType.GQL_CONNECTION_ERROR.type)`"
45-
))
45+
throw DecodingError.dataCorrupted(
46+
.init(
47+
codingPath: decoder.codingPath,
48+
debugDescription:
49+
"type must be `\(ResponseMessageType.GQL_CONNECTION_ERROR.type)`"
50+
)
51+
)
4652
}
4753
payload = try container.decodeIfPresent([String: Map].self, forKey: .payload)
4854
}
@@ -59,11 +65,16 @@ public struct ConnectionKeepAliveResponse: Equatable, JsonEncodable {
5965

6066
public init(from decoder: any Decoder) throws {
6167
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
62-
if try container.decode(ResponseMessageType.self, forKey: .type) != .GQL_CONNECTION_KEEP_ALIVE {
63-
throw DecodingError.dataCorrupted(.init(
64-
codingPath: decoder.codingPath,
65-
debugDescription: "type must be `\(ResponseMessageType.GQL_CONNECTION_KEEP_ALIVE.type)`"
66-
))
68+
if try container.decode(ResponseMessageType.self, forKey: .type)
69+
!= .GQL_CONNECTION_KEEP_ALIVE
70+
{
71+
throw DecodingError.dataCorrupted(
72+
.init(
73+
codingPath: decoder.codingPath,
74+
debugDescription:
75+
"type must be `\(ResponseMessageType.GQL_CONNECTION_KEEP_ALIVE.type)`"
76+
)
77+
)
6778
}
6879
payload = try container.decodeIfPresent([String: Map].self, forKey: .payload)
6980
}
@@ -83,10 +94,12 @@ public struct DataResponse: Equatable, JsonEncodable {
8394
public init(from decoder: any Decoder) throws {
8495
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
8596
if try container.decode(ResponseMessageType.self, forKey: .type) != .GQL_DATA {
86-
throw DecodingError.dataCorrupted(.init(
87-
codingPath: decoder.codingPath,
88-
debugDescription: "type must be `\(ResponseMessageType.GQL_DATA.type)`"
89-
))
97+
throw DecodingError.dataCorrupted(
98+
.init(
99+
codingPath: decoder.codingPath,
100+
debugDescription: "type must be `\(ResponseMessageType.GQL_DATA.type)`"
101+
)
102+
)
90103
}
91104
payload = try container.decodeIfPresent(GraphQLResult.self, forKey: .payload)
92105
id = try container.decode(String.self, forKey: .id)
@@ -105,10 +118,12 @@ public struct CompleteResponse: Equatable, JsonEncodable {
105118
public init(from decoder: any Decoder) throws {
106119
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
107120
if try container.decode(ResponseMessageType.self, forKey: .type) != .GQL_COMPLETE {
108-
throw DecodingError.dataCorrupted(.init(
109-
codingPath: decoder.codingPath,
110-
debugDescription: "type must be `\(ResponseMessageType.GQL_COMPLETE.type)`"
111-
))
121+
throw DecodingError.dataCorrupted(
122+
.init(
123+
codingPath: decoder.codingPath,
124+
debugDescription: "type must be `\(ResponseMessageType.GQL_COMPLETE.type)`"
125+
)
126+
)
112127
}
113128
id = try container.decode(String.self, forKey: .id)
114129
}
@@ -136,10 +151,12 @@ public struct ErrorResponse: Equatable, JsonEncodable {
136151
public init(from decoder: any Decoder) throws {
137152
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
138153
if try container.decode(ResponseMessageType.self, forKey: .type) != .GQL_ERROR {
139-
throw DecodingError.dataCorrupted(.init(
140-
codingPath: decoder.codingPath,
141-
debugDescription: "type must be `\(ResponseMessageType.GQL_ERROR.type)`"
142-
))
154+
throw DecodingError.dataCorrupted(
155+
.init(
156+
codingPath: decoder.codingPath,
157+
debugDescription: "type must be `\(ResponseMessageType.GQL_ERROR.type)`"
158+
)
159+
)
143160
}
144161
payload = try container.decode([GraphQLError].self, forKey: .payload)
145162
id = try container.decode(String.self, forKey: .id)

Sources/GraphQLWS/Server.swift

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ public actor Server<
88
InitPayload: Equatable & Codable & Sendable,
99
InitPayloadResult: Sendable,
1010
SubscriptionSequenceType: AsyncSequence & Sendable
11-
> where
11+
>
12+
where
1213
SubscriptionSequenceType.Element == GraphQLResult
1314
{
1415
let messenger: Messenger
@@ -39,7 +40,8 @@ public actor Server<
3940
messenger: Messenger,
4041
onInit: @escaping (InitPayload) async throws -> InitPayloadResult,
4142
onExecute: @escaping (GraphQLRequest, InitPayloadResult) async throws -> GraphQLResult,
42-
onSubscribe: @escaping (GraphQLRequest, InitPayloadResult) async throws -> SubscriptionSequenceType,
43+
onSubscribe:
44+
@escaping (GraphQLRequest, InitPayloadResult) async throws -> SubscriptionSequenceType,
4345
onOperationComplete: @escaping (String) async throws -> Void = { _ in },
4446
onOperationError: @escaping (String, [Error]) async throws -> Void = { _, _ in }
4547
) {
@@ -53,7 +55,8 @@ public actor Server<
5355

5456
/// Listen and react to the provided async sequence of client messages. This function will block until the stream is completed.
5557
/// - Parameter incoming: The client message sequence that the server should react to.
56-
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws -> Void where A.Element == String {
58+
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws
59+
where A.Element == String {
5760
for try await message in incoming {
5861
// Detect and ignore error responses.
5962
if message.starts(with: "44") {
@@ -77,7 +80,12 @@ public actor Server<
7780
// handle incoming message
7881
switch request.type {
7982
case .GQL_CONNECTION_INIT:
80-
guard let connectionInitRequest = try? decoder.decode(ConnectionInitRequest<InitPayload>.self, from: json) else {
83+
guard
84+
let connectionInitRequest = try? decoder.decode(
85+
ConnectionInitRequest<InitPayload>.self,
86+
from: json
87+
)
88+
else {
8189
try await error(.invalidRequestFormat(messageType: .GQL_CONNECTION_INIT))
8290
return
8391
}
@@ -95,7 +103,12 @@ public actor Server<
95103
}
96104
try await onStop(stopRequest)
97105
case .GQL_CONNECTION_TERMINATE:
98-
guard let connectionTerminateRequest = try? decoder.decode(ConnectionTerminateRequest.self, from: json) else {
106+
guard
107+
let connectionTerminateRequest = try? decoder.decode(
108+
ConnectionTerminateRequest.self,
109+
from: json
110+
)
111+
else {
99112
try await error(.invalidRequestFormat(messageType: .GQL_CONNECTION_TERMINATE))
100113
return
101114
}
@@ -110,7 +123,10 @@ public actor Server<
110123
subscriptionTasks.values.forEach { $0.cancel() }
111124
}
112125

113-
private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest<InitPayload>, _: Messenger) async throws {
126+
private func onConnectionInit(
127+
_ connectionInitRequest: ConnectionInitRequest<InitPayload>,
128+
_: Messenger
129+
) async throws {
114130
guard !initialized else {
115131
try await error(.tooManyInitializations())
116132
return
@@ -189,7 +205,9 @@ public actor Server<
189205
try await onOperationComplete(id)
190206
}
191207

192-
private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger) async throws {
208+
private func onConnectionTerminate(_: ConnectionTerminateRequest, _ messenger: Messenger)
209+
async throws
210+
{
193211
for (_, subscriptionTask) in subscriptionTasks {
194212
subscriptionTask.cancel()
195213
}

0 commit comments

Comments
 (0)