Skip to content

Commit 43ea113

Browse files
committed
Revert "Revert "Swap MapRepresentable with Encodable.""
This reverts commit 354fcfb.
1 parent 354fcfb commit 43ea113

39 files changed

+7022
-2215
lines changed

.gitignore

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
.DS_Store
2-
/.build
3-
/Packages
4-
/*.xcodeproj
2+
3+
### SwiftPM ###
4+
Packages
5+
.build/
6+
xcuserdata
7+
DerivedData/
8+
*.xcodeproj
9+
10+
### Xcode Patch ###
11+
*.xcodeproj/*
12+
!*.xcodeproj/project.pbxproj
13+
!*.xcodeproj/xcshareddata/
14+
!*.xcworkspace/contents.xcworkspacedata
15+
/*.gcno
16+
**/xcshareddata/WorkspaceSettings.xcsettings
17+
18+
## User settings
19+
xcuserdata/

.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Package.resolved

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ let package = Package(
1010

1111
dependencies: [
1212
.package(url: "https://github.com/wickwirew/Runtime.git", .upToNextMinor(from: "2.1.0")),
13-
14-
// ⏱ Promises and reactive-streams in Swift built for high-performance and scalability.
15-
.package(url: "https://github.com/vapor/core.git", from: "3.0.0"),
13+
.package(url: "https://github.com/apple/swift-nio.git", from: "1.14.1"),
1614
],
1715

1816
targets: [
19-
.target(name: "GraphQL", dependencies: ["Runtime", "Async"]),
17+
.target(name: "GraphQL", dependencies: ["Runtime", "NIO"]),
2018
.testTarget(name: "GraphQLTests", dependencies: ["GraphQL"]),
2119
]
2220
)

Sources/GraphQL/Error/GraphQLError.swift

Lines changed: 122 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
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 {
7+
public struct GraphQLError : Error, Encodable {
8+
private enum CodingKeys : String, CodingKey {
9+
case message
10+
case locations
11+
case path
12+
}
813

914
/**
1015
* A message describing the Error for debugging purposes.
@@ -31,7 +36,7 @@ public struct GraphQLError : Error {
3136
*
3237
* Appears in the result of `description`.
3338
*/
34-
public let path: [IndexPathElement]
39+
public let path: IndexPath
3540

3641
/**
3742
* An array of GraphQL AST Nodes corresponding to this error.
@@ -54,8 +59,14 @@ public struct GraphQLError : Error {
5459
*/
5560
public let originalError: Error?
5661

57-
public init(message: String, nodes: [Node] = [], source: Source? = nil, positions: [Int] = [],
58-
path: [IndexPathElement] = [], originalError: Error? = nil) {
62+
public init(
63+
message: String,
64+
nodes: [Node] = [],
65+
source: Source? = nil,
66+
positions: [Int] = [],
67+
path: IndexPath = [],
68+
originalError: Error? = nil
69+
) {
5970
self.message = message
6071
self.nodes = nodes
6172

@@ -82,6 +93,27 @@ public struct GraphQLError : Error {
8293
self.path = path
8394
self.originalError = originalError
8495
}
96+
97+
public init(
98+
message: String,
99+
locations: [SourceLocation],
100+
path: IndexPath = []
101+
) {
102+
self.message = message
103+
self.locations = locations
104+
self.path = path
105+
self.nodes = []
106+
self.source = nil
107+
self.positions = []
108+
self.originalError = nil
109+
}
110+
111+
public init(_ error: Error) {
112+
self.init(
113+
message: error.localizedDescription,
114+
originalError: error
115+
)
116+
}
85117
}
86118

87119
extension GraphQLError : CustomStringConvertible {
@@ -100,23 +132,96 @@ extension GraphQLError : Hashable {
100132
}
101133
}
102134

103-
extension GraphQLError : MapRepresentable {
104-
public var map: Map {
105-
var dictionary: [String: Map] = ["message": message.map]
135+
// MARK: IndexPath
136+
137+
public struct IndexPath : Encodable {
138+
public let elements: [IndexPathValue]
139+
140+
public init(_ elements: [IndexPathElement] = []) {
141+
self.elements = elements.map({ $0.indexPathValue })
142+
}
143+
144+
public func appending(_ elements: IndexPathElement) -> IndexPath {
145+
return IndexPath(self.elements + [elements])
146+
}
147+
}
148+
149+
extension IndexPath : ExpressibleByArrayLiteral {
150+
public init(arrayLiteral elements: IndexPathElement...) {
151+
self.elements = elements.map({ $0.indexPathValue })
152+
}
153+
}
106154

107-
if !path.isEmpty {
108-
dictionary["path"] = path.map({ $0.map }).map
155+
public enum IndexPathValue : Encodable {
156+
case index(Int)
157+
case key(String)
158+
159+
public func encode(to encoder: Encoder) throws {
160+
var container = encoder.singleValueContainer()
161+
162+
switch self {
163+
case let .index(index):
164+
try container.encode(index)
165+
case let .key(key):
166+
try container.encode(key)
109167
}
168+
}
169+
}
110170

111-
if !locations.isEmpty {
112-
dictionary["locations"] = locations.map({
113-
return [
114-
"line": $0.line.map,
115-
"column": $0.column.map
116-
] as Map
117-
}).map
171+
extension IndexPathValue : IndexPathElement {
172+
public var indexPathValue: IndexPathValue {
173+
return self
174+
}
175+
}
176+
177+
extension IndexPathValue : CustomStringConvertible {
178+
public var description: String {
179+
switch self {
180+
case let .index(index):
181+
return index.description
182+
case let .key(key):
183+
return key.description
118184
}
185+
}
186+
}
187+
188+
public protocol IndexPathElement {
189+
var indexPathValue: IndexPathValue { get }
190+
}
191+
192+
extension IndexPathElement {
193+
var constructEmptyContainer: Map {
194+
switch indexPathValue {
195+
case .index: return []
196+
case .key: return [:]
197+
}
198+
}
199+
}
200+
201+
extension IndexPathElement {
202+
public var indexValue: Int? {
203+
if case .index(let index) = indexPathValue {
204+
return index
205+
}
206+
return nil
207+
}
208+
209+
public var keyValue: String? {
210+
if case .key(let key) = indexPathValue {
211+
return key
212+
}
213+
return nil
214+
}
215+
}
216+
217+
extension Int : IndexPathElement {
218+
public var indexPathValue: IndexPathValue {
219+
return .index(self)
220+
}
221+
}
119222

120-
return .dictionary(dictionary)
223+
extension String : IndexPathElement {
224+
public var indexPathValue: IndexPathValue {
225+
return .key(self)
121226
}
122227
}

Sources/GraphQL/Error/LocatedError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* GraphQL operation, produce a new GraphQLError aware of the location in the
44
* document responsible for the original Error.
55
*/
6-
func locatedError(originalError: Error, nodes: [Node], path: [IndexPathElement]) -> GraphQLError {
6+
func locatedError(originalError: Error, nodes: [Node], path: IndexPath) -> GraphQLError {
77
// Note: this uses a brand-check to support GraphQL errors originating from
88
// other contexts.
99
if let originalError = originalError as? GraphQLError {

0 commit comments

Comments
 (0)