From 5b3fc0a49b476277e2e2c429315be6b120872fb5 Mon Sep 17 00:00:00 2001 From: Paulo Faria Date: Fri, 26 Jun 2020 20:48:39 -0300 Subject: [PATCH] Fix GraphQLError encoding. --- Sources/GraphQL/Error/GraphQLError.swift | 29 ++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Sources/GraphQL/Error/GraphQLError.swift b/Sources/GraphQL/Error/GraphQLError.swift index 5a27cc53..125326f1 100644 --- a/Sources/GraphQL/Error/GraphQLError.swift +++ b/Sources/GraphQL/Error/GraphQLError.swift @@ -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 { @@ -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 {