Skip to content

Commit c0c9664

Browse files
Merge pull request #106 from NeedleInAJayStack/refactor/swiftformat
SwiftFormat integration
2 parents 17b96ed + 0e9b9db commit c0c9664

File tree

81 files changed

+8282
-5778
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+8282
-5778
lines changed

.github/workflows/build.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@ on:
1010
workflow_dispatch:
1111

1212
jobs:
13+
formatlint:
14+
name: Lint for correct formatting
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v3
18+
- uses: sinoru/actions-setup-swift@v2
19+
with:
20+
swift-version: '5.6.1'
21+
- name: GitHub Action for SwiftFormat
22+
uses: CassiusPacheco/[email protected]
23+
with:
24+
swiftformat-version: '0.49.11'
25+
1326
macos:
1427
name: Build and test on macos-latest
1528
runs-on: macOS-latest

.swiftformat

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
--maxwidth 100
2+
--semicolons never
3+
--xcodeindentation enabled
4+
--wraparguments before-first
5+
--wrapcollections before-first
6+
--wrapconditions before-first
7+
--wrapparameters before-first

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
],
1313
targets: [
1414
.target(
15-
name: "GraphQL",
15+
name: "GraphQL",
1616
dependencies: [
1717
.product(name: "NIO", package: "swift-nio"),
1818
.product(name: "OrderedCollections", package: "swift-collections"),

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ To execute a subscription use the `graphqlSubscribe` function:
9797
```swift
9898
let subscriptionResult = try graphqlSubscribe(
9999
schema: schema,
100-
request: "{ hello }",
101-
eventLoopGroup: eventLoopGroup
102100
).wait()
103101
// Must downcast from EventStream to concrete type to use in 'for await' loop below
104102
let concurrentStream = subscriptionResult.stream! as! ConcurrentEventStream
@@ -131,6 +129,13 @@ Those contributing to this package are expected to follow the [Swift Code of Con
131129
[Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/), and the
132130
[SSWG Technical Best Practices](https://github.com/swift-server/sswg/blob/main/process/incubation.md#technical-best-practices).
133131

132+
This repo uses [SwiftFormat](https://github.com/nicklockwood/SwiftFormat), and includes lint checks to enforce these formatting standards.
133+
To format your code, install `swiftformat` and run:
134+
135+
```bash
136+
swiftformat .
137+
```
138+
134139
Most of this repo mirrors the structure of
135140
(the canonical GraphQL implementation written in Javascript/Typescript)[https://github.com/graphql/graphql-js]. If there is any feature
136141
missing, looking at the original code and "translating" it to Swift works, most of the time. For example:

Sources/GraphQL/Error/GraphQLError.swift

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* it also includes information about the locations in a
55
* GraphQL document and/or execution result that correspond to the error.
66
*/
7-
public struct GraphQLError : Error, Codable {
8-
enum CodingKeys : String, CodingKey {
7+
public struct GraphQLError: Error, Codable {
8+
enum CodingKeys: String, CodingKey {
99
case message
1010
case locations
1111
case path
@@ -78,22 +78,22 @@ public struct GraphQLError : Error, Codable {
7878
self.source = nil
7979
}
8080

81-
if positions.isEmpty && !nodes.isEmpty {
82-
self.positions = nodes.filter({ $0.loc != nil }).map({ $0.loc!.start })
81+
if positions.isEmpty, !nodes.isEmpty {
82+
self.positions = nodes.filter { $0.loc != nil }.map { $0.loc!.start }
8383
} else {
8484
self.positions = positions
8585
}
8686

8787
if let source = self.source, !self.positions.isEmpty {
88-
self.locations = self.positions.map({ getLocation(source: source, position: $0) })
88+
locations = self.positions.map { getLocation(source: source, position: $0) }
8989
} else {
90-
self.locations = []
90+
locations = []
9191
}
9292

9393
self.path = path
9494
self.originalError = originalError
9595
}
96-
96+
9797
public init(
9898
message: String,
9999
locations: [SourceLocation],
@@ -102,46 +102,46 @@ public struct GraphQLError : Error, Codable {
102102
self.message = message
103103
self.locations = locations
104104
self.path = path
105-
self.nodes = []
106-
self.source = nil
107-
self.positions = []
108-
self.originalError = nil
105+
nodes = []
106+
source = nil
107+
positions = []
108+
originalError = nil
109109
}
110-
110+
111111
public init(_ error: Error) {
112112
self.init(
113113
message: error.localizedDescription,
114114
originalError: error
115115
)
116116
}
117-
117+
118118
public init(from decoder: Decoder) throws {
119119
let container = try decoder.container(keyedBy: CodingKeys.self)
120120
message = try container.decode(String.self, forKey: .message)
121121
locations = try container.decode([SourceLocation]?.self, forKey: .locations) ?? []
122122
path = try container.decode(IndexPath.self, forKey: .path)
123123
}
124-
124+
125125
public func encode(to encoder: Encoder) throws {
126126
var container = encoder.container(keyedBy: CodingKeys.self)
127-
127+
128128
try container.encode(message, forKey: .message)
129-
129+
130130
if !locations.isEmpty {
131131
try container.encode(locations, forKey: .locations)
132132
}
133-
133+
134134
try container.encode(path, forKey: .path)
135135
}
136136
}
137137

138-
extension GraphQLError : CustomStringConvertible {
138+
extension GraphQLError: CustomStringConvertible {
139139
public var description: String {
140140
return message
141141
}
142142
}
143143

144-
extension GraphQLError : Hashable {
144+
extension GraphQLError: Hashable {
145145
public func hash(into hasher: inout Hasher) {
146146
hasher.combine(message)
147147
}
@@ -153,41 +153,41 @@ extension GraphQLError : Hashable {
153153

154154
// MARK: IndexPath
155155

156-
public struct IndexPath : Codable {
156+
public struct IndexPath: Codable {
157157
public let elements: [IndexPathValue]
158-
158+
159159
public init(_ elements: [IndexPathElement] = []) {
160-
self.elements = elements.map({ $0.indexPathValue })
160+
self.elements = elements.map { $0.indexPathValue }
161161
}
162-
162+
163163
public func appending(_ elements: IndexPathElement) -> IndexPath {
164164
return IndexPath(self.elements + [elements])
165165
}
166-
166+
167167
public init(from decoder: Decoder) throws {
168168
var container = try decoder.unkeyedContainer()
169169
elements = try container.decode([IndexPathValue].self)
170170
}
171-
171+
172172
public func encode(to encoder: Encoder) throws {
173173
var container = encoder.unkeyedContainer()
174174
try container.encode(contentsOf: elements)
175175
}
176176
}
177177

178-
extension IndexPath : ExpressibleByArrayLiteral {
178+
extension IndexPath: ExpressibleByArrayLiteral {
179179
public init(arrayLiteral elements: IndexPathElement...) {
180-
self.elements = elements.map({ $0.indexPathValue })
180+
self.elements = elements.map { $0.indexPathValue }
181181
}
182182
}
183183

184-
public enum IndexPathValue : Codable {
184+
public enum IndexPathValue: Codable {
185185
case index(Int)
186186
case key(String)
187-
187+
188188
public init(from decoder: Decoder) throws {
189189
let container = try decoder.singleValueContainer()
190-
190+
191191
if let index = try? container.decode(Int.self) {
192192
self = .index(index)
193193
} else if let key = try? container.decode(String.self) {
@@ -196,10 +196,10 @@ public enum IndexPathValue : Codable {
196196
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid type.")
197197
}
198198
}
199-
199+
200200
public func encode(to encoder: Encoder) throws {
201201
var container = encoder.singleValueContainer()
202-
202+
203203
switch self {
204204
case let .index(index):
205205
try container.encode(index)
@@ -209,13 +209,13 @@ public enum IndexPathValue : Codable {
209209
}
210210
}
211211

212-
extension IndexPathValue : IndexPathElement {
212+
extension IndexPathValue: IndexPathElement {
213213
public var indexPathValue: IndexPathValue {
214214
return self
215215
}
216216
}
217217

218-
extension IndexPathValue : CustomStringConvertible {
218+
extension IndexPathValue: CustomStringConvertible {
219219
public var description: String {
220220
switch self {
221221
case let .index(index):
@@ -239,29 +239,29 @@ extension IndexPathElement {
239239
}
240240
}
241241

242-
extension IndexPathElement {
243-
public var indexValue: Int? {
244-
if case .index(let index) = indexPathValue {
242+
public extension IndexPathElement {
243+
var indexValue: Int? {
244+
if case let .index(index) = indexPathValue {
245245
return index
246246
}
247247
return nil
248248
}
249-
250-
public var keyValue: String? {
251-
if case .key(let key) = indexPathValue {
249+
250+
var keyValue: String? {
251+
if case let .key(key) = indexPathValue {
252252
return key
253253
}
254254
return nil
255255
}
256256
}
257257

258-
extension Int : IndexPathElement {
258+
extension Int: IndexPathElement {
259259
public var indexPathValue: IndexPathValue {
260260
return .index(self)
261261
}
262262
}
263263

264-
extension String : IndexPathElement {
264+
extension String: IndexPathElement {
265265
public var indexPathValue: IndexPathValue {
266266
return .key(self)
267267
}

Sources/GraphQL/Error/SyntaxError.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ func syntaxError(source: Source, position: Int, description: String) -> GraphQLE
1010
let error = GraphQLError(
1111
message:
1212
"Syntax Error \(source.name) (\(location.line):\(location.column)) " +
13-
description + "\n\n" +
14-
highlightSourceAtLocation(source: source, location: location),
13+
description + "\n\n" +
14+
highlightSourceAtLocation(source: source, location: location),
1515
source: source,
1616
positions: [position]
1717
)
@@ -49,17 +49,16 @@ func highlightSourceAtLocation(source: Source, location: SourceLocation) -> Stri
4949
}
5050

5151
func splitLines(string: String) -> [String] {
52-
5352
var lines: [String] = []
5453
var location = 0
55-
54+
5655
let nsstring = NSString(string: string)
5756
do {
5857
let regex = try NSRegularExpression(pattern: "\r\n|[\n\r]", options: [])
59-
for match in regex.matches(in: string, options: [], range: NSRange(0..<nsstring.length)) {
60-
let range = NSRange(location..<match.range.location)
58+
for match in regex.matches(in: string, options: [], range: NSRange(0 ..< nsstring.length)) {
59+
let range = NSRange(location ..< match.range.location)
6160
lines.append(nsstring.substring(with: range))
62-
location = match.range.location + match.range.length
61+
location = match.range.location + match.range.length
6362
}
6463
} catch {
6564
// Let lines and location remain unchanged
@@ -68,7 +67,7 @@ func splitLines(string: String) -> [String] {
6867
if lines.isEmpty {
6968
return [string]
7069
} else {
71-
let range = NSRange(location..<nsstring.length)
70+
let range = NSRange(location ..< nsstring.length)
7271
lines.append(nsstring.substring(with: range))
7372
}
7473

0 commit comments

Comments
 (0)