Skip to content

Commit e381e88

Browse files
Merge pull request #10 from GraphQLSwift/refactor/swift-format
Converts to swift-format
2 parents 6d62d9f + dcdc00c commit e381e88

10 files changed

Lines changed: 113 additions & 67 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: "GraphQLTransportWS",
1111
targets: ["GraphQLTransportWS"]
12-
),
12+
)
1313
],
1414
dependencies: [
1515
.package(url: "https://github.com/GraphQLSwift/Graphiti.git", from: "3.0.0"),

Sources/GraphQLTransportWS/Client.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public actor Client<InitPayload: Equatable & Codable> {
2323
/// - onComplete: The callback run on receipt of a `complete` message
2424
public init(
2525
messenger: Messenger,
26-
onConnectionAck: @escaping (ConnectionAckResponse, Client) async throws -> Void = { _, _ in },
26+
onConnectionAck: @escaping (ConnectionAckResponse, Client) async throws -> Void = { _, _ in
27+
},
2728
onNext: @escaping (NextResponse, Client) async throws -> Void = { _, _ in },
2829
onError: @escaping (ErrorResponse, Client) async throws -> Void = { _, _ in },
2930
onComplete: @escaping (CompleteResponse, Client) async throws -> Void = { _, _ in }
@@ -37,7 +38,8 @@ public actor Client<InitPayload: Equatable & Codable> {
3738

3839
/// Listen and react to the provided async sequence of server messages. This function will block until the stream is completed.
3940
/// - Parameter incoming: The server message sequence that the client should react to.
40-
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws -> Void where A.Element == String {
41+
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws
42+
where A.Element == String {
4143
for try await message in incoming {
4244
// Detect and ignore error responses.
4345
if message.starts(with: "44") {
@@ -60,7 +62,12 @@ public actor Client<InitPayload: Equatable & Codable> {
6062

6163
switch response.type {
6264
case .connectionAck:
63-
guard let connectionAckResponse = try? decoder.decode(ConnectionAckResponse.self, from: json) else {
65+
guard
66+
let connectionAckResponse = try? decoder.decode(
67+
ConnectionAckResponse.self,
68+
from: json
69+
)
70+
else {
6471
try await error(.invalidResponseFormat(messageType: .connectionAck))
6572
return
6673
}
@@ -78,7 +85,8 @@ public actor Client<InitPayload: Equatable & Codable> {
7885
}
7986
try await onError(errorResponse, self)
8087
case .complete:
81-
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: json) else {
88+
guard let completeResponse = try? decoder.decode(CompleteResponse.self, from: json)
89+
else {
8290
try await error(.invalidResponseFormat(messageType: .complete))
8391
return
8492
}

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 & 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/GraphQLTransportWS/Requests.swift

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ 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) != .connectionInit {
21-
throw DecodingError.dataCorrupted(.init(
22-
codingPath: decoder.codingPath,
23-
debugDescription: "type must be `\(RequestMessageType.connectionInit.type)`"
24-
))
21+
throw DecodingError.dataCorrupted(
22+
.init(
23+
codingPath: decoder.codingPath,
24+
debugDescription: "type must be `\(RequestMessageType.connectionInit.type)`"
25+
)
26+
)
2527
}
2628
payload = try container.decode(InitPayload.self, forKey: .payload)
2729
}
@@ -41,10 +43,12 @@ public struct SubscribeRequest: Equatable, JsonEncodable {
4143
public init(from decoder: any Decoder) throws {
4244
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
4345
if try container.decode(RequestMessageType.self, forKey: .type) != .subscribe {
44-
throw DecodingError.dataCorrupted(.init(
45-
codingPath: decoder.codingPath,
46-
debugDescription: "type must be `\(RequestMessageType.subscribe.type)`"
47-
))
46+
throw DecodingError.dataCorrupted(
47+
.init(
48+
codingPath: decoder.codingPath,
49+
debugDescription: "type must be `\(RequestMessageType.subscribe.type)`"
50+
)
51+
)
4852
}
4953
payload = try container.decode(GraphQLRequest.self, forKey: .payload)
5054
id = try container.decode(String.self, forKey: .id)
@@ -63,10 +67,12 @@ public struct CompleteRequest: Equatable, JsonEncodable {
6367
public init(from decoder: any Decoder) throws {
6468
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
6569
if try container.decode(RequestMessageType.self, forKey: .type) != .complete {
66-
throw DecodingError.dataCorrupted(.init(
67-
codingPath: decoder.codingPath,
68-
debugDescription: "type must be `\(RequestMessageType.complete.type)`"
69-
))
70+
throw DecodingError.dataCorrupted(
71+
.init(
72+
codingPath: decoder.codingPath,
73+
debugDescription: "type must be `\(RequestMessageType.complete.type)`"
74+
)
75+
)
7076
}
7177
id = try container.decode(String.self, forKey: .id)
7278
}

Sources/GraphQLTransportWS/Responses.swift

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ 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) != .connectionAck {
21-
throw DecodingError.dataCorrupted(.init(
22-
codingPath: decoder.codingPath,
23-
debugDescription: "type must be `\(ResponseMessageType.connectionAck.type)`"
24-
))
21+
throw DecodingError.dataCorrupted(
22+
.init(
23+
codingPath: decoder.codingPath,
24+
debugDescription: "type must be `\(ResponseMessageType.connectionAck.type)`"
25+
)
26+
)
2527
}
2628
payload = try container.decodeIfPresent([String: Map].self, forKey: .payload)
2729
}
@@ -41,10 +43,12 @@ public struct NextResponse: Equatable, JsonEncodable {
4143
public init(from decoder: any Decoder) throws {
4244
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
4345
if try container.decode(ResponseMessageType.self, forKey: .type) != .next {
44-
throw DecodingError.dataCorrupted(.init(
45-
codingPath: decoder.codingPath,
46-
debugDescription: "type must be `\(ResponseMessageType.next.type)`"
47-
))
46+
throw DecodingError.dataCorrupted(
47+
.init(
48+
codingPath: decoder.codingPath,
49+
debugDescription: "type must be `\(ResponseMessageType.next.type)`"
50+
)
51+
)
4852
}
4953
payload = try container.decodeIfPresent(GraphQLResult.self, forKey: .payload)
5054
id = try container.decode(String.self, forKey: .id)
@@ -63,10 +67,12 @@ public struct CompleteResponse: Equatable, JsonEncodable {
6367
public init(from decoder: any Decoder) throws {
6468
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
6569
if try container.decode(ResponseMessageType.self, forKey: .type) != .complete {
66-
throw DecodingError.dataCorrupted(.init(
67-
codingPath: decoder.codingPath,
68-
debugDescription: "type must be `\(ResponseMessageType.complete.type)`"
69-
))
70+
throw DecodingError.dataCorrupted(
71+
.init(
72+
codingPath: decoder.codingPath,
73+
debugDescription: "type must be `\(ResponseMessageType.complete.type)`"
74+
)
75+
)
7076
}
7177
id = try container.decode(String.self, forKey: .id)
7278
}
@@ -94,10 +100,12 @@ public struct ErrorResponse: Equatable, JsonEncodable {
94100
public init(from decoder: any Decoder) throws {
95101
let container = try decoder.container(keyedBy: Self.CodingKeys.self)
96102
if try container.decode(ResponseMessageType.self, forKey: .type) != .error {
97-
throw DecodingError.dataCorrupted(.init(
98-
codingPath: decoder.codingPath,
99-
debugDescription: "type must be `\(ResponseMessageType.error.type)`"
100-
))
103+
throw DecodingError.dataCorrupted(
104+
.init(
105+
codingPath: decoder.codingPath,
106+
debugDescription: "type must be `\(ResponseMessageType.error.type)`"
107+
)
108+
)
101109
}
102110
payload = try container.decode([GraphQLError].self, forKey: .payload)
103111
id = try container.decode(String.self, forKey: .id)

Sources/GraphQLTransportWS/Server.swift

Lines changed: 20 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
@@ -38,7 +39,8 @@ public actor Server<
3839
messenger: Messenger,
3940
onInit: @escaping (InitPayload) async throws -> InitPayloadResult,
4041
onExecute: @escaping (GraphQLRequest, InitPayloadResult) async throws -> GraphQLResult,
41-
onSubscribe: @escaping (GraphQLRequest, InitPayloadResult) async throws -> SubscriptionSequenceType,
42+
onSubscribe:
43+
@escaping (GraphQLRequest, InitPayloadResult) async throws -> SubscriptionSequenceType,
4244
onOperationComplete: @escaping (String) async throws -> Void = { _ in },
4345
onOperationError: @escaping (String, [Error]) async throws -> Void = { _, _ in }
4446
) {
@@ -52,7 +54,8 @@ public actor Server<
5254

5355
/// Listen and react to the provided async sequence of client messages. This function will block until the stream is completed.
5456
/// - Parameter incoming: The client message sequence that the server should react to.
55-
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws -> Void where A.Element == String {
57+
public func listen<A: AsyncSequence & Sendable>(to incoming: A) async throws
58+
where A.Element == String {
5659
for try await message in incoming {
5760
// Detect and ignore error responses.
5861
if message.starts(with: "44") {
@@ -76,19 +79,26 @@ public actor Server<
7679
// handle incoming message
7780
switch request.type {
7881
case .connectionInit:
79-
guard let connectionInitRequest = try? decoder.decode(ConnectionInitRequest<InitPayload>.self, from: json) else {
82+
guard
83+
let connectionInitRequest = try? decoder.decode(
84+
ConnectionInitRequest<InitPayload>.self,
85+
from: json
86+
)
87+
else {
8088
try await error(.invalidRequestFormat(messageType: .connectionInit))
8189
return
8290
}
8391
try await onConnectionInit(connectionInitRequest, messenger)
8492
case .subscribe:
85-
guard let subscribeRequest = try? decoder.decode(SubscribeRequest.self, from: json) else {
93+
guard let subscribeRequest = try? decoder.decode(SubscribeRequest.self, from: json)
94+
else {
8695
try await error(.invalidRequestFormat(messageType: .subscribe))
8796
return
8897
}
8998
try await onSubscribe(subscribeRequest)
9099
case .complete:
91-
guard let completeRequest = try? decoder.decode(CompleteRequest.self, from: json) else {
100+
guard let completeRequest = try? decoder.decode(CompleteRequest.self, from: json)
101+
else {
92102
try await error(.invalidRequestFormat(messageType: .complete))
93103
return
94104
}
@@ -103,7 +113,10 @@ public actor Server<
103113
subscriptionTasks.values.forEach { $0.cancel() }
104114
}
105115

106-
private func onConnectionInit(_ connectionInitRequest: ConnectionInitRequest<InitPayload>, _: Messenger) async throws {
116+
private func onConnectionInit(
117+
_ connectionInitRequest: ConnectionInitRequest<InitPayload>,
118+
_: Messenger
119+
) async throws {
107120
guard !initialized else {
108121
try await error(.tooManyInitializations())
109122
return

Tests/GraphQLTransportWSTests/GraphQLTransportWSTests.swift

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ struct GraphqlTransportWSTests {
2828
).get()
2929
}
3030
)
31-
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>.makeStream()
31+
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>
32+
.makeStream()
3233
let serverMessageStream = serverMessenger.stream.map { message in
3334
messageContinuation.yield(message)
3435
// Expect only one message
@@ -51,10 +52,10 @@ struct GraphqlTransportWSTests {
5152
try await client.sendStart(
5253
payload: GraphQLRequest(
5354
query: """
54-
query {
55-
hello
56-
}
57-
"""
55+
query {
56+
hello
57+
}
58+
"""
5859
),
5960
id: UUID().uuidString
6061
)
@@ -64,8 +65,7 @@ struct GraphqlTransportWSTests {
6465
result.append(message)
6566
}
6667
#expect(
67-
messages ==
68-
["\(ErrorCode.notInitialized): Connection not initialized"]
68+
messages == ["\(ErrorCode.notInitialized): Connection not initialized"]
6969
)
7070
}
7171

@@ -91,7 +91,8 @@ struct GraphqlTransportWSTests {
9191
).get()
9292
}
9393
)
94-
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>.makeStream()
94+
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>
95+
.makeStream()
9596
let serverMessageStream = serverMessenger.stream.map { message in
9697
messageContinuation.yield(message)
9798
// Expect only one message
@@ -122,8 +123,7 @@ struct GraphqlTransportWSTests {
122123
result.append(message)
123124
}
124125
#expect(
125-
messages ==
126-
["\(ErrorCode.unauthorized): Unauthorized"]
126+
messages == ["\(ErrorCode.unauthorized): Unauthorized"]
127127
)
128128
}
129129

@@ -149,7 +149,8 @@ struct GraphqlTransportWSTests {
149149
).get()
150150
}
151151
)
152-
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>.makeStream()
152+
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>
153+
.makeStream()
153154
let serverMessageStream = serverMessenger.stream.map { message in
154155
messageContinuation.yield(message)
155156
return message
@@ -160,10 +161,10 @@ struct GraphqlTransportWSTests {
160161
try await client.sendStart(
161162
payload: GraphQLRequest(
162163
query: """
163-
query {
164-
hello
165-
}
166-
"""
164+
query {
165+
hello
166+
}
167+
"""
167168
),
168169
id: id
169170
)
@@ -194,8 +195,7 @@ struct GraphqlTransportWSTests {
194195
result.append(message)
195196
}
196197
#expect(
197-
messages.count ==
198-
3 // 1 connection_ack, 1 next, 1 complete
198+
messages.count == 3 // 1 connection_ack, 1 next, 1 complete
199199
)
200200
}
201201

@@ -226,7 +226,8 @@ struct GraphqlTransportWSTests {
226226
return subscription
227227
}
228228
)
229-
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>.makeStream()
229+
let (messageStream, messageContinuation) = AsyncThrowingStream<String, any Error>
230+
.makeStream()
230231
// Used to extract the server messages
231232
let serverMessageStream = serverMessenger.stream.map { message in
232233
messageContinuation.yield(message)
@@ -238,10 +239,10 @@ struct GraphqlTransportWSTests {
238239
try await client.sendStart(
239240
payload: GraphQLRequest(
240241
query: """
241-
subscription {
242-
hello
243-
}
244-
"""
242+
subscription {
243+
hello
244+
}
245+
"""
245246
),
246247
id: id
247248
)
@@ -285,7 +286,7 @@ struct GraphqlTransportWSTests {
285286
result.append(message)
286287
}
287288
#expect(
288-
messages.count == 5 // 1 connection_ack, 3 next, 1 complete
289+
messages.count == 5 // 1 connection_ack, 3 next, 1 complete
289290
)
290291
}
291292

0 commit comments

Comments
 (0)