Skip to content

Commit

Permalink
Fix GraphQLError encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofaria committed Jun 26, 2020
1 parent 024e6a6 commit 5b3fc0a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/GraphQL/Error/GraphQLError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@ public struct GraphQLError : Error, Codable {
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 {
Expand Down Expand Up @@ -144,6 +163,16 @@ public struct IndexPath : Codable {
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 {
Expand Down

0 comments on commit 5b3fc0a

Please sign in to comment.