Skip to content

Commit 5b3fc0a

Browse files
committed
Fix GraphQLError encoding.
1 parent 024e6a6 commit 5b3fc0a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Sources/GraphQL/Error/GraphQLError.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,25 @@ public struct GraphQLError : Error, Codable {
114114
originalError: error
115115
)
116116
}
117+
118+
public init(from decoder: Decoder) throws {
119+
let container = try decoder.container(keyedBy: CodingKeys.self)
120+
message = try container.decode(String.self, forKey: .message)
121+
locations = try container.decode([SourceLocation]?.self, forKey: .locations) ?? []
122+
path = try container.decode(IndexPath.self, forKey: .path)
123+
}
124+
125+
public func encode(to encoder: Encoder) throws {
126+
var container = encoder.container(keyedBy: CodingKeys.self)
127+
128+
try container.encode(message, forKey: .message)
129+
130+
if !locations.isEmpty {
131+
try container.encode(locations, forKey: .locations)
132+
}
133+
134+
try container.encode(path, forKey: .path)
135+
}
117136
}
118137

119138
extension GraphQLError : CustomStringConvertible {
@@ -144,6 +163,16 @@ public struct IndexPath : Codable {
144163
public func appending(_ elements: IndexPathElement) -> IndexPath {
145164
return IndexPath(self.elements + [elements])
146165
}
166+
167+
public init(from decoder: Decoder) throws {
168+
var container = try decoder.unkeyedContainer()
169+
elements = try container.decode([IndexPathValue].self)
170+
}
171+
172+
public func encode(to encoder: Encoder) throws {
173+
var container = encoder.unkeyedContainer()
174+
try container.encode(contentsOf: elements)
175+
}
147176
}
148177

149178
extension IndexPath : ExpressibleByArrayLiteral {

0 commit comments

Comments
 (0)