Skip to content

Commit ed02fc0

Browse files
Merge pull request #14 from GraphQLSwift/refactor/swift-format
Converts to swift-format
2 parents 6cc6c34 + 67eae71 commit ed02fc0

17 files changed

Lines changed: 429 additions & 225 deletions

.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+
}

Examples/HelloWorld/Package.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ import PackageDescription
55
let package = Package(
66
name: "HelloWorld",
77
platforms: [
8-
.macOS(.v14),
8+
.macOS(.v14)
99
],
1010
dependencies: [
1111
.package(name: "graphql-hummingbird", path: "../../"),
1212
.package(url: "https://github.com/apple/swift-async-algorithms.git", from: "1.0.0"),
1313
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "4.0.0"),
1414
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
15-
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "2.0.0"),
15+
.package(
16+
url: "https://github.com/hummingbird-project/hummingbird-websocket.git",
17+
from: "2.0.0"
18+
),
1619
],
1720
targets: [
1821
.executableTarget(
@@ -24,6 +27,6 @@ let package = Package(
2427
.product(name: "Hummingbird", package: "hummingbird"),
2528
.product(name: "HummingbirdWebSocket", package: "hummingbird-websocket"),
2629
]
27-
),
30+
)
2831
]
2932
)

Examples/HelloWorld/Sources/HelloWorld/HelloWorld.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct HelloWorld {
1717
resolve: { _, _, _, _ in
1818
"World"
1919
}
20-
),
20+
)
2121
]
2222
),
2323
subscription: GraphQLObjectType(
@@ -32,11 +32,14 @@ struct HelloWorld {
3232
subscribe: { _, _, _, _ in
3333
let clock = ContinuousClock()
3434
let start = clock.now
35-
return AsyncTimerSequence(interval: .seconds(3), clock: ContinuousClock()).map { instant in
35+
return AsyncTimerSequence(
36+
interval: .seconds(3),
37+
clock: ContinuousClock()
38+
).map { instant in
3639
"World at \(start.duration(to: instant))"
3740
}
3841
}
39-
),
42+
)
4043
]
4144
)
4245
)

Package.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ import PackageDescription
55
let package = Package(
66
name: "GraphQLHummingbird",
77
platforms: [
8-
.macOS(.v14),
8+
.macOS(.v14)
99
],
1010
products: [
1111
.library(
1212
name: "GraphQLHummingbird",
1313
targets: ["GraphQLHummingbird"]
14-
),
14+
)
1515
],
1616
dependencies: [
1717
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
1818
.package(url: "https://github.com/GraphQLSwift/GraphQL.git", from: "4.0.0"),
1919
.package(url: "https://github.com/GraphQLSwift/GraphQLTransportWS.git", from: "1.0.0"),
2020
.package(url: "https://github.com/GraphQLSwift/GraphQLWS.git", from: "1.0.0"),
2121
.package(url: "https://github.com/hummingbird-project/hummingbird.git", from: "2.0.0"),
22-
.package(url: "https://github.com/hummingbird-project/hummingbird-websocket.git", from: "2.0.0"),
22+
.package(
23+
url: "https://github.com/hummingbird-project/hummingbird-websocket.git",
24+
from: "2.0.0"
25+
),
2326
],
2427
targets: [
2528
.target(

Sources/GraphQLHummingbird/Coding/GraphQLJSONEncoder+encode.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ extension GraphQLJSONEncoder: @retroactive ResponseEncoder {
77
/// - value: Value to encode
88
/// - request: Request used to generate response
99
/// - context: Request context
10-
public func encode(_ value: some Encodable, from request: Request, context: some RequestContext) throws -> Response {
10+
public func encode(_ value: some Encodable, from request: Request, context: some RequestContext)
11+
throws -> Response
12+
{
1113
try encode(value, status: .ok, from: request, context: context)
1214
}
1315
}
@@ -20,7 +22,12 @@ extension GraphQLJSONEncoder {
2022
/// - status: The status of the response
2123
/// - request: Request used to generate response
2224
/// - context: Request context
23-
func encode(_ value: some Encodable, status: HTTPResponse.Status, from _: Request, context _: some RequestContext) throws -> Response {
25+
func encode(
26+
_ value: some Encodable,
27+
status: HTTPResponse.Status,
28+
from _: Request,
29+
context _: some RequestContext
30+
) throws -> Response {
2431
let data = try encode(value)
2532
let buffer = ByteBuffer(bytes: data)
2633
return Response(
@@ -40,7 +47,9 @@ extension GraphQLJSONEncoder {
4047
/// - value: GraphQLResult to encode
4148
/// - request: Request used to generate response
4249
/// - context: Request context
43-
func encode(_ value: GraphQLResult, from request: Request, context: some RequestContext) throws -> Response {
50+
func encode(_ value: GraphQLResult, from request: Request, context: some RequestContext) throws
51+
-> Response
52+
{
4453
var status = HTTPResponse.Status.ok
4554
// We must return `bad request` with the content if there were failures preventing a partial result
4655
// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#applicationgraphql-responsejson
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import Hummingbird
22

3-
public extension MediaType {
4-
static let applicationJsonGraphQL = MediaType(type: .application, subType: "graphql-response+json")
3+
extension MediaType {
4+
public static let applicationJsonGraphQL = MediaType(
5+
type: .application,
6+
subType: "graphql-response+json"
7+
)
58
}

Sources/GraphQLHummingbird/GraphQLConfig.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,17 @@ public struct GraphQLConfig<
102102

103103
/// WebSocket configuration
104104
public struct WebSocket: Sendable {
105-
let onWebSocketInit: @Sendable (WebSocketInit, Request, Context) async throws -> WebSocketInitResult
105+
let onWebSocketInit:
106+
@Sendable (WebSocketInit, Request, Context) async throws -> WebSocketInitResult
106107

107108
/// GraphQL over WebSocket configuration
108109
/// - Parameter onWebSocketInit: A custom callback run during `connection_init` resolution that allows
109110
/// authorization using the `payload` field of the `connection_init` message.
110111
/// Throw from this closure to indicate that authorization has failed.
111112
public init(
112-
onWebSocketInit: @Sendable @escaping (WebSocketInit, Request, Context) async throws -> WebSocketInitResult = { (_: EmptyWebSocketInit, _: Request, _: Context) in }
113+
onWebSocketInit:
114+
@Sendable @escaping (WebSocketInit, Request, Context) async throws ->
115+
WebSocketInitResult = { (_: EmptyWebSocketInit, _: Request, _: Context) in }
113116
) {
114117
self.onWebSocketInit = onWebSocketInit
115118
}

Sources/GraphQLHummingbird/GraphQLHandler.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ struct GraphQLHandler<
1010
let schema: GraphQLSchema
1111
let rootValue: any Sendable
1212
let config: GraphQLConfig<Context, WebSocketInit, WebSocketInitResult>
13-
let computeContext: @Sendable (GraphQLContextComputationInputs<Context, WebSocketInitResult>) async throws -> GraphQLContext
13+
let computeContext:
14+
@Sendable (GraphQLContextComputationInputs<Context, WebSocketInitResult>) async throws ->
15+
GraphQLContext
1416
}
1517

1618
/// Request metadata that can be used to construct a GraphQL context

Sources/GraphQLHummingbird/HTTP/GraphQLHandler+HTTP.swift

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ extension GraphQLHandler {
2424
guard operationType != .mutation else {
2525
throw HTTPError(.methodNotAllowed, message: "Mutations using GET are disallowed")
2626
}
27-
let graphQLContextComputationInputs = GraphQLContextComputationInputs<Context, WebSocketInitResult>(
27+
let graphQLContextComputationInputs = GraphQLContextComputationInputs<
28+
Context, WebSocketInitResult
29+
>(
2830
hummingbirdRequest: request,
2931
hummingbirdContext: context,
3032
graphQLRequest: graphQLRequest,
@@ -50,21 +52,31 @@ extension GraphQLHandler {
5052
switch mediaType {
5153
case .applicationJson, .applicationJsonGraphQL:
5254
do {
53-
graphQLRequest = try await config.coders.jsonDecoder.decode(GraphQLRequest.self, from: request, context: context)
55+
graphQLRequest = try await config.coders.jsonDecoder.decode(
56+
GraphQLRequest.self,
57+
from: request,
58+
context: context
59+
)
5460
} catch {
5561
throw HTTPError(.badRequest, message: error.localizedDescription)
5662
}
5763
case .applicationUrlEncoded:
5864
do {
59-
graphQLRequest = try await config.coders.urlEncodedFormDecoder.decode(GraphQLRequest.self, from: request, context: context)
65+
graphQLRequest = try await config.coders.urlEncodedFormDecoder.decode(
66+
GraphQLRequest.self,
67+
from: request,
68+
context: context
69+
)
6070
} catch {
6171
throw HTTPError(.badRequest, message: error.localizedDescription)
6272
}
6373
default:
6474
throw HTTPError(.unsupportedMediaType)
6575
}
6676

67-
let graphQLContextComputationInputs = GraphQLContextComputationInputs<Context, WebSocketInitResult>(
77+
let graphQLContextComputationInputs = GraphQLContextComputationInputs<
78+
Context, WebSocketInitResult
79+
>(
6880
hummingbirdRequest: request,
6981
hummingbirdContext: context,
7082
graphQLRequest: graphQLRequest,
@@ -103,13 +115,18 @@ extension GraphQLHandler {
103115
// This indicates a request parsing error
104116
return GraphQLResult(data: nil, errors: [error])
105117
} catch {
106-
return GraphQLResult(data: nil, errors: [GraphQLError(message: error.localizedDescription)])
118+
return GraphQLResult(
119+
data: nil,
120+
errors: [GraphQLError(message: error.localizedDescription)]
121+
)
107122
}
108123
return result
109124
}
110125

111126
/// https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md#body
112-
private func encodeResponse(result: GraphQLResult, request: Request, context: Context) throws -> Response {
127+
private func encodeResponse(result: GraphQLResult, request: Request, context: Context) throws
128+
-> Response
129+
{
113130
let acceptHeader = request.headers[.accept]
114131

115132
if !config.allowMissingAcceptHeader, acceptHeader == nil {
@@ -121,19 +138,31 @@ extension GraphQLHandler {
121138
// Try to respond with the best matching media type, in order
122139
for mediaType in acceptedTypes {
123140
if MediaType.applicationJsonGraphQL.isType(mediaType) {
124-
return try config.coders.graphQLJSONEncoder.encode(result, from: request, context: context)
141+
return try config.coders.graphQLJSONEncoder.encode(
142+
result,
143+
from: request,
144+
context: context
145+
)
125146
}
126147
if MediaType.applicationJson.isType(mediaType) {
127148
return try config.coders.jsonEncoder.encode(result, from: request, context: context)
128149
}
129150
if MediaType.applicationUrlEncoded.isType(mediaType) {
130-
return try config.coders.urlEncodedFormEncoder.encode(result, from: request, context: context)
151+
return try config.coders.urlEncodedFormEncoder.encode(
152+
result,
153+
from: request,
154+
context: context
155+
)
131156
}
132157
}
133158

134159
// Use the default if configured to do so
135160
if config.allowMissingAcceptHeader {
136-
return try config.coders.graphQLJSONEncoder.encode(result, from: request, context: context)
161+
return try config.coders.graphQLJSONEncoder.encode(
162+
result,
163+
from: request,
164+
context: context
165+
)
137166
}
138167

139168
// Fail
@@ -143,7 +172,8 @@ extension GraphQLHandler {
143172
private func parseAcceptHeader(_ header: String?) -> [MediaType] {
144173
guard let header = header else { return [] }
145174

146-
return header
175+
return
176+
header
147177
.split(separator: ",")
148178
.compactMap { segment in
149179
MediaType(from: segment.trimmingCharacters(in: .whitespaces))

0 commit comments

Comments
 (0)