Skip to content

SwiftFormat integration #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ on:
workflow_dispatch:

jobs:
formatlint:
name: Lint for correct formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: sinoru/actions-setup-swift@v2
with:
swift-version: '5.6.1'
- name: GitHub Action for SwiftFormat
uses: CassiusPacheco/[email protected]
with:
swiftformat-version: '0.49.11'

macos:
name: Build and test on macos-latest
runs-on: macOS-latest
Expand Down
7 changes: 7 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
--maxwidth 100
--semicolons never
--xcodeindentation enabled
--wraparguments before-first
--wrapcollections before-first
--wrapconditions before-first
--wrapparameters before-first
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
],
targets: [
.target(
name: "GraphQL",
name: "GraphQL",
dependencies: [
.product(name: "NIO", package: "swift-nio"),
.product(name: "OrderedCollections", package: "swift-collections"),
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ To execute a subscription use the `graphqlSubscribe` function:
```swift
let subscriptionResult = try graphqlSubscribe(
schema: schema,
request: "{ hello }",
eventLoopGroup: eventLoopGroup
).wait()
// Must downcast from EventStream to concrete type to use in 'for await' loop below
let concurrentStream = subscriptionResult.stream! as! ConcurrentEventStream
Expand Down Expand Up @@ -131,6 +129,13 @@ Those contributing to this package are expected to follow the [Swift Code of Con
[Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/), and the
[SSWG Technical Best Practices](https://github.com/swift-server/sswg/blob/main/process/incubation.md#technical-best-practices).

This repo uses [SwiftFormat](https://github.com/nicklockwood/SwiftFormat), and includes lint checks to enforce these formatting standards.
To format your code, install `swiftformat` and run:

```bash
swiftformat .
```

Most of this repo mirrors the structure of
(the canonical GraphQL implementation written in Javascript/Typescript)[https://github.com/graphql/graphql-js]. If there is any feature
missing, looking at the original code and "translating" it to Swift works, most of the time. For example:
Expand Down
84 changes: 42 additions & 42 deletions Sources/GraphQL/Error/GraphQLError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
* it also includes information about the locations in a
* GraphQL document and/or execution result that correspond to the error.
*/
public struct GraphQLError : Error, Codable {
enum CodingKeys : String, CodingKey {
public struct GraphQLError: Error, Codable {
enum CodingKeys: String, CodingKey {
case message
case locations
case path
Expand Down Expand Up @@ -78,22 +78,22 @@ public struct GraphQLError : Error, Codable {
self.source = nil
}

if positions.isEmpty && !nodes.isEmpty {
self.positions = nodes.filter({ $0.loc != nil }).map({ $0.loc!.start })
if positions.isEmpty, !nodes.isEmpty {
self.positions = nodes.filter { $0.loc != nil }.map { $0.loc!.start }
} else {
self.positions = positions
}

if let source = self.source, !self.positions.isEmpty {
self.locations = self.positions.map({ getLocation(source: source, position: $0) })
locations = self.positions.map { getLocation(source: source, position: $0) }
} else {
self.locations = []
locations = []
}

self.path = path
self.originalError = originalError
}

public init(
message: String,
locations: [SourceLocation],
Expand All @@ -102,46 +102,46 @@ public struct GraphQLError : Error, Codable {
self.message = message
self.locations = locations
self.path = path
self.nodes = []
self.source = nil
self.positions = []
self.originalError = nil
nodes = []
source = nil
positions = []
originalError = nil
}

public init(_ error: Error) {
self.init(
message: error.localizedDescription,
originalError: error
)
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
message = try container.decode(String.self, forKey: .message)
locations = try container.decode([SourceLocation]?.self, forKey: .locations) ?? []
path = try container.decode(IndexPath.self, forKey: .path)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(message, forKey: .message)

if !locations.isEmpty {
try container.encode(locations, forKey: .locations)
}

try container.encode(path, forKey: .path)
}
}

extension GraphQLError : CustomStringConvertible {
extension GraphQLError: CustomStringConvertible {
public var description: String {
return message
}
}

extension GraphQLError : Hashable {
extension GraphQLError: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(message)
}
Expand All @@ -153,41 +153,41 @@ extension GraphQLError : Hashable {

// MARK: IndexPath

public struct IndexPath : Codable {
public struct IndexPath: Codable {
public let elements: [IndexPathValue]

public init(_ elements: [IndexPathElement] = []) {
self.elements = elements.map({ $0.indexPathValue })
self.elements = elements.map { $0.indexPathValue }
}

public func appending(_ elements: IndexPathElement) -> IndexPath {
return IndexPath(self.elements + [elements])
}

public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
elements = try container.decode([IndexPathValue].self)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(contentsOf: elements)
}
}

extension IndexPath : ExpressibleByArrayLiteral {
extension IndexPath: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: IndexPathElement...) {
self.elements = elements.map({ $0.indexPathValue })
self.elements = elements.map { $0.indexPathValue }
}
}

public enum IndexPathValue : Codable {
public enum IndexPathValue: Codable {
case index(Int)
case key(String)

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()

if let index = try? container.decode(Int.self) {
self = .index(index)
} else if let key = try? container.decode(String.self) {
Expand All @@ -196,10 +196,10 @@ public enum IndexPathValue : Codable {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Invalid type.")
}
}

public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()

switch self {
case let .index(index):
try container.encode(index)
Expand All @@ -209,13 +209,13 @@ public enum IndexPathValue : Codable {
}
}

extension IndexPathValue : IndexPathElement {
extension IndexPathValue: IndexPathElement {
public var indexPathValue: IndexPathValue {
return self
}
}

extension IndexPathValue : CustomStringConvertible {
extension IndexPathValue: CustomStringConvertible {
public var description: String {
switch self {
case let .index(index):
Expand All @@ -239,29 +239,29 @@ extension IndexPathElement {
}
}

extension IndexPathElement {
public var indexValue: Int? {
if case .index(let index) = indexPathValue {
public extension IndexPathElement {
var indexValue: Int? {
if case let .index(index) = indexPathValue {
return index
}
return nil
}
public var keyValue: String? {
if case .key(let key) = indexPathValue {

var keyValue: String? {
if case let .key(key) = indexPathValue {
return key
}
return nil
}
}

extension Int : IndexPathElement {
extension Int: IndexPathElement {
public var indexPathValue: IndexPathValue {
return .index(self)
}
}

extension String : IndexPathElement {
extension String: IndexPathElement {
public var indexPathValue: IndexPathValue {
return .key(self)
}
Expand Down
15 changes: 7 additions & 8 deletions Sources/GraphQL/Error/SyntaxError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ func syntaxError(source: Source, position: Int, description: String) -> GraphQLE
let error = GraphQLError(
message:
"Syntax Error \(source.name) (\(location.line):\(location.column)) " +
description + "\n\n" +
highlightSourceAtLocation(source: source, location: location),
description + "\n\n" +
highlightSourceAtLocation(source: source, location: location),
source: source,
positions: [position]
)
Expand Down Expand Up @@ -49,17 +49,16 @@ func highlightSourceAtLocation(source: Source, location: SourceLocation) -> Stri
}

func splitLines(string: String) -> [String] {

var lines: [String] = []
var location = 0

let nsstring = NSString(string: string)
do {
let regex = try NSRegularExpression(pattern: "\r\n|[\n\r]", options: [])
for match in regex.matches(in: string, options: [], range: NSRange(0..<nsstring.length)) {
let range = NSRange(location..<match.range.location)
for match in regex.matches(in: string, options: [], range: NSRange(0 ..< nsstring.length)) {
let range = NSRange(location ..< match.range.location)
lines.append(nsstring.substring(with: range))
location = match.range.location + match.range.length
location = match.range.location + match.range.length
}
} catch {
// Let lines and location remain unchanged
Expand All @@ -68,7 +67,7 @@ func splitLines(string: String) -> [String] {
if lines.isEmpty {
return [string]
} else {
let range = NSRange(location..<nsstring.length)
let range = NSRange(location ..< nsstring.length)
lines.append(nsstring.substring(with: range))
}

Expand Down
Loading